I am trying to forward a request to another page using and I get the following exception:
I am not writing anything in the JSP page prior to calling the forward() method yet I still get this exception. When I look at the generated .java file it has a bunch of out.write("\r\n"); lines in it. Are those writes preventing the forward method from working? If so how can I remedy this without having to rewite the JSP page as a normal servlet. Below is the code snipet that generates the error:
Thanks, Jamin [ March 30, 2004: Message edited by: Jamin Williams ]
The "\r\n" you are seeing come from the crlf after each page tag. JSP assumes you are going to be writing a character stream - thats why people tend to use a servlet to do initial screening of a request instaed of a JSP - it avoids this premature openng of an output stream. Bill
You wouldn't be doing a forward in the JSP page at all. When the request is received by the server, a servlet handles all up front processing, be it SQL access or whatever; it emits no html. It then forwards to a JSP page to render the page. Any data that the servlet needs to pass to the page for display are in the form of beans (usually pretty dumb value objects) that are tacked onto the request as attributes.
Ah ha! I discovered what the problem was. If the output is buffered then you need to clear the buffer (i.e. out.clear(); ) prior to calling the RequestDispatcher forward method. If the buffer is not cleared then an exception is thrown when the forward method is called. The <jsp:forward> action generates code that clears the buffer for you before forwarding the request. That is why the <jsp:forward> action works and the equivelent RequestDispatcher forward will throw an exception (unless you clear the buffer first). Jamin [ March 30, 2004: Message edited by: Jamin Williams ]