The image underneath describes the servlet architecture.
Servlet Architecture
It says that the HttpServlet class need not implement the service() method as its being taken care by GenericServlet class. The HttpServlet class must only implement the doGet() and doPost() methods.
Doubt
In this diagram hierarchy, where are the signatures of doGet() and doPost() present, ie, how implementation of doGet()/doPost() is triggering a call to service() in GenericServlet class, because even abstract signatures of doGet() and doPost() are absent in GenericServlet class.
Question
I have a belief there is a nested call to these methods in service() method of superclass GenericServlet. Is this correct perception or it works in some other way?
Your suspicion is almost correct, the calls to doGet()
, doPost()
and the other do-methods are in the HttpServlet
class, which extends GenericServlet
. This is taken from version 3.1 of the Servlet API jar (see the source code starting at line 677):
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < lastModified) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
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 {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}