I asked the question earlier about Content-Disposition breaking JSP code. Although I didn't find an optimal solution, I did come to the conclusion of using a popup window to download the file. This way the parent UI doesn't get blocked due to 'Response Already Committed' error.
I am now trying to pretty things up by presenting the user with a confirmation message after the download button is pressed in the popup window. In the download action class, I am setting the CSV data in a HTTPServlet request attr and forwarding to the following JSP file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/sl.tld" prefix="sl" %><%@ taglib uri="/WEB-INF/c.tld" prefix="c" %><html:html><sl:head><sl:title bundle="labels" key="title"/><% response.setContentType("text/csv"); String csvData = (String) request.getAttribute("csvData"); String fileName = (String) request.getAttribute("fileName"); // Setting header that serves download functionality on button click String headerKey = "Content-Disposition"; String headerValue = "attachment; filename=".concat(fileName); response.setHeader(headerKey, headerValue); response.getWriter().append(csvData); response.getWriter().flush(); %></sl:head><sl:body onload="return true;" showMenu="false"><sl:message/><table border="0" cellpadding="2" cellspacing="1" align="center"><tr><td><sl:imageButton imageType="simpleGreenButton" submitValue="button.close" onclick="window.close(); return false;"/></td></tr></table></sl:body></html:html>
When I click the Download button on the popup, it downloads the CSV file but right after all the records in it, the JSP file's HTML is being appended as well. Something like:
I can see the HTML in this CSV is exactly what I want it to be but I want it to show in the webpage and not in the CSV file.Using writer.close()
in place of writer.flush()
doesn't help either
Please help.