Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Question ID :995549757220

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
1) For any HTTP request, at the most 2 of its methods will be called
2) For any and every request, atleast 1 of its methods will be called
3) for any and every request init() will be called
4) init() may be called more than once for an instance of this servlet
5) it'll throw an exception at runtime if an HTTP DELETE request is sent to it.

What would be the answer ?
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here service method is not overriden. So if I think only the overriden methods are considered, then only answer b is right. You see, for the first request, init, service and another method doGet/doPost... will be called. Thats why (a) is wrong.
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any difference between " every request" & "HTTP request" ?? in a
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question....I guess no....
Any one on this isesue ?
 
Enthuware Software Support
Posts: 4850
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Paul:
(Statements given below are regarding the methods defined in this class.)
1) For any HTTP request, at the most 2 of its methods will be called
2) For any and every request, atleast 1 of its methods will be called
3) for any and every request init() will be called
4) init() may be called more than once for an instance of this servlet
5) it'll throw an exception at runtime if an HTTP DELETE request is sent to it.

What would be the answer ?


Option 1 is correct, as is given in the s/w. For the very first request to this servlet (if it is a GET or POST), init() and doGet/doPost will be called.
2 is wrong because it does not implement all the doXXX methods (such as doDelete, doPut, etc), so for such requests, no method of this class will be called.
Options 3, 4, and 5 are obviously wrong.
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
so do u mean to say that HTTP request means only GET & POST ?
Raj Paul
 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 is the only right i belive.
Ash.
SCJP/SCWCD
 
Paul Anilprem
Enthuware Software Support
Posts: 4850
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Paul:
Hi Paul,
so do u mean to say that HTTP request means only GET & POST ?
Raj Paul


No, it means any HTTP request ie. GET, POST, DELETE, PUT, OPTIONS, and HEAD. In such questions, assume that request means HTTP request unless otherwise stated.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Anil:
2 is wrong because it does not implement all the doXXX methods (such as doDelete, doPut, etc), so for such requests, no method of this class will be called.


its clear for me, but..

(Statements given below are regarding the methods defined in this class.)

 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any chance you guys are overlooking the default service() method ? For any request first service() will be invoked. Then depending on the request it will pass the request to get..post..put...to any relevent method. So for the very first request init(), service() and a relevent method will be invoked. Ok...now if u only consider the given code...then I don't have any argument. I dislike such misguideing question.
Am I wrong?
 
no more user
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im with you :roll:
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well if the servlet implements SingleThreadModel then c is also correct .Right?
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
O yes, u r right...but the given class does not implements this...
 
reply
    Bookmark Topic Watch Topic
  • New Topic