Author |
OutputStream already obtained
|
Neeraj Vij
Ranch Hand
Joined: Nov 25, 2003
Posts: 315
|
|
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.
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12533
|
|
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
|
Java Resources at www.wbrogden.com
|
 |
Neeraj Vij
Ranch Hand
Joined: Nov 25, 2003
Posts: 315
|
|
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.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 58524
|
|
Is there any work around for it...
Yes. Use a servlet.
|
[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
|
 |
Neeraj Vij
Ranch Hand
Joined: Nov 25, 2003
Posts: 315
|
|
in jsp is there any way ..
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 58524
|
|
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
Joined: Nov 25, 2003
Posts: 315
|
|
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
Joined: Nov 25, 2003
Posts: 315
|
|
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.
|
 |
Sandeep Deshmukh
Greenhorn
Joined: Dec 06, 2004
Posts: 16
|
|
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
|
Java and Oracle Programmer
|
 |
Matt DeC
Greenhorn
Joined: Jun 22, 2003
Posts: 5
|
|
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.
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
http://simple.souther.us/SimpleStream.war
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Aparna Sharma
Ranch Hand
Joined: Mar 12, 2003
Posts: 30
|
|
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
|
Aparna Sharma<br />SCJP 1.4 (yippeeee!)
|
 |
Joey Garcia
Greenhorn
Joined: Oct 11, 2007
Posts: 2
|
|
This post worked for me.
Originally posted by Neeraj Vij: Is there any work around for it...
|
 |
Terry Roberts
Greenhorn
Joined: Jun 14, 2006
Posts: 1
|
|
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.
|
 |
 |
|
subject: OutputStream already obtained
|
|