• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

protected Method service???

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
service Method of GenericServlet is public .
& Service Method of HttpServlet is protected.
why???
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's public on both classes. That way, the container can call it.

HttpServlet api

The HttpServlet, however, has an additional overloaded version of service that is protected and accepts the Http versions of the request and response parameters. HttpServlet's public service method by default turns around and calls the protected version.

But why is the overloaded version protected? Because the container doesn't need to call it. It's all kept internal. The method could have been made private but they wanted to allow crazy people the opportunity to override it in subclasses.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Mark Peabody,

But why is the overloaded version protected? Because the container doesn't need to call it.



I'm not able to get it. Can you elaborate on this further. Thanks in advance!
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Originally posted by Mark Peabody,



I'm not able to get it. Can you elaborate on this further. Thanks in advance!



The inherited service() method ( the public one that takes ServletRequest and ServletResponse as arguments ) is called by the container. This then delegates the work to the overloaded service method ( the protected one with HttpServlet Request and HttpServletResponse ). This overloaded method has a default implementation that decides which doXXX() method gets called for servicing the request.

However, since it is protected, you can override it in your servlet and do whatever it is that you want to do; you'll lose the default functionality but that's probably what you want in the first place.
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not got why method is made protected insteadof public???
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vilas Lawande:
I do not got why method is made protected insteadof public???



Do you understand the implications of public and protected modifiers? Protected means that it is accessible only to subclasses anywhere and to other classes in the same package. If you want to stop other people from calling a method but you would like to allow subclasses to override it, you will pick protected.

Public means that the member is accessible to everyone. You would want to make methods called by the container public.

And as mentioned earlier, the service() method is overloaded, the public one has the signature of the method from GenericServlet. But the one you, as the developer could use and modify to suit your needs is the protected one.

Basically, the container will always call the public method. The implementation of this method in HttpServlet is such that it delegates to the protected method. The protected method may have been overridden in your servlet but if not, it also has a default implementation that will call doXXX() methods.
 
Vilas Lawande
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for valuable reply
 
Ranch Hand
Posts: 30
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
service() method of GenericServlet class :: The GenericServlet class of the javax.servlet package is an abstract class and it has an abstract service() method which is also public with ServletRequest and ServletResponse objects as its parameters.

So the concrete class extending the GenericServlet class must implement the service(ServletRequest,ServletResponse) method and we can also [I}override[/I] this method in the subclass.

service method of HttpServlet class :: The HttpServlet class of the javax.servlet.http package is also an abstract class having one protected service() method (not abstract) with HttpServletRequest and HttpServletResponse objects as parameter and one public service method() with ServletRequest and ServletResponse objects of type GenericServlet as HttpServlet is a subclass of GenericServlet.

The protected void service(HttpServletRequest,HttpServletResponse) method receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class.

The public void service(ServletRequest,ServletResponse) method dispatches client requests to the protected service method.

We need not have to override the service() method of the HttpServlet class as service() method handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods).

But a subclass of HttpServlet must override at least one method, usually one of these:

doGet, if the servlet supports HTTP GET requests
doPost, for HTTP POST requests
doPut, for HTTP PUT requests
doDelete, for HTTP DELETE requests
init and destroy, to manage resources that are held for the life of the servlet
getServletInfo, which the servlet uses to provide information about itself

---------------------------------------------------------------------------


K. Anutosh

SCJP 1.4
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic