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.
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.
Joined: Nov 07, 2006
I do not got why method is made protected insteadof public???
Joined: Sep 20, 2007
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.
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