• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

OutputStream already obtained

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I am putting OutputStream outStream = response.getOutputStream();

in my jsp file to open the excel file in browser.

I am getting exception,

java.lang.IllegalStateException: OutputStream already obtained


I feel this is coming bcoz of out object by default is there on the jsp pages.

Please provide some inputs ASAP.

Regards,
Neeraj.
 
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
Look at the Java code that JSP pages generate - you will see that the JSP compiler puts in a call that sets "out" by getting a Writer from the response stream. Thats because JSP are designed to make it easy to send character streams.
Any form of data that needs to be sent as a binary stream of bytes should be sent by a servlet, NOT a JSP.
Bill
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help william.

Is there any work around for it...

I have tried by closing the stream also.. still it gives the same exception.

Regards,
Neeraj.
 
Sheriff
Posts: 67754
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

Is there any work around for it...



Yes. Use a servlet.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in jsp is there any way ..
 
Bear Bibeault
Sheriff
Posts: 67754
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 probably might be able to trick the JSP into doing what you want, but why go through the pain and aggravation when a servlet would be so much easier and efficient? And stable.
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnx Bear...

I am modifying an existing application which has event and view handlers and a single controller servlet only..

only request object is being passed to view and event handlers..currently there is no response object to work with..That' why I was asking for some work around in jsp.

Regards,
Neeraj
 
Neeraj Vij
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more doubt is there..inputs will be a great help..


If we do objResponse.getOutputStream() in a servlet it will not throw an exception.

But how to show some success message or do a redirection to some page after file has been downloaded.

it still comes down to using the response object which has already obtained a outputstream.

Regards,
Neeraj.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neeraj

If you have placed the process of getting the outputstream inside a try catch it will be easy to track completion depending on whether try block is successfully executed or not. A successful closing of stream can help you do what you require to do.


Sandeep
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was having the same issue. The postings here have been very helpful. My problem was in upgrading code from WebSphere 4.x to WebSphere 5.1. 4.x must be more forgiving than 5.1 because 5.1 was throwing the error, while 4.x never did.

To solve, I literally c/p the logic in the jsp and placed it in a new servlet. I placed a <jsp:forward page="/blahServlet" /> in the jsp, and it solved my issue w/o having to change all the pages that referenced the old/bad jsp.

Things seem to be working fine.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was having the same trouble.I rewrote the JSP to a Servlet and it worked like a charm.

I was getting a pdf as binary array and trying to display it in browser.First i was getting some weird characters in the display.

Then one fine morning, I was being shown the File Download message(You computer is trying to download a file.Some files maybe potentially harmful. File name, File type, Location . Open,Save,Cancel,More Info blah blah blah).

But curiously the file type was missing , file name was my whole URL ( .../record.jsp?ID=12345) and location was localhost(I am using WSAD and running the JSP on the test server). When I say Open on the File Download dialog,it started downloading and then errored out saying Could not download from location. On top of that I was getting the outputstream exception in my jsp.

Given below is the jsp code which gave me error.I was writing a scriplet.

If some one is having this same trouble, consider rewriting it to a Servlet.


thanks,
Aparna
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This post worked for me.

Originally posted by Neeraj Vij:
Is there any work around for it...




 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can use OutputStreams with JSP's. I just did it then using:

<%
java.io.OutputStream o = response.getOutputStream();
javax.naming.InitialContext iniCtx = new javax.naming.InitialContext();
javax.sql.DataSource dataSource = (javax.sql.DataSource)iniCtx.lookup("java:jdbc/datasource");
java.sql.Connection con = dataSource.getConnection();

java.sql.PreparedStatement stmt = con.prepareStatement("select top 1 photo from photos order by id desc");
java.sql.ResultSet rs = stmt.executeQuery();
rs.next();
o.write(rs.getBytes(1));
o.flush();
o.close();
%>

Worked a charm! You can't have any spaces around otherwise it will use a JSPWriter.

Why use Servlets if you want to do a quick test? Servlets are a pain to setup.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic