• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in enthuwere mocktest question

 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c Question 8 of 60
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.)
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
}
}

select any two.
a.For any HTTP request, at the most 2 of it's methods will be called.
b For any and every request, atleast 1 of it's methods will be called.
c.For any and every request, init() will be called.
d init() may be called more than once for an instance of this servlet.
e It'll throw an exception at runtime if an HTTP DELETE request is sent to it.

answer is a,e

but why b is wrong as for anyrequest any of the two methods(doget or dopost will be invoked)?




 
Francois Nadeau
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Geeta,

I didn't write the question, but I think b's "for any and every request" is the key. A doPut (HTTP PUT) or doDelete (HTTP DELETE) request can be sent to the servlet, in which case none of the methods would be called.

Cheers.
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Geeta,
Since the question says "preloaded or preinitialized",
1. if the servlet is not loaded, the server will load the servlet and call its init() method.After initialization, the service method will invoke the appropriate doGET/doPOST method depending on the request i.e if the request is for GET or POST.2 method calls

2. if the servlet is loaded but not initialized, its init() method will be called and then the service method will invoke the appropriate method depending on the request i.e if the request is for GET or POST. 2 method calls

So option B is not correct because the first time init() and doGET/doPOST will be invoked and subsequent requests will invoke only doGET/doPOST.

Hope that helps. Guys correct me if I'm wrong.

Kind Regards.

Hasnain Javed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic