同線程一樣,servlet也有建立,使用,銷毀三個階段
順便複習一下多線程的生命周期吧~~
Servlet的生命周期
代碼實作一下~~
package com.Myservlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
*
* @Auther: 風離
* @Date: 2020/11/26/0:24
* @Description:
*/
@WebServlet("/Servlet00")
public class Servlet生命周期 extends HttpServlet {
//init
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("Servlet被建立了!!");
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("服務被調用了~");
}
@Override
public void destroy() {
System.out.println("Servlet被銷毀了!");
}
}
記住這裡的“/”不能省略,否則會運作失敗
很明顯了,Servlet隻會被建立一次,并且在關閉伺服器的時候會被銷毀
求關注嗷~~一起成長為大佬吧