Servlet的生命周期
初始化init()
服务器启动时(如果web.xml中配置load-on-startup=1)那么在容器启动时就会执行初始化方法init(ServletConfig conf),反之(如果web.xml中配置load-on-startup=0,默认为0),第一次请求该servlet时,就会初始化一个Servlet对象,也就是会执行初始化方法init(ServletConfig conf)
public void init() throws ServletException {
System.out.println("初始化方法");
}
服务Service()
servlet对象去处理所有的客户端请求, 并在service(ServletRequest req,ServletResponse res)方法进行相应的处理
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
System.out.println("服务处理");
}
销毁destroy()
最后服务器关闭时,才会销毁这个servlet对象,执行destroy()方法
先执行destroy()方法,再销毁servlet对象
public void destroy() {
System.out.println("销毁方法");
}