Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many methods are called???

 
zena sam
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question fron JWebplus....
Following is the code for TestServlet. Assuming that it is not preloaded or preinitialized, which of the given statements about it are correct?
//[code]
public class TestServlet extends HttpServlet
{
public void init()
{
}
public void service(HttpServletRequest req, HttpServletResponse res)
{
super.service();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
//do something
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
//do something
}
}
//[code]
the options available are
1.For any and every HTTP request at the most 2 of it's methods will be called
2.For any and every request atleast one of it's methods will be called
3.For any and every request ,service() will be called.
4.If it is sent as HTTP PUT request,none of it's methods will be called
5.If it is sent as HTTP PUT request,it'll throw an exception
the correct answers given are 2 and 3...
can any one explain this...as I feel 1 and 3 as the right one's....
Also if service method is not overidden does the answer vary....

Thanks!!
zena
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zena
1 is not correct because the questions states that it is not preloaded or initialized, so on the first call to it the servlets init method will run, along with the service and possible one of the doXXX methods.
2 is correct because the service method will always be run.
3 is correct.
4 is false because the service method will be called.
5 is false there is no reason for it to throw an exception.
Keep in mind too that the service method in the code is overloading the public method it gets from its HttpServlet. The public method in HttpServlet takes a ServletResponse and a ServletRequest as its parameters not HttpServletResponse and HttpServletRequest. All the public method does is dispatch the request to the protected method after converting the request and response objects to Http objects. So, in reality, the method in the code posted will never be called, but the method it overloads will be, along with the protected method the servlet will inherit from HttpServlet.
hope that helps
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In HttpServlet class, i dont think there is public service method. More than that, what is that super.service(), which method will it call,that program is not getting compiles properly
Please give more light on this
Thanks & Regards
Mike
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody...
ha HttpServlet have public service method....but this code is not getting compiled properly
what could be the mistake
Thanks & Regards
Mike
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code posted wont compile because the call to super.service does not pass any arguments. You would need to change it to:
super.service(req, res);
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super.service(); should actualy be super.service(req, res); But that will not affect the answers, though.
 
John Williamer
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Mike Kumar, HttpServlet methods are protected but can be overiden as both public and protected. Just a small point.
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Dave Vick's first post,
Keep in mind too that the service method in the code is overloading the public method it gets from its HttpServlet.
This is not true. The code in this question DOES NOT OVERLOAD any method. It simply OVERRIDES it's base class HttpServlet method "protected service(HttpServletRequest req, HttpServletResponse res)" method.
Try this code to clear this Java concept.

If you compile both classes and run java Sub, you will get this printed.

Regards,
Maha Anna
[ December 03, 2002: Message edited by: Maha Annadurai ]
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware, there are two service methods defined in HttpServlet abstract class.
protected void service(HttpServletRequest res, HttpServletResponse res) dispatches HTTP request to doXXX methods.
void service(ServletResquest req, ServletResponse res). When invoked by servlet container, it forwards the request to the protected service method.
Basically, there is no need to override any of these methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic