Detailed steps for JSP + Tomcat classic configuration instances

Source: Internet
Author: User

The following describes 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.

◆ JSP + Tomcat Development Environment Configuration

Step 1 of JSP + Tomcat configuration:

Download j2sdk and Tomcat:

Download j2sdk to sun official site (http://java.sun.com/j2se/1.5.0/download.jsp), pay attention to download the version of Windows Offline Installation SDK, at the same time it is best to download J2SE 1.5.0 Documentation, and then to Tomcat official site (http://jakarta.apache.org/site/downloads/downloads_Tomcat-5.cgi) download Tomcat (download the latest version of Tomcat 5.9 );

Step 2 of JSP + Tomcat configuration:

Install and configure your j2sdk and Tomcat:

Run 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, add the following environment variables to my computer> Properties> advanced> environment variables> system variables (assuming your j2sdk is installed in c: \ j2sdk1.5.0 ):

JAVA_HOME = c: \ j2sdk1.5.0
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:

 
 
  1. public class Test{  
  2.      public static void main(String args[]){  
  3.          System.out.println("This is a test program.");  
  4.      }  
  5. }  
  6.  

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 that 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 of JSP + Tomcat configuration:

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:

 
 
  1. ﹤?xml version="1.0" encoding="ISO-8859-1"?﹥  
  2. ﹤!DOCTYPE web-app  
  3. PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  4. "http://java.sun.com/dtd/web-app_2_3.dtd"﹥  
  5. ﹤web-app﹥  
  6. ﹤display-name﹥My Web Application﹤/display-name﹥  
  7. ﹤description﹥  
  8. A application for test.  
  9. ﹤/description﹥  
  10. ﹤/web-app﹥  
  11.  

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

 
 
  1. ﹤html﹥﹤body﹥﹤center﹥  
  2. Now time is: ﹤%=new java.util.Date()%﹥  
  3. ﹤/center﹥﹤/body﹥﹤/html﹥  
  4.  

6. Restart Tomcat

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

Step 4 of JSP + Tomcat configuration:

Create your own Servlet:

1. Use the editor you are most familiar with to use java ide with syntax check) to create a servlet program named Test. java. The file content is as follows:

 
 
  1. package test;  
  2. import java.io.IOException;  
  3. import java.io.PrintWriter;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. public class Test extends HttpServlet {  
  9. protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  10. throws ServletException, IOException {  
  11. PrintWriter ut=response.getWriter();  
  12. out.println("﹤html﹥﹤body﹥﹤h1﹥This is a servlet test.﹤/h1﹥﹤/body﹥﹤/html﹥");  
  13. out.flush();  
  14. }  
  15. }  
  16.  

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:

 
 
  1. ﹤?xml version="1.0" encoding="ISO-8859-1"?﹥  
  2. ﹤!DOCTYPE web-app  
  3. PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  4. "http://java.sun.com/dtd/web-app_2_3.dtd"﹥  
  5. ﹤web-app﹥  
  6. ﹤display-name﹥My Web Application﹤/display-name﹥  
  7. ﹤description﹥  
  8. A application for test.  
  9. ﹤/description﹥  
  10. ﹤servlet﹥  
  11. ﹤servlet-name﹥Test﹤/servlet-name﹥  
  12. ﹤display-name﹥Test﹤/display-name﹥  
  13. ﹤description﹥A test Servlet﹤/description﹥  
  14. ﹤servlet-class﹥test.Test﹤/servlet-class﹥  
  15. ﹤/servlet﹥  
  16. ﹤servlet-mapping﹥  
  17. ﹤servlet-name﹥Test﹤/servlet-name﹥  
  18. ﹤url-pattern﹥/Test﹤/url-pattern﹥  
  19. ﹤/servlet-mapping﹥  
  20. ﹤/web-app﹥  
  21.  

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 of JSP + Tomcat configuration: Create your own Bean:

1. Use the editor you are most familiar with to use java ide with syntax check) to create a java program named TestBean. java. The file content is as follows:

 
 
  1. package test;  
  2. public class TestBean{  
  3. private String name = null;  
  4. public TestBean(String strName_p){  
  5. this.name=strName_p;  
  6. }  
  7. public void setName(String strName_p){  
  8. this.name=strName_p;  
  9. }  
  10. public String getName(){  
  11. return this.name;  
  12. }  
  13. }  
  14.  

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:

 
 
  1. ﹤%@ page import="test.TestBean" %﹥  
  2. ﹤html﹥﹤body﹥﹤center﹥  
  3. ﹤%  
  4. TestBean testBean=new TestBean("This is a test java bean.");  
  5. %﹥  
  6. Java bean name is: ﹤%=testBean.getName()%﹥  
  7. ﹤/center﹥﹤/body﹥﹤/html﹥  
  8.  

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

JSP + Tomcat configuration experiences

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 "attribute"-> "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 5.5. The configuration is complete.

Find the Tomcat option from the Start Menu. The general opening sequence is Start> program> Apache Tomcat 5.5. Select Start Tomcat to Start running the jsp server, in this case, a Dos-like window is opened, and some related information is displayed.

So, as shown in the preceding JSP + Tomcat configuration, do you have some knowledge?

  1. JSP tutorial basics: Technical Features of JSP
  2. Specific algorithms for implementing the tree structure of JSP forums
  3. How to export Oracle Data Tables Using JSP
  4. Implement page Jump in JSP
  5. What is JSP and comparison with Servlet?

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.