aspose file tools*
The moose likes JSP and the fly likes Help with getOutputStream() Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Help with getOutputStream() Exception" Watch "Help with getOutputStream() Exception" New topic
Author

Help with getOutputStream() Exception

Jamin Williams
Ranch Hand

Joined: Dec 16, 2003
Posts: 44
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 ]

Jamin Williams<br />SCJP, SCWCD
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Dec 16, 2003
Posts: 44
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

Joined: Dec 16, 2003
Posts: 44
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jamin Williams
Ranch Hand

Joined: Dec 16, 2003
Posts: 44
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with getOutputStream() Exception