• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected void service() method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic