Configuration of JSP, Servlet, and JavaBean environments in Tomcat

Source: Internet
Author: User
Configuration of JSP, Servlet, and JavaBean environment in Tomcat-general Linux technology-Linux programming and kernel information. For more information, see the following. I often see jsp beginners asking how to configure jsp, servlet, and bean in tomcat. So I summarized how to configure jsp, servlet, and ben in tomcat, hoping to help beginners.

I. Development Environment Configuration

Step 1: Download j2sdk and tomcat: To the sun official site (http://java.sun.com/j2se/1.4.2/download.html) download j2sdk, pay attention to download the version of Windows Offline Installation SDK, at the same time it is best to download J2SE 1.4.2 Documentation, then go to the tomcat official site (http://www.apache.org/dist/jakarta/tomcat-4/) to download tomcat (download the latest version of tomcat 4.1.x );

Step 2: install and configure your j2sdk and tomcat: Execute the j2sdk and tomcat installation programs and install them according to the default settings.
1. after installing j2sdk, You need to configure the environment variables, choose my computer> Properties> advanced> environment variables> system variables to add the following environment variables (assuming your j2sdk is installed in c: \ j2sdk1.4.2 ):
JAVA_HOME = c: \ j2sdk1.4.2
Classpath =.; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar; (.; must not be small, because it represents the current path)
Path = % JAVA_HOME % \ bin
Then, you can write a simple java program to test whether J2SDK has been installed successfully:
Public class Test {
Public static void main (String args []) {
System. out. println ("This is a test program .");
}
}
Save the above program as a file named Test. java.
Then open the Command Prompt window, cd to the directory where your Test. java is located, and then type the following command
Javac Test. java
Java Test
If This is a test program is printed, the installation is successful. If This is not printed, You need to carefully check your configuration.
2. after Tomcat is installed, add the following environment variables to my computer> Properties> advanced> environment variables> system variables (assuming your tomcat is installed in c: \ tomcat ):
CATALINA_HOME = c: \ tomcat
CATALINA_BASE = c: \ tomcat
Modify the classpath in the environment variable, and append servlet. jar under the common \ lib directory under the tomat installation directory (you can append it according to the actual situation) to the classpath. The modified classpath is as follows:
Classpath =.; % JAVA_HOME % \ lib \ dt. jar; % JAVA_HOME % \ lib \ tools. jar; % CATALINA_HOME % \ common \ lib \ servlet. jar;
Start tomcat and access http: // localhost: 8080 in IE. If you see the welcome page of tomcat, the installation is successful.

Step 3: Create your own jsp app directory
1. Go to the webapps directory of the Tomcat installation directory, and you can see the tomcat built-in directories such as ROOT, examples, and Tomcat-docs;
2. Create a directory named myapp under the webapps directory;
3. Create a directory WEB-INF under myapp, note that the directory name is case sensitive;
4. Create a file web. xml under the WEB-INF with the following content:

PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 // EN"
Http://java.sun.com/dtd/web-app_2_3.dtd>

My Web Application

A application for test.


5. Create a test jsp page under myapp. The file name is index. jsp. The file content is as follows:

Now time is: <% = new java. util. Date () %>

6. Restart Tomcat
7. Open the browser and enter http: // localhost: 8080/myapp/index. jsp to view the current time.

Step 4: Create your own Servlet:
1. Use the editor you are most familiar with (it is recommended to use java ide with syntax check) to create a servlet program named Test. java. The file content is as follows:
Package test;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class Test extends HttpServlet {
Protected void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
PrintWriter out = response. getWriter ();
Out. println ("This is a servlet test.");
Out. flush ();
}
}
2. Compile
Place Test. java under c: \ test and compile it with the following command:
C: \ Test> javac Test. java
Then a compiled servlet File: Test. class will be generated under c: \ Test.
3. structure test \ Test. class cut to % CATALINA_HOME % \ webapps \ myapp \ WEB-INF \ classes, that is, cut the test directory to the classes directory, if the classes directory does not exist, create a new one. Now webapps \ myapp \ WEB-INF \ classes has the file directory structure test \ Test. class
4. Modify webapps \ myapp \ WEB-INF \ web. xml, add servlet and servlet-mapping
The edited web. xml is shown as follows, and red indicates the added content:

PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 // EN"
Http://java.sun.com/dtd/web-app_2_3.dtd>

My Web Application

A application for test.


Test
Test
A test Servlet
Test. Test


Test
/Test


In this section, servlet declares the Servlet you want to call, while servlet-mapping maps the declared servlet to the address/Test.
5. Restart Tomcat, start the browser, and enter http: // localhost: 8080/myapp/Test. If This is a servlet test, the servlet is successfully written.
Note: You must restart Tomcat after modifying web. xml and adding a new class.

Step 4: Create your own Bean:
1. Use the editor you are most familiar with (we recommend using java ide with syntax check) to create a java program named TestBean. java. The file content is as follows:
Package test;
Public class TestBean {
Private String name = null;
Public TestBean (String strName_p ){
This. name = strName_p;
}
Public void setName (String strName_p ){
This. name = strName_p;
}
Public String getName (){
Return this. name;
}
}
2. Compile
Place TestBean. java under c: \ test and compile it with the following command:
C: \ Test> javac TestBean. java
Then a compiled bean file TestBean. class will be generated under c: \ Test.
3. Cut the TestBean. class file to % CATALINA_HOME % \ webapps \ myapp \ WEB-INF \ classes \ test,
4. Create a New TestBean. jsp file with the following content:
<% @ Page import = "test. TestBean" %>

<%
TestBean testBean = new TestBean ("This is a test java bean .");
%>
Java bean name is: <% = testBean. getName () %>

5. restart Tomcat, start the browser, and enter http: // localhost: 8080/myapp/TestBean. jsp if the output Java bean name is: This is a test java bean. it indicates that the Bean is successfully written.
In this way, the configuration of jsp, servlet, and javabean in Tomcat is completed. What needs to be done next is to read more books and read more good code from others. You need to write more code to enhance your development capabilities in this area.

Jvm should be filled in
C: \ j2sdk \ bin

A simple configuration ::::

JSP environment configuration experience

First of all, we can use jdk + tomcat to configure our jsp server. We don't need anything. Many articles have introduced Apache, but we don't need it at all, general learning and debugging of tomcat is fully qualified.
After jdk is installed, tomcat will automatically find the jdk installation path before installation, and click "Next" all the way. After a period of file copying, and finally "close", complete comcat installation.
You 'd better download a tomcat version with a higher version, such as 4.1 or above, because it does not need to set too many system variables, right-click "my computer ", select "property"-> "advanced"-> "environment variable"-> "system variable" to create a TOMCAT_HOME, and set the value to the path of your tomcat, for example, D: \ Program Files \ Apache Group \ Tomcat 4.1. The configuration is complete.
Find the tomcat option from the Start Menu. The general opening sequence is Start> program> Apache Tomcat 4.1. Select Start Tomcat to Start running the jsp server, in this case, a Dos-like window is opened, and some related information is displayed.
If you use a proxy to access the Internet, you must remove the proxy first, otherwise your jsp program will never be executed. If it is not a proxy, skip this step.
Open your browser and enter http: // localhost: 8080 in the address bar. If you see a picture of a tiger (I don't know whether it is a tiger or a cat), congratulations, half of your success.
To enjoy the success, enter the following code:


First Page


Today is: h
<% = New java. util. Date () %>



Save the program as: First. put jsp in the ROOT directory of Tomcat, and enter: http: // localhost: 8080/First in the address bar of the browser. jsp, (First. jsp is the same as the file name we saved.) Press enter. If not, you can see the result in the format of Today is: h Fri Apr 11 08:32:38 CST 2003.
Note: ROOT is the default virtual directory of tomcat. What should I do if I want to change it to my own virtual directory? Continue.
To change to your own virtual directory, you need to go to the server. the xml file is a configuration file. You can open it in the Tomcat \ conf directory using any text editing software. First, find the following sentence:
Port = "8080" minProcessors = "5" maxProcessors = "75"
EnableLookups = "true" redirectPort = "8443"
AcceptCount = "100" debug = "0" connectionTimeout = "20000"
UseURIValidationHack = "false" disableUploadTimeout = "true"/>
The port = "8080" here is the port. We can use another port instead, but it cannot be the port occupied by the system (0--1023.
Next we will find the following statement:


We should find these two statements. If you do not understand E, you should think that these two statements are good. Then we change the statement as follows:




Here, path = "/myjsp" is the virtual directory we configured. Enter http: // localhost: 8080/myjsp in the address bar. DocBase = "e:/myjsp" is the local path of the machine. They form a ing relationship through this statement.
Put the above First. jsp file in the e:/myjsp directory and input http: // localhost: 8080/myjsp/First. jsp. Is there a pleasant sensation?
What I see most in the Forum is that many people do not know where to put the javaBean file. To be honest, I do not know it at first. What's even more puzzling is that there are nine different statements about ten people, this makes us even more confused. In fact, this problem is not as complicated as we think. We will illustrate it with an example:
Create a java program with the following code:
Package hall;
Public class SimpleBean {
Private String message = "No message specified ";
Public String getMessage (){
Return (message );
}
Public void setMessage (String message ){
This. message = message;
}
}
Save as SimpleBean. java. After compilation, a package will be generated. In fact, it is equivalent to a directory, that is, SimpleBean. class will be stored in the hall directory. It will be saved temporarily and will be used up in the future.
Enter the following code:


Reusing JavaBeans in JSP





Reusing JavaBeans in JSP





Message:



Save it in the virtual directory e:/myjsp we just created and name it BeanTest. jsp.
Where should we put the hall (Package) directory now? Don't worry, we first create a folder WEB-INF under e:/myjsp, then create a classes folder under the WEB-INF, and finally put the hall directory under classes, of course, simpleBean. the class also needs to be moved, while SimpleBean. java and BeanTest. put jsp in the same directory (you can try it yourself if you don't need it ).
All right, it's done. Restart the machine (if you have tried it many times, you must do it). In the browser, enter: http: // localhost: 8080/myjsp/BeanTest. jsp, what do you see? Oh, don't tell me you haven't seen anything. It must have been a problem with your settings.
Well, I am only a cainiao after I finish writing the article, so please advise me if you are not prepared. Wish you a pleasant trip to jsp !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.