The thing to watch out for when you use a JSP instead of a servlet is the white space.
Benjamin wrote that his JSP looked like this: <%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook, com.business.poi.WorksheetGenerator" %> <% [scriptlet] There is a end-of-line in between the third and fourth line of this JSP, which will be copied to 'out'. There may be (probably is) more white space in the JSP that is also sent to 'out' before the scriptlet begins. So these blanks and newlines would be the first bytes received by the client unless something else happens. The reason there is different behavior is because of the implementation of response.getOutputStream(). It's a different kind of stream from the JSP out. But both streams are connected to the HTTP servers socket. The problem comes when you mix output to the two streams. Most implementations will reset the other stream when new output is sent. WebLogic will not reset either stream unless you specifically call it's reset() method. In my opinion, WebLogic is more correct than Tomcat, but I guess the servlet JSP specification does not address this issue. When I use a JSP to serve binary content, I do this: <%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook, com.business.poi.WorksheetGenerator" %><% [scriptlet] if( true) return; %> I also include the response.reset(), as Danny suggested. The funny way of putting an end tag and begin tag on the same line is to ensure that there is no white space before the scriptlet is run. The 'if (true) return;' is so that there can be no white space sent after the scriptlet has run. Another tip. Call the .jsp with a URL that ends in '.xls'. For example, use 'MyExcelDownload?bill_gates=sheet.xls'. Internet Explorer will often ignore the Mime Type from the response, and look at the last few characters of the URL to determine that the file is meant to be opened in Excel. (I used bill_gates as a dummy parameter). -----Original Message----- From: Danny Mui [mailto:[EMAIL PROTECTED] Sent: Friday, September 05, 2003 2:13 PM To: POI Users List Subject: Re: weblogic 6.1 writing poi to the response I have a servlet doing it for me but JSP is a servlet at the end of the day. Well something to look out for in the future anyways. Joshua Davis wrote: >Danny, > >What is serving up the Excel stream, a JSP or a Servlet? My code uses a >Servlet, and this works fine without a response.reset() in Weblogic 6.1. In >this case, the only material difference between Ben's scriplet and my >Servlet code is a call to flush the output stream. > >-----Original Message----- >From: Danny Mui [mailto:[EMAIL PROTECTED] >Sent: Friday, September 05, 2003 1:44 PM >To: POI Users List >Subject: Re: weblogic 6.1 writing poi to the response > > >Tomcat needs this too :) > >I'm going to add this to the FAQ! I will, I promise. > >In Tomcat (well it was Resin for me..), if you click OPEN instead of >SAVE it fails. Issuing response.reset() solved this for me so it was a >hunch. I found this solution on the FOP mailing lists since it's a >common enough problem. > >Benjamin Simpson wrote: > > > >>Danny >> >>Thanks for the correct tip. Here are the javadocs, version 1.3 because >>it is Weblogic 6.1 we are talking about, on what response.reset() is >>supposed to do: >> >>"Clears any data that exists in the buffer as well as the status code >>and headers. If the response has been committed, this method throws an >>IllegalStateException. >>Throws: java.lang.IllegalStateException - if the response has already >> >> >been > > >>committed >>See Also: setBufferSize(int), getBufferSize(), flushBuffer(), >>isCommitted()" >> >>Why weblogic 6.1 needs this, and Tomcat doesn't is a mystery that only >>the weblogic source could shed light upon. >> >> >>BTW, >> >>Flushing the response buffer, see javadocs here >>"http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRes >>ponse >>.html#flushBuffer()" will only push whatever junk was in the the Weblogic >>implementation of HttpServletResponse INTO the xls file that I am pushing >>out to the client. >> >>If you are ever in Michigan or Chicago, I owe you a beer. >> >>Ben >> >> >> >> >>----- Original Message ----- >>From: "Danny Mui" <[EMAIL PROTECTED]> >>To: "POI Users List" <[EMAIL PROTECTED]> >>Sent: Friday, September 05, 2003 12:52 PM >>Subject: Re: weblogic 6.1 writing poi to the response >> >> >> >> >> >> >>>Also try a call to response.reset() before you do anything. >>> >>>Benjamin Simpson wrote: >>> >>> >>> >>> >>> >>>>Hello poi users and developers. >>>> >>>>I built web application using poi to assemble spread sheets of data. >>>>I provide links to jsps which build an HSSFWorkbook and then tell the >>>> >>>> >>>> >>>> >>workbook >> >> >> >> >>>>to write via a "wb.write(response.getOutputStream());" call. There >>>>have been examples on how to do this in the list archives. I've done >>>>this >>>> >>>> >>>> >>>> >>before >> >> >> >> >>>>using other contentTypes. This is truly not a big deal... >>>> >>>>Here is the code I am using. The plan is to take it out of the jsp >>>>AFTER >>>> >>>> >>>> >>>> >>it >> >> >> >> >>>>is proven to work: >>>> >>>><%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook, >>>> com.business.poi.WorksheetGenerator" >>>>%> >>>><% >>>> HSSFWorkbook wb = new HSSFWorkbook(); >>>> WorksheetGenerator.getInstance().getSheet(wb); >>>> >>>> >>>> >>>> >> response.reset(); >> >> >> >> >> >>>> response.setContentType("application/vnd.ms-excel"); >>>> response.setHeader("Content-Disposition","attachment; filename = >>>>business.xls"); >>>> wb.write(response.getOutputStream()); >>>>%> >>>> >>>>It works fine on Tomcat 4.1.x. In fact, it worked the first time. >>>>Which >>>> >>>> >>>> >>>> >>is >> >> >> >> >>>>always bad luck. :( >>>> >>>>I move the war over to Weblogic6.1, not my choice, (NT or Solaris >>>>same >>>>results) and the content comes out in a binary format. This won't go >>>> >>>> >>>> >>>> >>over >> >> >> >> >>>>very well with the users who only know how to read spread sheets, not >>>> >>>> >>>> >>>> >>binary >> >> >> >> >>>>formats... >>>> >>>>I figured it was a mime issue and stuck an entry into the web.xml: >>>> >>>><mime-mapping> >>>> <extension>xls</extension> >>>> <mime-type>application/vnd.ms-excel</mime-type> >>>></mime-mapping> >>>> >>>>ok. Still doesn't work. I have been trying different approaches to >>>>the problem to no avail. >>>> >>>>A couple of them included sticking a simple xls file in the >>>>application >>>> >>>> >>>> >>>> >>and >> >> >> >> >>>>clicking on a link to it through a browser to make sure that it is >>>>not a mime issue. Great. I can open excel pre-existing documents >>>>through the browser but not ones that are stuffed into the response's >>>>outputStream. (This opens an ugly and unacceptable workaround of just >>>>writing the new HSSFWorkbooks to the exploded war and >>>>response.sendRedirecting to them... not an option). >>>> >>>> >>>>So here comes the problem statement: >>>> >>>>The above code works on tomcat 4.1.x but not Weblogic 6.1 sp3. >>>> >>>>This is not the first issue that I have run into with Weblogic 6.1 >>>>being funky about writing to the output stream. What is the deal, >>>>any clues? >>>> >>>>Benjamin Simpson >>>>www.javablog.com --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]