File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes protected Method service??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "protected Method service???" Watch "protected Method service???" New topic
Author

protected Method service???

Vilas Lawande
Ranch Hand

Joined: Nov 07, 2006
Posts: 127
service Method of GenericServlet is public .
& Service Method of HttpServlet is protected.
why???
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9618
    
    2

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!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
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

Joined: Nov 07, 2006
Posts: 127
I do not got why method is made protected insteadof public???
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
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

Joined: Nov 07, 2006
Posts: 127
Thanks a lot for valuable reply
Anutosh Swamy
Ranch Hand

Joined: Sep 20, 2007
Posts: 30

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


K. Anutosh<br /> <br />SCJP 1.4<br />SCWCD 1.4<br />OCA (Paper I)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected Method service???