J
jimjim
hello guys,
Curiosity made me take a look at the tomcat 's implementation of the servlet
API.
The HttpServlet class, which can be subclassed by http servlets, contains
the "service" method that is invoked by the servlet container in order to
pass a web request to a particular servlet. The method accepts a
ServletRequest and ServletResponse object as arguments, and downcasts them
to HttpServletRequest and HttpServletResponse respectively and then calls a
"service" method implemented by the same class:
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);
}
}
How is it possible to do so, as the ServletRequest interface is more generic
than the HttpServletRequest (they have a hereditary relationship
HttpServletRequest== extends ServletRequest)?
TIA
Curiosity made me take a look at the tomcat 's implementation of the servlet
API.
The HttpServlet class, which can be subclassed by http servlets, contains
the "service" method that is invoked by the servlet container in order to
pass a web request to a particular servlet. The method accepts a
ServletRequest and ServletResponse object as arguments, and downcasts them
to HttpServletRequest and HttpServletResponse respectively and then calls a
"service" method implemented by the same class:
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);
}
}
How is it possible to do so, as the ServletRequest interface is more generic
than the HttpServletRequest (they have a hereditary relationship
HttpServletRequest== extends ServletRequest)?
TIA