Should I override service() or doPost()?

后端 未结 5 2043
余生分开走
余生分开走 2021-01-04 08:53

I was reading a book on servlets, in that book a brief explanation is given about the servlet class, as well as the HttpServlet class.

There is one exam

相关标签:
5条回答
  • 2021-01-04 09:18

    The service() method belongs to Genericservlet and can be overloaded to support any type of protocol such as Http,Ftp etc.

    Then you have specialized servlet for handling HttpProtocol, we call it HttpServlet. The HttpServlet also provides default implementation for service() and doGet() and doPost() methods.

    Why we should not override the service() method?

    Since It's not a good practice to override the service method. If we call any of the doxxx method then internally it will call the service method of the HttpServlet. So there's no need for you to call it explicitly.

    Order of Execution of service():

    service(ServletRequest,ServletResponse)-->

    -->calls

    -->service(HttpServletRequest req,HttpServletResponse res)

    -->calls

    -->doGet/doPost(HttpServletRequest req,HttpServletResponse res)

    This is how you can override the service in case you want to:

    protected void service(HttpServletRequest req, HttpServletResponse resp) {
    String method = req.getMethod();
    
    if (method.equals(METHOD_GET)) {
            doGet(req, resp);
    } else if (method.equals(METHOD_HEAD)) {
        doHead(req, resp);
    } else if (method.equals(METHOD_POST)) {
        doPost(req, resp);
    
    } else if (method.equals(METHOD_PUT)) {
        doPut(req, resp);   
    
    } else if (method.equals(METHOD_DELETE)) {
        doDelete(req, resp);
    
    } else if (method.equals(METHOD_OPTIONS)) {
        doOptions(req,resp);
    
    } else if (method.equals(METHOD_TRACE)) {
        doTrace(req,resp);
    
    } else {
        resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
    }}
    

    Implementation code given by Tomasz Nurkiewicz from SO community only Overriding Service Method

    0 讨论(0)
  • 2021-01-04 09:20

    If you must respond to GET or POST requests made by a HTTP protocol client (usually a browser) don't hesitate to extend HttpServlet and use its convenience methods. If you must respond to requests made by a client that is not using the HTTP protocol, you must use service()

    0 讨论(0)
  • 2021-01-04 09:28

    Do not override service() method. The preferred approach is using doPost() for post and doGet() for get. Here is an excellent post on what each does. http://www.jguru.com/faq/view.jsp?EID=47730

    If you must respond to requests made by a client that is not using the HTTP protocol, you must use service().

    0 讨论(0)
  • 2021-01-04 09:28

    You most probably override the doXXX() method where XXX stands for the HTTP Methods like GET, POST, and so on. service() method invoked by the container will decide which of the doXXX() to be called.

    0 讨论(0)
  • 2021-01-04 09:34

    I think you need to understand the flow in order to decide for yourself. The default implementation of service() for an HttpServlet simply calls the appropriate handler for the request method (GET, POST, whatever).

    You need to override service() when you want the same method to handle all incoming methods (no matter if it's a GET, PUT or POST request, you'll answer the same to all). If you're happy with treating each method separately, go with the default service() implementation and override the specific handlers.

    0 讨论(0)
提交回复
热议问题