当我阅读有关 servlet 生命周期的书时,它说它首先调用服务方法,然后服务方法调用另一个方法来处理特定的 HTTP 请求(GET 或 POST)。但是当我尝试这个时,我发现在调用服务方法之前首先调用 doGet 或 doPost 方法。我的代码和结果如下,谢谢!
public class Main extends HttpServlet {
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
System.out.println("init has been called");
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(arg0, arg1);
System.out.println("service has been called");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("get has been called");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("post has been called");
}
}
结果:
init has been called
get has been called
service has been called
post has been called
service has been called