• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpServlet provides empty implementation for all do***()?

 
Kyle Tang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Say, your Servlet only has doPost(), but a Get is sent to it, what will happen? Empty output, or Exception with status 405?
From Jwebplus, it says HttpServlet provides emptry implementation for all do***() methods, so there will be no Exceptions. But from the Mockexam on the ranch, it says Exception will be thrown. I tried it on tomcat, status 405 is returned.
What do you think?
Kyle
 
Rajesh Pathak
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever we call a doXXX method of a servlet the container calls service method of HttpServlet which inturn calls the relevant doXXX method.
Since you have not overridden the doGet in your servlet class the default doGet method of HttpServlet is called (which does nothing).
Hence there is no question of throwing an exception.
 
Kyle Tang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rajesh.
But the problem is, I tried it in Tomcat4, and I got a Exception with status code 405 (Http Method not allowed). And what's more, in the mock exam, there is one question with the correct answer saying an Exception should be thrown.
That's what makes me confused.

Kyle
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3820
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kyle,
What you are getting is not actually an exception. The default implemention of the doXXX methods call response.sendError() method to send an error message to the browser. This is not an exception.
The following source code for the doGet() method of HttpServlet will help you understand this better:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic