servlt的生命周期
在学习servlet的生命周期,我一共用到了HellServlt类和三个方法init(),doGet和Service。Destroy这个肯定是最后运行的所以就没有试。
代码在下面。运行结果如图
由图我们可以推出Servlet的生命周期。
1.运行类
2.初始化
3.如果有doget或者dopost先运行
4.Service
5.Destroy
package com.imcode.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
public HelloServlet(){
System.out.println("test print");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().println(request.getParameter("name"));
System.out.println("doget print");
}
@Override
public void init() throws ServletException {
super.init();
System.out.println("init print");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
System.out.println("service print");
}
}