Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question: why wrong super.service( (ServletRequest) req, (ServletResponse) res)

 
Majid Al-Fifi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Majid Al-Fifi:
Hi all,

I have the servlet:

1. public class TestServlet extends HttpServlet
2. {
3. public void service(HttpServletRequest req, HttpServletResponse res)
4. {
5. service(req, res);
6. }
7. }




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 ]
 
Majid Al-Fifi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


but if I change line 5 with:
super.service((ServletRequest)req, (ServletResponse)res);


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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected.

Nice post, ramprasad, Majid.
 
Majid Al-Fifi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you All..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic