File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes interview question on doget and dopost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "interview question on doget and dopost" Watch "interview question on doget and dopost" New topic

interview question on doget and dopost

vicky baba

Joined: Mar 04, 2004
Posts: 13
in interview he asked me can we use doget(),dopost() and service method in one servlet?if yes then which method first call?
thanks in advance
Chris Hall
Ranch Hand

Joined: Dec 04, 2002
Posts: 39
The service method is clearly called first, altough this is transparent when using an HttpServlet. The doGet() and doPost() methods are part of the HttpServlet class.
But to answer the question, (I tried it out just now) you can have an HttpServlet with all 3 methods, and the service method is called first. I am not sure when you have the service method in an HttpServlet, how the doGet or doPost is even called.
kapil bhate

Joined: Mar 04, 2004
Posts: 15
thanks chris for reply .........
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Inside the service() method of HttpServlet, it figures out which method to call, based upon the HTTP "method" property, and dispatches to the appropriate doXXX() method. If you override the service() method without calling the superclass' implementation, your servlet will NEVER call the doXXX() method, unless you specifically call it yourself. So, be careful. In general, though, you CAN override multiple doXXX() methods. One reason would be to have the doGet() method render a screen and the doPost() method handle the posting of information FROM that screen and dispatch to another servlet. That way, all of a screen's logic is in one class.

James Carman, President<br />Carman Consulting, Inc.
I agree. Here's the link:
subject: interview question on doget and dopost
It's not a secret anymore!