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.
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.
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.
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.
No you don't. You can't call a method that doesn't exist.
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(.....);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With