Servlet doGet() doPost() method
Generally we specify the doGet
and doPost
in our HTML code, so that servlet will invoke these methods with respect to the call in HTML code.
Is there any way that whether we call doPost
or d开发者_如何学运维oGet
Servlet doPost
method will get called?
I know there is one way that in doGet
we can call doPost
method , But apart from that is there any other method .
Calling one from the other, or better - calling a 3rd method from both is the best approach.
You can override the service()
method as well, but there's more code there that you might not want to loose.
You can create a common method and put it into doGet() or doPost().
protected void processRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
精彩评论