Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another mock exam question

 
Rasika Chitnis
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a question in one of the jWebPlus mock exam about how many methods in the user defined servlet that extends from HttpServlet get called for an http request. The example code had a servlet with over-ridden no args init method, doGet method and doPost method.
My answer was :
for any and every request, at least one of it's method will be called.
The correct answer is :
for any http request at the most two of it's methods will be called.
If service methods (two of them in HttpServlet) are counted then it would be three. If service methods are not counted it would be just one i.e. one of the doXXX methods. How would it be at the most two ??
thanks in advance.
 
Bhushan Jawle
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the init had no args, then it will never be called by container for initialization of servlet. So at the most service and one of doXXX will be called , if doXXX is not overidden, only service will be called.
 
Rasika Chitnis
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it is correct to say that no-args init method will never be called. GenericServlet's with args init method always calls no-args init method.
I guess what got me wrong on this question is the fact that I was considering HttpServlet's methods that are inherited by user defined concrete servlet class. What they meant here was how many of over-ridden methods will be called, not counting the methods inherited from HttpServlet.
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from Bhushan if doXXX is not overidden, only service will be called

I already read in this forum that even if we don't ovveride the doXXX method than the default doXXX will be called.
First, when the user makes a request the service(ServletRequest req,ServletResponse res) is called and after as it is an HTTP request, the req is casted to an HttpServletRequest.
Then even if we don't override, we are calling 3 methods, isnt'it ? and for init() method, it may be it has already been called when the servers was started. It's not sure that we are calling the init() method when we do the request.
I'm wrong ? let me know .
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Engin Okucu,
Yes. You are right. Even if we do not override the do*** method in our user defined Servlet, when we make a request to that servlet,still the do*** method of HttpServlet will be called. The default implementations of do** methods in HttpServlet is to call a sendError statement.
This is the reason, if we write a servlet which just extends HttpServlet but does not override any do*** methods we will see an error page saying "Method not Implemented" on the browser.
Regards,
Maha Anna
 
Rasika Chitnis
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder why HttpServlet class is made abstract without any abstract method in it. I don't find in the javadoc of servlet 2.3 that doXXX methods in HttpServlet class send error to the client. If it is so, then why not declare them abstract like GenericServlet's service method ?
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's kind of tricky implementation I guess. In Java we can declare a class as abstract for 2 reasons.
1. If the class has any unimplemented abstract
methods.
2. If you do not want anyone to instantiate your class and use it. The reason behind this could be anything. In our case, the reason is we must extend the HttpServlet class and then use it. Because the mostly used doGet(..) doPost(..) methods in HttpServlet class have dummy implementation which is useless and the authors want us to extend the class and take care of it.
If you see the source code of HttpServlet class you can see the default implementation is to call sendError() with a message of "Method Not implemented".
Instead of doing this way, the author could have very well made doGet,doPost,doDelete,doHead methods as abstract. But if they had done this way, then we MUST implement all these methods in our servlet eventhough all we care about is either doGet/doPost. But now all we do is override doGet / doPost or both only.
Probably this could be the reason which convinced the author to give the dummy implementations for the do** methods in HttpServlet.
Just a thought
Regards,
Maha Anna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic