This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Spark in Action and have Jean-Georges Perrin on-line!
See this thread for details.
Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

protected void service() method

 
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
 
Author and all-around good cowpoke
Posts: 13078
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
 
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
Posts: 13078
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
 
(instanceof Sidekick)
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.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic