M-D
M-D

Reputation: 10397

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.

Upvotes: 16

Views: 15617

Answers (2)

JB Nizet
JB Nizet

Reputation: 691953

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.

Upvotes: 9

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340883

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(.....);

}

Upvotes: 24

Related Questions