Tomcat装载Servlet的三种情况

一 时刻1
Servlet容器启动时自动装载某些Servlet,实现它只需要在web.xml文件中的<Servlet></Servlet>之间添加如下代码
<loadon-startup>1</loadon-startup>
数字越小表示优先级别越高。
1 关键代码
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< web-app  version = "2.5"
         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
   < display-name ></ display-name >
   < 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 >        
   < welcome-file-list >
     < welcome-file > index.jsp </ welcome-file >
   </ welcome-file-list >
</ web-app >
2 测试
TestServlet2构造方法被执行....
TestServlet2的初始化方法被执行....
TestServlet1构造方法被执行....
TestServlet1的初始化方法被执行....
 
二 时刻2
在Servlet容器启动后,客户首次向Servlet发送请求。
1 关键代码
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); //让doPost()执行与doGet()相同的操作。
    }
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
        System.out.println("TestServlet1的初始化方法被执行....");
    }
}
 
2 测试
TestServlet1构造方法被执行....
TestServlet1的初始化方法被执行....
TestServlet1的doGet()方法被执行...
九月 24, 2017 2:51:01 下午 org.apache.catalina.core.StandardServer await
信息: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
九月 24, 2017 2:51:01 下午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["http-apr-8888"]
九月 24, 2017 2:51:01 下午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["ajp-apr-8009"]
九月 24, 2017 2:51:01 下午 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Catalina
九月 24, 2017 2:51:02 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextDestroyed()
九月 24, 2017 2:51:02 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextDestroyed()
TestServlet1销毁方法被执行....
 
三 时刻3
Servlet类文件被更新后,重新装载Servlet。
修改文件后,生成类文件时间发生变化


 
 
四 说明
Servlet被装载后,Servlet容器创建一个Servlet实例并且调用Servlet的init()方法进行初始化。在Servlet的整个生命周期,init()方法只被调用一次。

猜你喜欢

转载自cakin24.iteye.com/blog/2396237