同线程一样,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只会被创建一次,并且在关闭服务器的时候会被销毁
求关注嗷~~一起成长为大佬吧