The HTTPServlet Class defines a protected void service(HttpRequest req, HttpResponse resp). Any specific reasons as to why method needs to be protected? If I extend this class in MyServlet class and override it (though the spec says that we need not override it) and make it public, then any class can use this method? Any explanation for this will be quite helfpul.
If you want to override service, you would override the more general version in GenericServlet, the one with the signature using ServletRequest and ServletResponse. The HttpServlet class exists for convenience - it lets you write doGet and doPost methods without worrying about the other HTTP request types such as PUT and HEAD. Bill
Joined: Aug 18, 2004
Thanks for your reply. However, if I were to still override the protected service() method and take care of invoking the appropriate doXXX() methods, then I will be acheiving the same functionality provided by HttpServlet Class (though this is redundant and soundz wierd). The concern here is, while overriding the service() method, if I change the access modifier from "protected" to "public", then what will be the implications of such a modification? Also, what is the specific reason that this service() method in HttpServlet is protected as against a public or a default access?
The reason is straight forward .. Java fundamentals
If the access specifier is protected that means it can only be accessed within the package or any class that extends the current class outside the package.
If it is public then it can be accessed by all.
Apply the above funda to your doubts, everything would be clear.
--- A T U L
Author and all-around good cowpoke
Joined: Mar 22, 2000
Also, what is the specific reason that this service() method in HttpServlet is protected as against a public or a default access?
Because the designers of the API expected that anybody who wanted to create their own service routine would extend GenericServlet - there is absolutely NO REASON to try to extend service in HttpServlet when GenericServlet is sitting right there.
Servlet has a public constructor and in theory any object could create an instance of a servlet and start calling methods. In practice, the container creates one instance of each servlet class and only the container ever calls the methods. So there would be no value to any other class if you expanded the access to public for these methods.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi