This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

doGet and doPost

 
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?
 
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 :-)
 
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?
 
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.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!