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.
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 :-)
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.
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.
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!