File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes ServletOutputStream is not working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "ServletOutputStream is not working?" Watch "ServletOutputStream is not working?" New topic

ServletOutputStream is not working?

Maruli Lee

Joined: Dec 09, 2004
Posts: 11
I had a previous program which is working fine so I just copied the chunk of code and paste it on the new program which I am building. Basically my program will generate a report using JExcel API and then I will try to display it on the web so that user can Open/Save/Cancel it (the normal IE notification window). Furthermore, there is no error message on the tomcat log.

[ November 27, 2007: Message edited by: Maruli Lee ]
[ November 27, 2007: Message edited by: Bear Bibeault ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Is this code in a servlet or a JSP?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Maruli Lee

Joined: Dec 09, 2004
Posts: 11
Hi Ben,

It's in JSP, that's why I posted it under the JSP forum.

Additional info, this chunck of codes are embedded within

<form name="frm" method="POST">
//Code goes here
[ November 27, 2007: Message edited by: Maruli Lee ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63529

So you are embedding the output in the middle of some HTML? Of course that won't work. What are you really trying to do?

[Asking smart questions] [About Bear] [Books by Bear]
Maruli Lee

Joined: Dec 09, 2004
Posts: 11
No no no. The codes are within the JSP tags <% %>

Actually my JSP will be calling a Java program which will generate a report in Excel (Using JExcel API). After the report is created it will be stored in C:\tomcat\webapps\SnetReport\REPORT

What I am trying to do is to pop up a save file dialog box so that the user can retrieve the file from the web instead of having to go into the folder to get it manually.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
A good rule of thumb is to not use the servlet output stream in a JSP, ever. As Bear said, it won't work. Theoretically, it might, but practically, it won't. Moving the code into a servlet will help.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13024

Of course it won't - JSP has automagically already gotten the output stream for character output - thats what JSP do. You are undoubtedly getting a thrown at that point. See the JavaDocs.

Maruli Lee

Joined: Dec 09, 2004
Posts: 11
I have modified my code to avoid using OutputStream since it will cause a lot of problem. Now my modified code looks like this:

After I ran through this code my JSP page will refresh and show me this screen:

In addition, Tomcat shows following message:

Nov 29, 2007 10:44:38 AM org.apache.jasper.runtime.PageContextImpl release
WARNING: Internal error flushing the buffer in release()
Stefan Evans

Joined: Jul 06, 2005
Posts: 1518
As has been mentioned, JSPs are designed for output of text.
Sending a binary file via JSP is tricky for a number of reasons.

I would recommend you read Bear's excellent article on this subject: The Secret Life of JavaServer Pages where he answers the question "Why does everyone tell me to use a servlet to send binary data to the browser? I wanna use a JSP!"

You've written it all in java code anyway - it SHOULD be a servlet.
I agree. Here's the link:
subject: ServletOutputStream is not working?
It's not a secret anymore!