load-on-startup 元素是 web.xml 中的一个节点。具体地说,是 servlet 元素的子元素,用来标记 Servlet 容器启动时是否初始化当前 Servlet,以及当前 Servlet 的初始化顺序。
load-on-startup 元素的取值规则如下:
@WebServlet 注解的 loadOnStartup 属性与 web.xml 中的 load-on-startup 元素相对应,取值的规则和含义相同。
1、示例
下面通过一个示例进行说明:
1)在 servletDemo 项目的 com.hoperun.www 包中,创建名称为 MyServletTestLoadOnStartUp 的类,代码如下:
- package com.hoperun.www;
-
- import java.io.IOException;
-
- import jakarta.servlet.ServletException;
- import jakarta.servlet.http.HttpServlet;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
-
- public class MyServletTestLoadOnStartUp extends HttpServlet {
-
- private static final long serialVersionUID = 1L;
-
- @Override
- public void init() throws ServletException {
- System.out.println(this.getServletName() + ": 初始化完成!");
- }
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- }
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
-
- @Override
- public void destroy() {
- System.out.println(this.getServletName() + ": 销毁!");
- }
-
- }
2)web.xml 的配置如下:
- "1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="https://jakarta.ee/xml/ns/jakartaee"
- xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
- xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="5.0">
- <display-name>servletDemodisplay-name>
-
-
- <servlet>
- <servlet-name>MyServletTestLoadOnStartUp3servlet-name>
- <servlet-class>com.hoperun.www.MyServletTestLoadOnStartUpservlet-class>
- <load-on-startup>0load-on-startup>
- servlet>
- <servlet>
- <servlet-name>MyServletTestLoadOnStartUp2servlet-name>
- <servlet-class>com.hoperun.www.MyServletTestLoadOnStartUpservlet-class>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet>
- <servlet-name>MyServletTestLoadOnStartUp1servlet-name>
- <servlet-class>com.hoperun.www.MyServletTestLoadOnStartUpservlet-class>
- <load-on-startup>2load-on-startup>
- servlet>
-
-
- <servlet>
- <servlet-name>MyServletTestLoadOnStartUp4servlet-name>
- <servlet-class>com.hoperun.www.MyServletTestLoadOnStartUpservlet-class>
- <load-on-startup>-1load-on-startup>
- servlet>
-
-
- <servlet>
- <servlet-name>MyServletTestLoadOnStartUp5servlet-name>
- <servlet-class>com.hoperun.www.MyServletTestLoadOnStartUpservlet-class>
- servlet>
-
-
- <servlet-mapping>
- <servlet-name>MyServletTestLoadOnStartUp1servlet-name>
- <url-pattern>/MyServletTestLoadOnStartUp1url-pattern>
- servlet-mapping>
- <servlet-mapping>
- <servlet-name>MyServletTestLoadOnStartUp2servlet-name>
- <url-pattern>/MyServletTestLoadOnStartUp2url-pattern>
- servlet-mapping>
- <servlet-mapping>
- <servlet-name>MyServletTestLoadOnStartUp3servlet-name>
- <url-pattern>/MyServletTestLoadOnStartUp3url-pattern>
- servlet-mapping>
- <servlet-mapping>
- <servlet-name>MyServletTestLoadOnStartUp4servlet-name>
- <url-pattern>/MyServletTestLoadOnStartUp4url-pattern>
- servlet-mapping>
- <servlet-mapping>
- <servlet-name>MyServletTestLoadOnStartUp5servlet-name>
- <url-pattern>/MyServletTestLoadOnStartUp5url-pattern>
- servlet-mapping>
- web-app>
3)启动Tomcat服务器,控制台显示如下:
4)在地址栏输入http://localhost:8080/servletDemo/MyServletTestLoadOnStartUp1,访问 MyServletTestLoadOnStartUp1,结果如下图:
5)在地址栏输入http://localhost:8080/servletDemo/MyServletTestLoadOnStartUp4,访问 MyServletTestLoadOnStartUp4,结果如下图:
6)在地址栏输入http://localhost:8080/servletDemo/MyServletTestLoadOnStartUp5,访问 MyServletTestLoadOnStartUp5,结果如下图:
总结: