We want to allow our web users to view some excel document. If they download the excel files in a zip file and open locally, they are fine. But if we send a file directly as a response to a web request, the file is not showed correctly, a lot of strange characters are displayed.
Yes, I did. I even tried to use HSSF classes to load the excel file and write to the response. The code looks something like the following:
response.setContentType("application/octet-stream"); response.setHeader( "Content-Disposition ", "inline;" + "filename=testWorkSheet.xls; "); //reading the excel file into write out to the response output stream
As noted in previous threads from the last couple weeks, this is difficult (if not perilous) to do from a JSP. It's much easier done from a servlet.
Setting the content type and then sending the file works from a JSP if you're just sending delimited text or HTML tables. In this case you're using a 3rd party library to build a binary Excel file. I should have noticed that before telling you to change the content type.
The other option is to write the Excel file to disk on your server and use a sendRedirect to have the browser retrieve it as a static file. (yuk).
There was a thread earlier this week. In it someone posted some JSP code that streamed a binary file. He managed to get it working in Tomcat 4.1 by making sure there was no space in between any of the JSP tags.
If you go that route, instead of using a servlet, there is no guarantee that it will work in other app server or in future releases of the app server you are using.
If you want to see an example of a servlet that streams binary files go to. http://simple.servlet.com and download SimpleStream. It streams image files from the server's file system. It shouldn't be difficult to modify it to stream the Excel file you've created on the fly.
Originally posted by Bear Bibeault: Why people insist on banging their heads against the wall like this is quite beyond me; especially when servlets are so easy to code.
Fear of javac or CLASSPATHs? That first experience with javac leaves a bad taste in the mouths of a lot migrants from ASP/PHP/Perl... which is a shame because (as I've barked before), servlets lay the concepts of server side java out much more clearly than do JSPs. [ January 21, 2005: Message edited by: Ben Souther ]
You're absolutely right, Ben. I did not know how came people started to use JSP instead of Servlet. I tried using Servlet, and my tesg code works fine now. The reason I started this thread was that our legacy code was written in JSP and peope claimed that it worked in the past and it does not work any more, and I was asked to look at that. I'm going to change it into Servlet for this tiny part now.
Thanks a million and have a nice day, Jas
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo: