Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with getOutputStream() Exception

 
Jamin Williams
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jamin Williams
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
that's 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

That is interesting. If that is the case then how is the <jsp:forward> action ever supposed to work?
Jamin
 
Jamin Williams
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jamin Williams:

That is interesting. If that is the case then how is the <jsp:forward> action ever supposed to work?
Jamin

Can anyone help me out with this?
Jamin
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jamin Williams
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic