HttpServlet class extends GenericServlet class.the GenericServlet can handle all type request like ftp,smtp all.but HttpServlet can handle only http request why can not it handle all type request?
A GenericServlet class contain an empty method-
public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
When anybody servlet class extended with it .it can necessary to override it.
So this method is not specifies for a particular type protocol request.
So it handle all type request .
But when the HttpServlet class is extended. it contain two service method using overloading.
First is superclass method
Public void service(HttpServletRequest,HttpServletResponse);
And second on method protected void service(ServletRequest,ServletResponse);
It define in library classes as:-
-------------------------------------------------------------------------------------------------------------
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
doGet(req, resp);
} else
{
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince < (lastModified / 1000L) * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else
{
resp.setStatus(304);
}
}
} else
if(method.equals("HEAD"))
{
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else
if(method.equals("POST"))
{
doPost(req, resp);
} else
if(method.equals("PUT"))
{
doPut(req, resp);
} else
if(method.equals("DELETE"))
{
doDelete(req, resp);
} else
if(method.equals("OPTIONS"))
{
doOptions(req, resp);
} else
if(method.equals("TRACE"))
{
doTrace(req, resp);
} else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object errArgs[] = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
---------------------------------------------------------------------------
The own class method check the only http get,post,head and some other http requests and executed only its . the time of overriding the super class method it cast the ServletRequest reference
Into HttpServletRequest reference , and servletResponse to HttpServletResponse
. in library it define as:-
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{ HttpServletRequest request;
HttpServletResponse response;
try
{
request = (HttpServletRequest)req;
response = (HttpServletResponse)res;
}
catch(ClassCastException e)
{
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
-------------------------------
it indirectly call own method by passing as arguments in these Own method . so if the http request is not matched then it ignore them .So it unable to handle other type request while the GenericServlet can handle all request.
there not necessary to override service method in HttpServlet class which extend it. For Further Reading,
0 comments:
Post a Comment