Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected modifier of doGet(), doPost() etc.

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Feeling confused about the access modifier of the doGet(...), doPost(...) and other methods of HttpServlet class.

How come it is protected instead of public?

Please take a look at the following Servlet:


As per my understanding, "protected" modifier of doGet() means that a client has to be in the same package (or a child - through inheritance) to access doGet(). So how will the invoking JSP or the Container access it?

Hope my question makes sense.
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nidhi!

That's quite interesting question and you made me to take some time with it :-)

I think that the container calls the Servlet.service(-) method (which is public). It then calls the HttpServlet.service(-) method which is protected and it then call doXxx(-) method.

So, the public access for Servlet.service(-) method is enough for the container to operate on servlet.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro Kowalski wrote:I think that the container calls the Servlet.service(-) method (which is public). It then calls the HttpServlet.service(-) method which is protected and it then call doXxx(-) method.

So, the public access for Servlet.service(-) method is enough for the container to operate on servlet.


Got it! Didn't think of the public service(...) method between the container & the doXXX(...) methods!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic