4

I'm trying to include a jsp page from a servlet:

  RequestDispatcher rd = ctx.getRequestDispatcher( jspPage );
  rd.include( req, wrapper );   

But I'm getting the following exception:

java.lang.IllegalStateException: Cannot forward after response has been committed

The problem is with a JSP page that specifies its own default error page through the JSP error tag. The JSP error page can also throw an exception which will trickle up to the application level error page specified in web.xml. So when the jsp page that I am trying to include throws an exception, and the error page throws an exception as well, the include fails.

I have to handle this case gracefully because I am including user-written modules on a page and an erroneous module should display the exception to the user rather than bomb with the IllegalStateException. Any ideas?

Art Peterson
  • 569
  • 2
  • 6
  • 14

2 Answers2

0

You could try redirecting the user's browser to the error page? You might need to save any error information you want to display to the user in the session, so the error page can display it.

The only issue is writing the redirect message might fail as content has already been delivered from the faulty 'page'. Not sure it's very bulletproof :)

Possibly increasing the buffer size might make it work on more pages.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

As already hinted in a comment in your previous question, it's simply too late to change the response. After a X amount of bytes in the response, the response headers will be committed to the client and this is a point of no return. You need to rewrite your JSP's. They should contain only markup and presentation logic and not any business logic which can throw exceptions.

If you really can't refactor the erroneous business code in your JSP's into fullworthy servlet/business classes, there where they belongs, then your best bet is to increase the HTTP response buffer size in the server config and pray that nowhere a flush() is been invoked before the exception occurs in the JSP and you don't get OutOfMemoryError failures when it gets busy on your webapp.

Related questions:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Like I mentioned before, I cannot rewrite my JSPs because they are user submitted and I am just writing the application to include them all on one page as modules. I was hoping there was a workaround where I could maybe create a mock response and try committing there first and if there are no exceptions then include to the actual response?? – Art Peterson Jan 07 '11 at 14:49
  • So, increase response buffer size and give your server enough RAM. I would however still reconsider the design so that the "modules" are been processed before the request is about to be forwarded to a JSP view. This however requires an entirely different approach of how "modules" are setup. – BalusC Jan 07 '11 at 14:50
  • Yes I think that it what I am trying to figure out, how to pre-process the jsps before having to commit them to the response. Could you share a strategy to do this? – Art Peterson Jan 07 '11 at 14:52
  • Don't make them JSP's, but just something like UBB or pure EL. – BalusC Jan 07 '11 at 14:54
  • Ok thanks for the input. I was really hoping there was a simple way to create a fake copy of the response and try including to there first to make sure no exceptions are thrown. – Art Peterson Jan 07 '11 at 14:57
  • It boils down to just disallow scriptlets in user-submitted data. – BalusC Jan 07 '11 at 14:58