• Post Reply Bookmark Topic Watch Topic
  • New Topic

illegal state exception, forward/redirect

 
Jesus Angeles
Ranch Hand
Posts: 2069
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to confirm with you guys,

When a RequestDispatcher / forward is done, and if there are codes after the forward command, the codes will get executed? And if those codes have another forward, then we get an IllegalStateException?

example:



On sendRedirect, is it the same issue?

[ November 04, 2006: Message edited by: Jesus Angeles ]
[ November 04, 2006: Message edited by: Jesus Angeles ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,
Always be sure to either branch your code in such a way that the forwards/redirects happen last OR.. put a return statement just after the line that redirects/forwards.
 
Jesus Angeles
Ranch Hand
Posts: 2069
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
[ November 04, 2006: Message edited by: Jesus Angeles ]
 
Jesus Angeles
Ranch Hand
Posts: 2069
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if it is a jsp that has this forward/redirect, is it still safe to use the 'return;'? (assuming the forward/redirect is on the first level - that is not inside any user-defined method in the jsp) (as final class code is generated by container)

example, jsp:


(setting aside good design practices)
[ November 04, 2006: Message edited by: Jesus Angeles ]
 
Jesus Angeles
Ranch Hand
Posts: 2069
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the jsp specs seem to not limit the container-implementation from how it 'calls' the jsp content (either directly on the _jspService, or the container puts it in another method), then using 'return;' may lessen portability.

public void _jspService(<ServletRequestSubtype>request,<ServletResponseSubtype> response)
throws ServletException, IOException {}
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way Servlet programming is often taught, an assumption that gets made is that a forward represents the terminiation of execution of a Servlet or JSP. A forward is usualy the last thing you do in a Servlet, when you're learning anyways, so people are often perplexed when you continue to do state management tasks following a forward.

The problem that often arises, is people don't terminate their methods with a return, or proper logic, and when Servlet code is updated, a forward can potentially be used twice. Since the outputstream is closed when a forward is executed, a second forward will trigger the illegal state esception

This is a largely American problem. In Canada, we have IllegalProvinceExceptions, as opposed to state exceptions.

Cheers!

-Cameron McKenzie
[ November 04, 2006: Message edited by: Bear Bibeault ]
 
Jesus Angeles
Ranch Hand
Posts: 2069
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, Im just trying to limit the change to the current code, so as not to need to retest the whole thing.

I put 'return;' on the servlet to fix it as I can clearly see that the method returns void. But in the jsp I think it is vendor-dependent (I dont know if 100% of vendors may put the jsp content in the _jspService, but the specs doesnt stop them from doing something else).
[ November 04, 2006: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!