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
Joined: May 10, 2001
Posts: 3244
posted
0
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
Dave
Raj Paul
Ranch Hand
Joined: Jul 09, 2002
Posts: 77
posted
0
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
Joined: Jul 09, 2002
Posts: 77
posted
0
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
Joined: May 10, 2001
Posts: 3244
posted
0
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
Joined: Sep 23, 2000
Posts: 2912
posted
0
super.service(); should actualy be super.service(req, res); But that will not affect the answers, though.
Actually Mike Kumar, HttpServlet methods are protected but can be overiden as both public and protected. Just a small point.
Maha Annadurai
Ranch Hand
Joined: Oct 27, 2002
Posts: 87
posted
0
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
Joined: Jun 25, 2002
Posts: 104
posted
0
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.