• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RequestDispatcher and .forward()

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Dan Spaid Dan Spaid
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic