Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding HttpServlet service method

I have a servlet which looks like :

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws IOException, ServletException {
    doTheJob(request, response);
}//method doGet

public void doPost(HttpServletRequest request, HttpServletResponse response)
              throws IOException, ServletException {
    doTheJob(request, response);
}//method doPost

private void doTheJob(.....) {
    ...........................
}

}

Because of the way my application works, I only need to call doTheJob() both from doGet() and from doPost(). So I think, I better override the method service() of the HttpServlet.

But I would like to know if that will brake anything or will cause any issues.

like image 814
M-D Avatar asked Dec 15 '12 12:12

M-D


People also ask

Can we override service method?

Since the routing takes place in service(), there is no need to generally override service() in an HTTP servlet. Instead, override doGet(), doPost(), and so on, depending on the expected request type. The automatic routing in an HTTP servlet is based simply on a call to request.

Can we override service method in servlet?

Unlike Generic Servlet, the HTTP Servlet doesn't override the service() method. Instead it overrides the doGet() method or doPost() method or both. The doGet() method is used for getting the information from server while the doPost() method is used for sending information to the server.

Which method is override in HTTP servlet?

A subclass of HttpServlet must override at least one method, usually one of these: doGet , if the servlet supports HTTP GET requests. doPost , for HTTP POST requests. doPut , for HTTP PUT requests.

Can we override doGet method in servlet?

No you don't. You can't call a method that doesn't exist.


2 Answers

This is how service() is typically implemented (very simplified):

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);
    }
}

As you can see it barely delegates to doGet() and doPost() depending on HTTP method. So from one hand replacing doGet() and doPost() with service() is fine. On the other hand your servlet will also handle PUT, DELETE, HEAD and other methods while with separate doGet() and doPost() it will return 405 Method not allowed.

That's why I would actually advice separate doGet() and doPost() delegating to your code and let servlet handle other methods. If this is a recurring pattern in your code, consider the following helper servlet:

public class AbstractServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                 throws IOException, ServletException {
        doGetOrPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws IOException, ServletException {
        doGetOrPost(request, response);
    }

    abstract protected void doGetOrPost(.....);

}
like image 70
Tomasz Nurkiewicz Avatar answered Nov 09 '22 02:11

Tomasz Nurkiewicz


You'd better keep it as it is. Overriding the service() method also makes this method answer to PUT, HEAD, DELETE, etc. And it bypasses the work the default service() method does with last-modified headers.

like image 30
JB Nizet Avatar answered Nov 09 '22 01:11

JB Nizet