Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why HttpServlet is abstract?

 
yuga reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me.
Even though there are no abstract methods in it Why HttpServlet is abstract?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean what makes it abstract or why was it defined that way?
 
yuga reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean why it is abstract?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So programmers are forced to extend it.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about the doGet() and doPost() methods?

If the class were concrete, what would the default implementation do?
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation Ulf
 
Shikhar Madhok
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic