• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet questin

 
Timber Lee
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question ID :995549757220
Following is the code for LoginServlet. Assuming that it is not preloaded or preinitialized, which of the given statements about it are correct?
(Statements given below are regarding the methods defined in this class.)
//[code]
public class LoginServlet extends HttpServlet
{
public void init()
{
//initialize db.
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
//do something
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
//do something
}
}
//[code]
the answer is that at most 2 of its methods are called. I don't understand it.
init,service,and doPost(or doGet) are called
 
Dan Jepp
Author
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Timber,
If you read the question carefully you will see a hint to the correct answer.
It says
(Statements given below are regarding the methods defined in this class.

Therefore as the service() method is defined in the parent class not the servlet itself then the only methods that will be called during any request is init() if it is the first time the servlet has been called, then either doGet(), doPost() etc...
Hope that helps
Dan
 
Barry Kushnir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timber,
Since the servlet was not preloaded or preinitialized, a single request may or may not cause the init() method to be called.
the same request, may or may not cause the doPost() or doGet() method to be called (could be doPut() or doDelete() etc.)
Therefore, a request for LoginServlet may cause zero, one, or at most -- two methods to be called.
-BK
[ October 18, 2002: Message edited by: Barry Kushnir ]
 
Bill Wang
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Dan and Barry. But this question in general is not a very good one since it has some holes in it. Consider this situation,
public TestServlet extends HttpServlet
{
public init(){//do Something}
public void doGet(HttpServletRequest req, HttpServletResponse res){
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res){
//do Something
}
}
In this case, if the servlet is not proloaded, when the request is a GET request, init(), doGet(), and doPost() will be called. Therefore, three methods will be invoked. Again, this is my opinion, I don't know what anyone else thinks
 
Sam Dalton
Author
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Wang:
I agree with Dan and Barry. But this question in general is not a very good one since it has some holes in it. Consider this situation,
public TestServlet extends HttpServlet
{
public init(){//do Something}
public void doGet(HttpServletRequest req, HttpServletResponse res){
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res){
//do Something
}
}
In this case, if the servlet is not proloaded, when the request is a GET request, init(), doGet(), and doPost() will be called. Therefore, three methods will be invoked. Again, this is my opinion, I don't know what anyone else thinks

Bill,
Whilst this is indeed true, it is not what the question asks. The question wants to know which methods are invoked by the container.
Cheers
Sam
 
Barry Kushnir
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam,
While it is probably true that the question is looking for the methods invoked by the container, the question should explicitly state that. Otherwise the question becomes a little ambiguous.
-BK
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic