File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes service method in HttpServlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "service method in HttpServlet" Watch "service method in HttpServlet" New topic

service method in HttpServlet

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
There are two service methods in HttpServlet abstrtact class (Service method is overloaded and overridden)
1.public method
2.protected method
service in class GenericServlet
public void service(ServletRequest req,ServletResponse res) throws ServletException,
Dispatches client requests to the protected service method. There's no need to override this method.
Overloads :
protected void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,
Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class. This method is an HTTP-specific version of the Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse) method.

Why deos it require two service methods in HttpServlet Class? As HttpServlet class is meant for HTTP protocol,then why does that specific class has another service mehod with parameters ServletRequest and ServletResponse ?
Understanding the sequence of events in HttpServlet
1.The servlet container calls the service(ServletRequest req,ServletResponse res) method f HttpServlet
2.The service(ServletRequest,ServletResponse) method of HttpServlet calls the service(HttopServletRequest ,HttpServletResponse ) method of the same class.
3.The service(HttopServletRequest ,HttpServletResponse ) method of HttpServlet analyzes the request and finds out which HTTP method is being used .

As per the above quote ,Container calls service method(ServletRequest,ServletResponse) and this method in turns calls service(HttopServletRequest ,HttpServletResponse ) method.How does the service method calls another service method in same class?In service method definition ,it is not calling another service method ..
Mahesh swami

Joined: Aug 13, 2002
Posts: 26
See ,
HttpServlet class needs to override teh service() method in the GenericServlet class or else it would be a Abstract class.
Now it requires that the arguments to this method should be objects of type HttpServletRequest and HttpServletResponse. Hence it wraps the ServletRequest and ServletResponse type objects into required type and then calls the protected service()method. Which in turn checks for the Http method(Get/Post) and call doXXX method().
Eric Lidell

Joined: Jul 02, 2003
Posts: 20
The service (ServletRequest request ,ServletResponse response) is used to override the genericservlet.and it transfers the code to service (HttpServletRequest,HttpServletResponse ) by probably inserting the following code.
service((HttpServletRequest) request,(HttpServletResponse)response)
I agree. Here's the link:
subject: service method in HttpServlet
It's not a secret anymore!