Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

service method in HttpServlet

 
thomas davis
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two service methods in HttpServlet abstrtact class (Service method is overloaded and overridden)
1.public method
2.protected method
Overrides:
service in class GenericServlet
public void service(ServletRequest req,ServletResponse res) throws ServletException,java.io.IOException
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,java.io.IOException
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic