I am using response.sendRedirect("x.jsp") in mid of the jsp page and getting illegalStateexception during testing. The JSP page contains some gif images and text using html tags and doing java codes logical part, here checking for a condition, if true redirecting using the method. It throws exception with message "response already committed". Need suggestion to overcome in my application so that redirection works. thankx in advance.
If you are using a Printwriter out, then it should work fine and it will call the *.jsp page when using a response.redirect (*.jsp).It may be without a PrintWriter too, still it should work.
Another way of redirecting is
req.setAttribute ("x",x) and then a callPage("url",req,res).
If you try to use both of them on the same page with a Printwriter it will throw an IllegalAccessException so try avoiding it.It is b/c of the Servlet Spec.
What is Webserver you are using.If it is TOMCAT , I somehow think there was somebody also mentioning the same problem here.Please refer to that.
I think it's impossible to use any redirect commands after writing something to response regarding to the HTTP standard. The redirect command enforces browser to request another page you have specified.
In your case I think the best choice will be to use <%@ include file='x.jsp' %> between flow control operators. In this file you must not specify standard begin and end tags like <html>, <head> or <body>. So you can show different content depending on your application logic.
This redirection works with some pages but in few pages says respose already committed-illegalStateException. I can't get when it really occurs and where to use redirection. right now testing it on Weblogic5.1
Yeah, I think you might have problems redirecting once output has been written because the jsp redirect should work by sending an HTTP 301 (redirection) header to the client. If you're in the middle of the page, a 200 (ok) response has already been sent.
Thankx steve holdener, I got it from your explanation.
Redirection is not allowed once any HTML tags have been written out. The way to eliminate this problem is to check all the problematic conditions before writing any HTML.