Reputation: 29
In servlet program we are calling the doGet()
or doPost()
method but in the servlet life cycle says that all requests will be redirected to service()
method. But in the HTTP servlet we are not writing a service()
method in our program. Then how is it calling service()
? Please explain briefly.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
.......// It is working fine but how internally it is calling service()
}
Upvotes: 2
Views: 5733
Reputation: 279970
but how internally it is calling service()
It doesn't. The Javadoc of HttpServlet
states
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. 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
- doDelete, for HTTP DELETE requests
- init and destroy, to manage resources that are held for the life of the servlet getServletInfo, which the servlet uses to provide information about itself
There's almost no reason to override the
service
method.service
handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).
Your custom sub type of HttpServlet
inherits the service()
method. When the Servlet container decides that your Servlet should be used, it invokes that inherited method which is implemented like so
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);
}
}
Through inheritance, if you've overriden any of the methods above, your implementation will be used.
Upvotes: 4
Reputation: 8009
Any service method have not overridden will be delighted to the service method of super class implementation is called by the servlet container.
Upvotes: 0
Reputation: 23637
The service() method detects the HTTP method used and delegates to doGet(), doPost() and other methods which process HTTP requests in a HTTPServlet. It also encapsulates the ServletRequest and ServletResponse objects in HttpServletRequest and HttpServletResponse objects which contain additional context data from the HTTP headers.
You never really call doGet() or doPost() (the service() method will, and it is called by the Web container as you read in the lifecycle). You should also not override the service() method if you are writing an HTTPServlet.
Upvotes: 3