1.什么是servlet的生命周期?
servlet的生命周期是指servlet在容器中开始实例化到实例销毁的整个过程
2.实例化:
①:通过配置文件web.xml实例化。
public class ActionServlet extends HttpServlet {
//1、构造器
public ActionServlet(){
System.out.println("servlet对象实例化中.....");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>20180510</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/servlet/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
②:注解。
23-Jan-2021 21:52:28.552 信息 [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
23-Jan-2021 21:52:28.554 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 39 ms
Connected to server
[2021-01-23 09:52:29,482] Artifact ZhenaiDemo:war exploded: Artifact is being deployed, please wait...
23-Jan-2021 21:52:29.779 警告 [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
23-Jan-2021 21:52:30.540 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
servlet对象实例化中.....
3.就绪:
service()方法是执行实际任务的主要方法,Web服务器将调用service方法处理客户端(浏览器)的请求,然后将格式化的数据响应给浏览器客户端。每当客户端向服务端发送一个请求时,服务端都会产生一个新的线程调用服务。service方法将会检测客户端发送给服务端的HTTP请求协议的类型,从而调用service方法中的 doGet、doPost、doPut、doDelete 等方法进行实际任务的逻辑处理。
4.销毁。
销毁状态:调用了destroy()方法后,当前servlet实例将会被标记为回收垃圾,会对servlet实例进行清除处理。
public void destroy() {
System.out.println("servlet实例销毁中....");
}