JSP Servlet JavaBean built under jdk6.0 Tomcat6.0

Source: Internet
Author: User

What are the requirements for JSP Servlet JavaBean construction? Preparations:

Install and configure jdk6.0 and tomcat6.0 for JSP Servlet JavaBean Construction

Debugging of JSP Servlet JavaBean construction (jsp ):

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="gb2312"?﹥  
  2. ﹤web-app﹥  
  3. ﹤display-name﹥My Web Application﹤/display-name﹥  
  4. ﹤description﹥  
  5. A application for test.  
  6. ﹤/description﹥  
  7. ﹤/web-app﹥ 

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

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

6. Restart Tomcat.

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

Debugging of JSP Servlet JavaBean construction (servlet ):

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

Package test;

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

2. Compile

Place TestServlet. java under c: \ test and compile it with the following command:
C: \ Test> javac TestServlet. java
Then a compiled servlet File TestServlet. class will be generated under c: \ Test.
(If the import javax. servlet. * fails during compilation .*. So we should copy the servlet-api.jar file \ Tomcat \ lib to D: \ Java \ jdk1.6.0 \ lib, and add the environment variable % JAVA_HOME % \ lib \ servlet-api.jar to the classpath for re-compilation, there is no problem)

3. Cut the structure test \ TestServlet. class to D: \ Tomcat6.0 \ 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 test \ Test. class file directory structure.

4. web. modify webapps \ myapp \ WEB-INF \ web. xml, add servlet and servlet-mapping. the edited web. xml is as follows, and red is the added content:

 
 
  1. ﹤?xml version="1.0" encoding="gb2312"?﹥  
  2. ﹤web-app﹥  
  3. ﹤display-name﹥My Web Application﹤/display-name﹥  
  4. ﹤description﹥  
  5. A application for test.  
  6. ﹤/description﹥  
  7. ﹤servlet﹥  
  8. ﹤servlet-name﹥Test﹤/servlet-name﹥  
  9. ﹤servlet-class﹥test.TestServlet﹤/servlet-class﹥  
  10. ﹤/servlet﹥  
  11. ﹤servlet-mapping﹥  
  12. ﹤servlet-name﹥Test﹤/servlet-name﹥  
  13. ﹤url-pattern﹥/Test﹤/url-pattern﹥  
  14. ﹤/servlet-mapping﹥  
  15. ﹤/web-app﹥ 

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.

Debugging of JSP Servlet JavaBean construction (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:

Package test;

 
 
  1. public class TestBean ...{  
  2. private String name = null;  
  3.  
  4. public TestBean(String strName_p) ...{  
  5. this.name=strName_p;  
  6. }  
  7.  
  8. public void setName(String strName_p) ...{  
  9. this.name=strName_p;  
  10. }  
  11.  
  12. public String getName() ...{  
  13. return this.name;  
  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 D: \ Tomcat6.0 \ webapps \ myapp \ WEB-INF \ classes \ test.

4. Create a New testBean. jsp file with the following content:

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

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.

The construction of JSP Servlet JavaBean has been completed in Tomcat. Is it very simple? Of course, it is necessary for beginners to step by step and practice more!

  1. Analysis on the working principle of JSP + JavaBean + Servlet Structure
  2. At the beginning of JSP Servlet Development
  3. Java Servlet API documentation
  4. How to Improve Servlet and JSP Application Efficiency
  5. Functions and principles of several encodings in JSP and 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.