public class LifeServlet extends HttpServlet {
//构造方法
public LifeServlet(){
System.out.println("LifeServlet");
}
//服务请求----每请求一次,就执行一次
//method不管是get还是post,service都可以执行
//一般有service方法后不需要再写doGet和doPost方法
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("service");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
}
//初始化方法,Servlet第一次加载进行存储时执行
@Override
public void init() throws ServletException {
System.out.println("init");
}
//销毁方法---关闭服务器时执行
@Override
public void destroy() {
System.out.println("destory");
}
}
类加载时机:
1.默认第一次访问当前servlet的时候加载。
2.配置这个标签就会在启动服务器的时候加载servlet