File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet download file writer is already used by this request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet download file writer is already used by this request" Watch "Servlet download file writer is already used by this request" New topic

Servlet download file writer is already used by this request

Karen Fishwick

Joined: Aug 23, 2001
Posts: 2
I am a beginner jsp/java developer. My problem seems very common, but I have tried many posted answers and can't seem to solve it. I am using a servlet to try to download a file to the client. I want this file to download as an attachment.
I get an error "java.lang.IllegalStateException: Writer is already being used for this request". I have tried using a PrintWriter, Writer, and OutputStream types to download the file.
I have an additional question which is, how do you set a default location for the file to be saved on the client (I would like to save straight to diskette).
Here is my servlet code:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName = "c:/jakarta-tomcat/smithapp/01/batches.txt";
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
FileInputStream fileInput = new FileInputStream(fileName);
int numOfBytes = fileInput.available();
byte byteArray[] = new byte[numOfBytes];
int nextByte =;
OutputStream outStream = response.getOutputStream();
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

The only thing I could see without trying the code was that getOutputStream actually returns a ServletOutputStream - I don't know if using its superclass would be a problem here or not.
As far as local file names go, this depends on what method of file-saving you're doing. Java applets are constrained by their sandboxes. JavaScript allows you to specify the default filename for a "save" dialog, I believe.

An IDE is no substitute for an Intelligent Developer.
Karen Fishwick

Joined: Aug 23, 2001
Posts: 2
I think my problem was related to the way I was calling the servlet (by jsp:include or jsp:forward). I changed it to forward to the url after a short timeout and that seemed to fix things.
I can get the filename to default but I have trouble with the drive. It might just be that I'm not escaping my characters correctly, it appears to ignore the 'a:\download.txt' and replaces it with the url. If I take off the a:\ then 'download.txt' is the default file.
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
JSP's cannot write binary data to the response output stream, either directly or by calling <jsp:include>. The JSP specification explicitly forbids it. The reason for this is that the JSP container automatically initializes the "out" implicit variable with response.getWriter(). If you look at the API documentation for javax.servlet.ServletResponse, you'll note that you can call either getWriter() or getOutputStream(), but not both.
Tomcat defers opening the JspWriter "out" object until you actually write a character, but it will still probably throw an IllegalStateException when it does.
You can write a binary output stream from a servlet, exactly as you have described, but not from a JSP (at least not reliably). If you need to use the JSP as the front end to the application, then use response.sendRedirect("the servlet URL") instead of <jsp:forward>. That will cause the client browser to initiate an additional request, which will not comingle the character and binary output.
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
[This message has been edited by Phil Hanna (edited August 25, 2001).]

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
I agree. Here's the link:
subject: Servlet download file writer is already used by this request
It's not a secret anymore!