This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes why HttpServlet is abstract? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "why HttpServlet is abstract?" Watch "why HttpServlet is abstract?" New topic
Author

why HttpServlet is abstract?

yuga reddy
Greenhorn

Joined: Jul 22, 2008
Posts: 2
please help me.
Even though there are no abstract methods in it Why HttpServlet is abstract?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Do you mean what makes it abstract or why was it defined that way?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
yuga reddy
Greenhorn

Joined: Jul 22, 2008
Posts: 2
I mean why it is abstract?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What should it do? HttpServlets are designed to respond to specific HTTP requests. I suppose the designers of this API could have added do-nothing methods to HttpServlet and not made it abstract, but I can't think of a good reason for doing this.
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
So programmers are forced to extend it.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

And what about the doGet() and doPost() methods?

If the class were concrete, what would the default implementation do?


SCJP, SCWCD.
|Asking Good Questions|
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Originally posted by Amit Ghorpade:
And what about the doGet() and doPost() methods?

If the class were concrete, what would the default implementation do?
They are not abstract methods (the programmer does not have to override either of them). Therefore there must actually be a default implementation.

And in my observation, what the default implementation does is, for example: the doGet() method returns an HTML return code saying that the URL does not support the GET method.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

I did not meant to say that the doGet() and doPost() methods are abstract,I meant one needs a override to make his own implementation.

And in my observation, what the default implementation does is, for example: the doGet() method returns an HTML return code saying that the URL does not support the GET method.

I think that the GET method not supported error is returned by the container.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
I think that the GET method not supported error is returned by the container.

We could discuss whether any result code is returned by the servlet or the container, but in this case the error code is returned by the methods in the HttpServlet class, as Paul said.

The javadocs of earlier version of the HttpServlet class used to state explicitly that the default implementation returns an error code about the method not being supported, but someone chose to remove that rather useful comment.


Ping & DNS - my free Android networking tools app
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Thanks for the explanation Ulf
Shikhar Madhok
Ranch Hand

Joined: Dec 18, 2006
Posts: 95
Originally posted by Paul Sturrock:
I suppose the designers of this API could have added do-nothing methods to HttpServlet and not made it abstract,


Not sure what that meant. Would you please elaborate?

I think the question probably came from the fact that there are no abstract methods in the HttpServlet class. So we could do without the abstract modifier. Couldn't we?

Shikhar


He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever - Chinese proverb
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
So we could do without the abstract modifier. Couldn't we?


It keeps new programmers from wasting time with questions like this one by giving immediate feedback from the compiler.

Bill
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Since there would be no point in instanciating and running an instance of HttpServlet, it makes perfect sense for it to be abstract.

Also, since a subclass of HttpServlet can be useful with zero, one, or up to all of the doXxx methods implemented, it makes no sense to force developers to implement any of these methods by declaring them abstract.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why HttpServlet is abstract?