Preparation:
Install and configure jdk6.0 and tomcat6.0
debugging (jsp):
1. Go to the webapps directory of Tomcat's installation directory. You can see ROOT, examples, tomcat-docs and other directories that come with Tomcat.
2. In Create a new directory under the webapps directory and name it myapp.
3. Create a new directory WEB-INF under myapp. Note that the directory name is case-sensitive.
4. Create a new file web.xml under 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 new 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. If you see the current time, it means it is successful.
Debugging (servlet):
1. Use the editor you are most familiar with ( It is recommended to use a java ide with syntax checking) to create a new servlet program. The file name is TestServlet.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 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 using the following command:
C:Test>javac TestServlet.java
Then a compiled servlet file will be generated under c:Test: TestServlet.class
(If you are unable to import javax.servlet.* during compilation. Then you should copy the servlet-api.jar file in Tomcatlib to D:Javajdk1.6.0lib and add environment variables to the classpath. Compile %JAVA_HOME%libservlet-api.jar again, and there will be no problem)
3. Cut the structure testTestServlet.class to D:Tomcat6.0webappsmyappWEB-INFclasses, also Just cut the test directory to the classes directory. If the classes directory does not exist, create a new one. Now there is a file directory structure of testTest.class under webappsmyappWEB-INFclasses.
4. (Register web.xml file) Modify webappsmyappWEB-INFweb.xml and add servlet and servlet- mapping. The edited web.xml is as follows, with the added content in red:
<?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> The servlet section in this paragraph declares the Servlet you want to call, and servlet-mapping "maps" the declared servlet to the address/Test.
5. Okay, restart Tomcat, Start the browser and enter http://localhost:8080/myapp/Test. If you see the output This is a servlet test., it means that the written servlet is successful.
Note: If you modify web.xml and add a new class, you must restart Tomcat
debugging (Bean):
1. Use your most familiar editor (it is recommended to use a java ide with syntax checking) to create a new java program with the file name 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 use the following command to compile:
C:test>javac TestBean.java
Then a compiled bean file will be generated under c:test: TestBean.class
3. Cut the TestBean.class file to D:Tomcat6.0webappsmyappWEB-INFclassestest.
4 .Create a new testBean.jsp file with the following contents:
<%...@ 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. Okay, restart Tomcat, start the browser, enter http://localhost:8080/myapp/testBean.jsp. If you see the output Java bean name is: This is a test java bean., it means that the Bean written is successful.
This completes the configuration of jsp, servlet and javabean under Tomcat.