This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

service method in HttpServlet

Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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 ..
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic