There was a good article on this in the now defunct magazine Java Report a
while back. Here's a simple example that uses a controller servlet to place
an Object[] in the request attributes.

<%@ page contentType="application/csv" %>
<jsp:useBean id="values" class="com.mycompany.MyBean[]" scope="request"/>
<% for (int i = 0; i < values.length; i++) { %>
"<%= values[i].getA() %>","<%= values[i].getB() %>","<%= values[i].getC() %>"
<% } %>

The contentType tells the browser it's not getting HTML, but something
else. This generally triggers a "Save As..." dialog allowing the user
to save the file wherever they want.

The trick is to map a url with a .csv extension to your JSP page or to a
servlet that forwards to a JSP as follows...

<servlet>
  <servlet-name>DownloadCSV</sevlet-name>
  <jsp-file>/mycsv.jsp</jsp-file>
</servlet>

<servlet-mapping>
  <servlet-name>DownloadCSV</servlet-name>
  <url-pattern>/download.csv</url-pattern>
</servlet-mapping>

By spoofing the file extension, MSIE users with MS Excel installed will
be able to open the file directly into Excel or save it to disk. Netscape
won't know how to feed the file to Excel, but it will open a Save dialog.

Note that a carriage return in your JSP file after a closing '%>' will generate a
carriage return in your output even if the JSP tag didn't generate any text. So
you need to tweak your tag delimiters to control the white space in the resulting
output. Here's the above example with better control of whitespace:

<%@ page contentType="application/csv"
%><jsp:useBean id="values" class="com.mycompany.MyBean[]" scope="request"
/><%
 for (int i = 0; i < values.length; i++) { %>
"<%= values[i].getA() %>","<%= values[i].getB() %>","<%= values[i].getC() %>"
<% } %>

It looks ugly, but it works...

Enjoy,

Jeff




----- Original Message -----
From: "Rick Sample" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, April 05, 2002 3:29 PM
Subject: CSV output help


> I could use some links to some examples for output to a CSV file.
>
> Basically what I want to do is select some fields
> from a form and gererate a CSV file to the client.
>  - Examples on how to create the CSV to out.
>  - and, a dialog box for the user to select where to save it.
>
> Thanks!
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://archives.java.sun.com/jsp-interest.html
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.jsp
>  http://www.jguru.com/faq/index.jsp
>  http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to