| Author |
Question: why wrong super.service( (ServletRequest) req, (ServletResponse) res)
|
Majid Al-Fifi
Ranch Hand
Joined: Aug 22, 2006
Posts: 45
|
|
Hi all, I have the servlet: 1. public class TestServlet extends HttpServlet 2. { 3. public void service(HttpServletRequest req, HttpServletResponse res) 4. { 5. super.service(req, res); 6. } 7. } this is working fine... but if I change line 5 with: super.service((ServletRequest)req, (ServletResponse)res); I get StackOverflowError. Why is that although there is the following method in HttpServlet: public void service(ServletRequest req, ServletResponse res) which will down cast the objects and call the Http-specific service method. any idea.. thanks, Majid [ August 28, 2006: Message edited by: Majid Al-Fifi ]
|
SCJP1.4, SCWCD1.4
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
It's got nothing to do with the casting. If you call a method from itself (recursion) you will create an endless loop that adds method calls to the stack with each iteration until you run out of memory. [ August 29, 2006: Message edited by: Ben Souther ]
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Majid Al-Fifi
Ranch Hand
Joined: Aug 22, 2006
Posts: 45
|
|
Yes. and the reason is that service(ServletRequest req, ServletResponse res) in HttpServlet will call service(HttpServletRequest req, HttpServletResponse res) within the same class but that was already overriden and hence it ends up in a loop of method calls. if req and res were not up casted this won't happen. [ August 28, 2006: Message edited by: Majid Al-Fifi ]
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Again, it's got nothing to do with casting. You changed line 5 from calling a method in the super class to have it call a method of itself (removed the "super." qualifier). If you call any method from itself and don't provide a mechanism to stop the recursion, you will end up with a stack overflow exception.
|
 |
ramprasad madathil
Ranch Hand
Joined: Jan 24, 2005
Posts: 489
|
|
Again, it's got nothing to do with casting.
Ben, It may. In case 1 (the posted code), there is a call from the service() method of the Servlet to the super class (HttpServlet) service() method (the one that accepts HttpServletRequest & HttpServletResponse). This method in the HttpServlet class examines the method of the request and calls the doXXX() methods on the subclass Servlet. (works fine) In case 2
a call to the super class's overloaded service method (the one that accepts ServletRequest & ServletResponse objects) is made. Now the javadoc for this method has this to say
Dispatches client requests to the protected service method. There's no need to override this method.
The protected service method in question is the one that accepts HttpServletRequest and HttpServletResponse and which has been overridden in the sub class Servlet. The subclass's service method gets called. So there..... This had me stumped for a while as my first impressions were consistent with Majid's (casting problem). However I couldnt understand the infinite loop until I looked up the javadoc cheers, ram.
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
I stand corrected. Nice post, ramprasad, Majid.
|
 |
Majid Al-Fifi
Ranch Hand
Joined: Aug 22, 2006
Posts: 45
|
|
|
Thanks to you All..
|
 |
 |
|
|
subject: Question: why wrong super.service( (ServletRequest) req, (ServletResponse) res)
|
|
|