Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

service method in HttpServlet

 
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 ..
 
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().
 
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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic