title: Build CSV dynamically in JSF 2
body: <p>Basically what I want to do is this : <a href="http://www.mkyong.com/java/how-to-export-data-to-csv-file-java/" rel="nofollow">http://www.mkyong.com/java/how-to-export-data-to-csv-file-java/</a></p>
  
  <p>Except at the end serve it as a download to the user.
  Obviously I can't use FileWriter as I'm using JSF,but I tried doing something similar by getting the output stream from the external context and then making a buffered writer using the output stream(code below).According to my logs this does execute completely till the end,but no save as dialog pops up.I am using JSF 2(MyFaces) and RichFaces 4.3,if there is a better way to do this using a component please tell me.</p>
  
  <pre class="lang-java prettyprint-override"><code>externalContext.responseReset();
  externalContext.setResponseHeader("Content-Disposition", "attachment; filename=test.csv");
  externalContext.setResponseContentType("text/csv");
  
  try {
      OutputStream csvOut = externalContext.getResponseOutputStream();    
      BufferedWriter csvWriter = new BufferedWriter(new OutputStreamWriter(csvOut, "UTF-8"));
  
      csvWriter.append("data,data,data,data");
  
      csvWriter.flush();
      csvWriter.close();
      csvOut.close();
  
      facesContext.responseComplete();
  }catch(Exception ex){
      System.out.println(ex.toString());
  }
  </code></pre>
  
  <p>Any suggestions will help alot,been struggling with this for some time now.</p>