package net.biancheng.www; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override public void destroy() { System.out.println(this.getServletName() + ":销毁"); } @Override public void init() throws ServletException { System.out.println(this.getServletName() + ":初始化完成"); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" metadata-complete="false" version="4.0"> <servlet> <servlet-name>MyServlet1</servlet-name> <servlet-class>net.biancheng.www.MyServlet1</servlet-class> <!-- load-on-startup 取值0 --> <load-on-startup>0</load-on-startup> </servlet> <servlet> <servlet-name>MyServlet2</servlet-name> <servlet-class>net.biancheng.www.MyServlet1</servlet-class> <!-- load-on-startup 取值1 --> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>MyServlet3</servlet-name> <servlet-class>net.biancheng.www.MyServlet1</servlet-class> <!-- load-on-startup 取值2 --> <load-on-startup>2</load-on-startup> </servlet> <servlet> <servlet-name>MyServlet4</servlet-name> <servlet-class>net.biancheng.www.MyServlet1</servlet-class> <!-- load-on-startup 取值-1--> <load-on-startup>-1</load-on-startup> </servlet> <!--不设置 load-on-startup ,去默认值 --> <servlet> <servlet-name>MyServlet5</servlet-name> <servlet-class>net.biancheng.www.MyServlet1</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet1</servlet-name> <url-pattern>/MyServlet1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyServlet2</servlet-name> <url-pattern>/MyServlet2</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyServlet3</servlet-name> <url-pattern>/MyServlet3</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyServlet4</servlet-name> <url-pattern>/MyServlet4</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyServlet5</servlet-name> <url-pattern>/MyServlet5</url-pattern> </servlet-mapping> </web-app>启动 Tomcat 服务器,控制台输出如下图所示。
本文链接:http://task.lmcjl.com/news/4338.html