Redirect to new JSP page


Web tier: servlets, JSP, Web frameworks: Redirect to new JSP page

  1. Redirect to new JSP page (6 messages)

    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.

    Threaded Messages (6)

  2. Redirect to new JSP page[ Go to top ]

    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.

  3. Redirect to new JSP page[ Go to top ]

    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.
  4. Redirect to new JSP page[ Go to top ]

    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
  5. 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.

    If you can safely assume the client has javascript support, I'd use a javascript redirect (self.location.href="x.jsp"). Otherwise, you may have to test the condition before generating output.
  6. Thankx steve holdener, I got it from your explanation.
  7. 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.

    Using a JavaScript location setting is another way of displaying a different page, however this method should be used carefully since it may not be supposted by all the browsers.