• 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

Help with getOutputStream() Exception

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic