• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why do we need to override either doPost() or doGet()???

 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,
I have just started Head First Servlet & JSP . In this book it is mentioned that we must override doPost() or doGet() method in Servlet class.

When i explore the API for HttpServlet ,i found not a single method is Abstract(i.e without body or implementation). My question is if those methods are not abstract then why their is need of implementing them in Servlet class i.e override them??? if we don't override them the methods in HttpServlet Class should be just called...

Please correct me if i m wrong.


With thanks in advance,
Regards,

Prabhat
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prabhat,

of course you don't have to override theses methods but then your web application will be quite useless

If you plan to handle user requests (which is what you usually want when you write an application) then you simply have to override all of the methods for the corresponding HTTP methods you want to handle (GET, POST, HEAD, ...). If you don't override them your application will do nothing! The methods like doGet(), doPost() and so on is where you have to place the logic for your application. At least this is the starting point to process user requests and to generate corresponding responses for the user.

Or did you really think you don't have to write any code and your application knows what to do itself?

Marco
[ April 02, 2008: Message edited by: Marco Ehrentreich ]
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marco!!
Now ,I have got it.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we don't override them the methods in HttpServlet Class should be just called.

That's correct, and they will be called. But if you read the javadocs for that class and those methods, you'll see that they throw exceptions, which is not what you want in a production environment.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... But if you read the javadocs for that class and those methods, you'll see that they throw exceptions, which is not what you want in a production environment.


Just for interest: Is it really in the JavaDocs? To tell the truth I thought the methods would simply do nothing if you don't override them. And I wondered why there's no hint in the API doc about what the default implementation does. Of course the methods can throw a ServletException but the documentation doesn't state that an exception will always be thrown by default. At least I couldn't find a clue in the JavaDocs...
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for interest: Is it really in the JavaDocs?


I stand corrected - it's not in there any more. But it used to be; see the "Method Index" here.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see. But then I think it wasn't really the best idea to remove this information from the JavaDocs. Just from this documentation everybody new to the servlet specification will be confused where to start... I don't think this was the intention of the authors. Perhaps it was a mistake.

But thanks for clarification, Ulf.

Marco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic