• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

illegal state exception, forward/redirect

 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
[ November 04, 2006: Message edited by: Jesus Angeles ]
 
Jesus Angeles
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 {}
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic