Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doGet and doPost

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doGet and doPost in HttpServlet class is protected void but when we use them in our class extending HttpServlet we make it public void doget and public void doPost? Why is this so?
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question. I used to have this doubt when knowing about servlets. Let me give it a try.
I think the reason for this is that one is always expected to(have to) extend the HTTPServlet class and implement the doGet/doPost methods. The container will execute the service() method in the parent HTTPServlet class and the overridden doGet/doPost methods get executed in the customized child classes of our own, by polymorphism. Obviously we cannot call a protected method on an object instance and the same will the case of the container managing the servlet instance. I think i convinced myself here
Guys please correct me for any mistakes in the interpretation.
Thanks.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried protected and still works fine. ???
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you deploy your servlet in tomcat or any other container and made sure that it serves the HTTP requests? If so, some body should come forward and explain the behaviour. I'll get on your side to listen to the explanation :-)
 
Ivan Matmati
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you redefine a method you can always make it more accessible but not less!
There, you can make your protected methods public, they'll still work.But if you make it private, your compilation will fail.
Good luck.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ivan Matmati:
If you redefine a method you can always make it more accessible but not less!
There, you can make your protected methods public, they'll still work.But if you make it private, your compilation will fail.
Good luck.

You are absolutely right. Everything will go fine as per compilation as its like compiling a simple java class. But if the method is protected, will the client be able to call it on the instance created i.e., our servlet container calling on the servlet instance. That is the reason why i asked shan to make sure that the servlet serves the request from tomcat or any other container.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using websphere studio 5.0
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you put your servlet with protected doGet/doPost into service and send it a request using the client browser or whatever?
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
It doesn't matter the access level of doGet, doPost,doPut...
A servlet life cycle consists of calling:
init(ServletConfig conf) while in initialization
service(ServletRequest req,ServletResponse resp) while servicing
destroy() while under destruction...
doGet method will be called by method service(HttpServletRequest req,HttpServletResponse resp) which will be called by service(ServletRequest req, ServletResponse resp). Notice that if the method
service(ServletRequest req,ServletResponse resp) has public access level, everything will work!!
Hope it helps!
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I forgot the fact that the servlet container calls only the service method and rest everything is delegated internally by the service method. Something learnt for me for the day.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic