Title: Jason Hunter's MultipartResponse

I've got a situation where I have a servlet that builds a 'file' on the fly and pushes it to the client using

response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
response.setHeader("Content-encoding","binary");

This works just fine. The problem is that I need to display a page to the user ALSO. I thought I would use a technique similar to Hunter's MultipartResponse on p. 192 of his book. This involves setting headers, getting your output stream, writing, then flushing the stream and doing it all over again - within the same servlet.

I went so far to implement that MultipartResponse class from Hunter's book. None of this worked. It seems that I can't change that content type once I've set it. If I try to write to a page after the file, I get an error and the HTML of that error winds up in the file too. I'm running inside WebLogic and it gives me a runtime error saying :

<E> <ServletContext-Servlets> java.lang.IllegalStateException: cannot set headers after headers have already been written

I thought of a possible alternate way of doing this using frames. You have one frame of column size zero that runs the servlet, another that displays a page. This hasn't worked yet either though.

I've checked the archives but can't turn up anything on how you do something like send a file to the browser AND display a page. If you have any ideas, please let me know.

Jay Baker

Reply via email to