Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected void service() method

 
Srinivasa Raghavan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks much.
Srini
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Srinivasa Raghavan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?

Thanks much.
Srini
 
Atul Prabhu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

How could I possibly be any clearer?
Bill
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic