Is it an acceptable practice to use response.sendRedirect() in a JSP page? Or should you just throw an exception and use the error page directive?

Lee Meador

The response.sendRedirect() will direct the browser to go to a new page/url. The new url will appear in the 'location' at the top of the browser page. You must specifiy a full URL like Using this requires a round trip to the server with all the attendant delays.

A pageContext.forward() will "jump" to the url specified which may be a relative url like "other.jsp" to indicate it is in the same base location as the current page. The browser location box will show the URL of the original page, not the one you redirected to. There is no additional trip back to the browser and then back to the server so time is kept to a minimum.

When using pageContext.forward(), this forwards the original request and response objects. If you need to change one of the attributes in the original request object, there is no function to do this but this can be reset by passing new querystrings in the URL given to forward.

An errorpage directive just puts a big "try {.. } catch (Throwable x) {..}" around the Java for the whole jsp page (including each of the out.print() lines that handle the HTML portion of the jsp page) If an exception is "caught", things are redirected (as in pageContext.redirect() above) to the error page. If you put the "isError" directive on the error page you get a few predefined jsp variables that tell you what the exception is that triggered the jump to the page.

So the answer to your question, in no uncertain terms, is "maybe". It just depends on what you want to do.
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact