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:
- ﹤?xml version="1.0" encoding="gb2312"?﹥
- ﹤web-app﹥
- ﹤display-name﹥My Web Application﹤/display-name﹥
- ﹤description﹥
- A application for test.
- ﹤/description﹥
- ﹤/web-app﹥
5. Create a test jsp page under myapp. The file name is index. jsp. The file content is as follows:
- ﹤html﹥
- ﹤body﹥
- ﹤center﹥Now time is: ﹤%=new java.util.Date()%﹥﹤/center﹥
- ﹤/body﹥
- ﹤/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;
- 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 TestServlet extends HttpServlet ...{
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{
- PrintWriter out=response.getWriter();
- out.println("﹤html﹥﹤body﹥﹤h1﹥This is a servlet test.﹤/h1﹥﹤/body﹥﹤/html﹥");
- out.flush();
- }
- }
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:
- ﹤?xml version="1.0" encoding="gb2312"?﹥
- ﹤web-app﹥
- ﹤display-name﹥My Web Application﹤/display-name﹥
- ﹤description﹥
- A application for test.
- ﹤/description﹥
- ﹤servlet﹥
- ﹤servlet-name﹥Test﹤/servlet-name﹥
- ﹤servlet-class﹥test.TestServlet﹤/servlet-class﹥
- ﹤/servlet﹥
- ﹤servlet-mapping﹥
- ﹤servlet-name﹥Test﹤/servlet-name﹥
- ﹤url-pattern﹥/Test﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
- ﹤/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;
- 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 D: \ Tomcat6.0 \ webapps \ myapp \ WEB-INF \ classes \ test.
4. Create a New testBean. jsp file with the following content:
- ﹤%...@ page import="test.TestBean" %﹥
-
- ﹤html﹥
- ﹤body﹥
- ﹤center﹥
- ﹤%...
- TestBean testBean = new TestBean("This is a test java bean.");
- %﹥
- Java bean name is: ﹤%=testBean.getName()%﹥
- ﹤/center﹥
- ﹤/body﹥
- ﹤/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!
- Analysis on the working principle of JSP + JavaBean + Servlet Structure
- At the beginning of JSP Servlet Development
- Java Servlet API documentation
- How to Improve Servlet and JSP Application Efficiency
- Functions and principles of several encodings in JSP and Servlet