Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JSP and the fly likes RequestDispatcher and .forward() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "RequestDispatcher and .forward()" Watch "RequestDispatcher and .forward()" New topic

RequestDispatcher and .forward()

Dan Spaid Dan Spaid

Joined: Sep 18, 2004
Posts: 2
Ok, I'm very new to JSP, so forgive me if this is a simple one. I'm working on fixing some errors in a page (one that I did not write myself unfortunately) and I keep getting a "Cannot forward because the response has already been committed" 500 Error.

I've narrowed it down to where I'm doing a forward() of my RequestDispatcher object, and I've read various topics on it that have told me this is happening because I've committed some output already. My question is how do I find where that is. Here's the code in the method that calls forward().

RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
HttpSession session = request.getSession(true);

session.setAttribute("user_type", "volunteer");
session.setMaxInactiveInterval( 60 * 15);
session.setAttribute("loggedin", "true");
if (rd != null)
rd.forward(request, response);

If anyone has any suggestions or if there's any other part of the code that might help just let me know.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

The code you posted is fine. As you have surmised, the problem is where in the JSP that code is called from. Any decisions regarding forwarding or redirection should take place at the top of the JSP before any template text or code that causes output to be generated . Actually, ideally, all this would have been taken care of in a servlet controller rather than the JSP itself, but that's probably not the structure you've been handed.

Once any template text or other output has been emitted, the response is considered comitted and you will get the result you are witnessing.
[ September 18, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Dan Spaid Dan Spaid

Joined: Sep 18, 2004
Posts: 2
Ok, thats pretty much what I thought. Now my question is how the layout of the script might be screwing things up. Its set up for a account registration page, with one JSP script creating the 4 registration pages, with this login step being the final end of the script. The question is
A: When do I have to worry aobut output and committing the page, after the final page is displayed? or some time earlier. Still working on figuring out exactly how all that works
B: Is there a better way to do this? Granted I don't really want to reprogram the whole thing from scratch, but is there a better way of doing it than forward()?
I agree. Here's the link:
subject: RequestDispatcher and .forward()
It's not a secret anymore!