tomcat装载servlet的三种情况
package servlet;
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 TestServlet1 extends HttpServlet {
/**
* Constructor of the object.
*/
public TestServlet1() {
System.out.println("TestServlet1构造方法被执行");
}
/** * Destruction of the servlet. <br> */public void destroy() { System.out.println("TestServlet1销毁方法被执行");}/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred * */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("TestServlet1的doGet方法被执行。。。。。");response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.println("<h1>您好,我是TestServlet1</h1>");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("TestServlet1的doPost方法被执行。。。。。"); doGet(request,response); //dePost执行与doGet相同的操作}/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException {System.out.println("TestServlet1的初始化方法被执行。。。。。");}}
package servlet;
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 TestServlet2 extends HttpServlet {
/**
* Constructor of the object.
*/
public TestServlet2() {
System.out.println("TestServlet2构造方法被执行");
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
System.out.println("TestServlet2销毁方法被执行");
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("TestServlet2的doGet方法被执行。。。。。");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<h1>您好,我是TestServlet2</h1>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("TestServlet2的doPost方法被执行。。。。。");
doGet(request,response); //dePost执行与doGet相同的操作
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
System.out.println("TestServlet2的初始化方法被执行。。。。。");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet1</servlet-name>
<servlet-class>servlet.TestServlet1</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet2</servlet-name>
<servlet-class>servlet.TestServlet2</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet1</servlet-name>
<url-pattern>/servlet/TestServlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet2</servlet-name>
<url-pattern>/servlet/TestServlet2</url-pattern>
</servlet-mapping>
</web-app>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet1</servlet-name>
<servlet-class>servlet.TestServlet1</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet2</servlet-name>
<servlet-class>servlet.TestServlet2</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet1</servlet-name>
<url-pattern>/servlet/TestServlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet2</servlet-name>
<url-pattern>/servlet/TestServlet2</url-pattern>
</servlet-mapping>
</web-app>
如何执行:先执行构造方法,然后是init初始化方法,然后是doget方法
当有俩个实例该如何执行,谁先执行,谁后执行呢,不是随机的,是由程序说了算, <load-on-startup>1</load-on-startup>,这句话来确定,等级越高,里面的数越小,越先被执行