サーブレットのライフサイクルに関する本を読むと、最初にサービスメソッドを呼び出し、次にサービスメソッドが別のメソッドを呼び出して特定の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