aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doGet and doPost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doGet and doPost" Watch "doGet and doPost" New topic
Author

doGet and doPost

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 25, 2002
Posts: 662
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

Joined: Nov 22, 2008
Posts: 18944
I tried protected and still works fine. ???
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
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

Joined: Feb 26, 2003
Posts: 41
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.


No Gates!<p>SCPJ 1.4<br />SCWCD 1.4
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
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

Joined: Nov 22, 2008
Posts: 18944
I am using websphere studio 5.0
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
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

Joined: Nov 07, 2002
Posts: 298
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

Joined: Mar 25, 2002
Posts: 662
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doGet and doPost