---
title: "Tomcat装载Servlet的三种情况"
author: "cakin24"
date: "2017-10-13T20:08:27Z"
tags: []
categories: ["编程语言"]
slug: "tomcat-servlet"
draft: false
lastmod: "2017-10-13T20:08:27Z"
id: 2396237
url: "https://cakin24.iteye.com/blog/2396237"
---

一 时刻1

Servlet容器启动时自动装载某些Servlet，实现它只需要在web.xml文件中的&lt;Servlet&gt;&lt;/Servlet&gt;之间添加如下代码

&lt;loadon-startup&gt;1&lt;/loadon-startup&gt;

数字越小表示优先级别越高。

1 关键代码

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app version="2.5"

        xmlns="[http://java.sun.com/xml/ns/javaee"](http://java.sun.com/xml/ns/javaee%22);

        xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22);

        xsi:schemaLocation="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)

[http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"](http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd%22);&gt;

  &lt;display-name&gt;&lt;/display-name&gt;

  &lt;servlet&gt;

    &lt;description&gt;This is the description of my J2EE component&lt;/description&gt;

    &lt;display-name&gt;This is the display name of my J2EE component&lt;/display-name&gt;

    &lt;servlet-name&gt;TestServlet1&lt;/servlet-name&gt;

    &lt;servlet-class&gt;servlet.TestServlet1&lt;/servlet-class&gt;

 

    &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;

 

  &lt;/servlet&gt;

  &lt;servlet&gt;

    &lt;description&gt;This is the description of my J2EE component&lt;/description&gt;

    &lt;display-name&gt;This is the display name of my J2EE component&lt;/display-name&gt;

    &lt;servlet-name&gt;TestServlet2&lt;/servlet-name&gt;

    &lt;servlet-class&gt;servlet.TestServlet2&lt;/servlet-class&gt;

 

    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;

 

  &lt;/servlet&gt;

 

 

  &lt;servlet-mapping&gt;

    &lt;servlet-name&gt;TestServlet1&lt;/servlet-name&gt;

    &lt;url-pattern&gt;/servlet/TestServlet1&lt;/url-pattern&gt;

  &lt;/servlet-mapping&gt;

  &lt;servlet-mapping&gt;

    &lt;servlet-name&gt;TestServlet2&lt;/servlet-name&gt;

    &lt;url-pattern&gt;/servlet/TestServlet2&lt;/url-pattern&gt;

  &lt;/servlet-mapping&gt;       

  &lt;welcome-file-list&gt;

    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

  &lt;/welcome-file-list&gt;

&lt;/web-app&gt;

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. &lt;br&gt;

     \*/

    public void destroy() {

        System.out.println("TestServlet1销毁方法被执行....");

    }

    /\**

     * The doGet method of the servlet. &lt;br&gt;

     *

     * 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("&lt;!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.01 Transitional//EN\\"&gt;");

        out.println("&lt;HTML&gt;");

        out.println("  &lt;HEAD&gt;&lt;TITLE&gt;A Servlet&lt;/TITLE&gt;&lt;/HEAD&gt;");

        out.println("  &lt;BODY&gt;");

        out.println("&lt;h1&gt;大家好，我是TestServlet1！&lt;/h1&gt;");

        out.println("  &lt;/BODY&gt;");

        out.println("&lt;/HTML&gt;");

        out.flush();

        out.close();

    }

    /\**

     * The doPost method of the servlet. &lt;br&gt;

     *

     * 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. &lt;br&gt;

     *

     * @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。

修改文件后，生成类文件时间发生变化

![](http://dl2.iteye.com/upload/attachment/0127/3644/3dc408a3-e455-3d57-b3d0-9573d103cd05.png)  
 

 

四 说明

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