GeeCON Prague 2014*
The moose likes Servlets and the fly likes doXXX methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "doXXX methods" Watch "doXXX methods" New topic
Author

doXXX methods

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I always thought HttpServlet's doXXX methods are public, but Wrox's book says those methods are protected. The link to Sun's servlet package API package is dead. Can someone please help me understand why is it "protected"?
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
I am surprised that no one knows this. What do we override all the time? Please help.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

well, by reasoning...

if they were private, you couldn't use or override them.

and typical practice is NOT to make things public if you don't need to.
protected provides the best level of access in this case. We don't want other classes that are totally unrelated to be able to invoke a servlet method like doPost, but we want our subclasses to be able to use or in our case with servlets, override it.

In the case of a web-based servlet, perhaps there are also security considerations as well. (wild speculation)
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, Mike. I just thought doGet() etc are public methods because in many code examples they are. Now, suddenly I see them as protected, I am confused. Do public void doGet() and protected void doGet() coexist? If so, what are their relationships in terms of the lifecycle?
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, Mike. I just thought doGet() etc are public methods because in many code examples they are. Now, suddenly I see them as protected, I am confused. Do public void doGet() and protected void doGet() coexist? If so, what are their relationships in terms of the lifecycle?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

you are confusing class HttpServlet, which is the 'base' class for every servlet that anyone writes.

When you write a servlet, you extend this class.

In the HttpServlet, the doXXX methods are all protected. When you extend the class to make a servlet, you must make the doXXX methods that you implement (usually doGet and doPost) public. That way the servlet runner can actually use those methods.

The methods do not co-exist. You are overriding them in your servlet class. Here is a very simple example, that doesn't involve servlets. You should get 25 for both. the protected methods of the super class are overriden.

Your last question is confusing to me... access modifiers aren't involved with lifecycle at all.

Hopefully that clears things up.
Laurent Lebrun
Greenhorn

Joined: May 15, 2001
Posts: 2
These methods in javax.servlet.http.HttpServlet are protected.
However, when you override a method, you can make it more accessible but NOT less. For example if the method of your base class is protected, you can override it by a public method but not by a private method. So writing these methods as public when subclassing HttpServlet is legal. But as Mark already pointed out, there is no reason why it should be public.

Laurent Lebrun
 
 
subject: doXXX methods