Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>here is a class you can using to export to CSV:</p> <pre><code>import java.io.FileWriter; import java.io.IOException; import User; public class GenerateCsv { private static void generateCsvFile(ArrayList&lt;User&gt; users) { String output = "Email, Name\n"; for (User user in users) { output += user.getEmail() + ", " + user.getName() + "\n"; } return output; } } </code></pre> <p>Working the MVC way</p> <p>Here is how your code should be written:</p> <p>Let's say you have a class called. <em>User.java</em> inside of which there is a static function called get all users</p> <pre><code>public class User { String name; String email; public static ArrayList&lt;User&gt; getAllUsers() { // returns all users ... } } </code></pre> <p>Then let's say you have a servlet called UsersServlet which get these users:</p> <pre><code>import javax.servlet.*; import javax.servlet.http.*; public class UsersServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("application/csv"); PrintWriter w = res.getWriter(); ArrayList&lt;User&gt; users = Users.getAllUsers(); w.prinln(GenerateCsv.generateCsvFile(users)); w.flush(); w.close(); } public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ... } } </code></pre> <p>in your jsp, for example, you will have a simple anchor tag which calls the servlet (the servlets calls User.java, get data, forms them into a CSV and then outputs it to the browser...). Something like this would work:</p> <pre><code>&lt;a href='/getCSV' &gt; Export CSV &lt;/a&gt; </code></pre> <p>but please note that you have to link the servlet to the <em>url</em> using web.xml:</p> <pre><code>&lt;web-app&gt; &lt;servlet&gt; &lt;servlet-name&gt;UsersServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;__package__.UsersServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;UsersServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;getCSV&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p>EDIT: Writing to disk instead of sending to browser</p> <pre><code> import java.io.FileWriter; import java.io.IOException; import User; public class GenerateCsv { private static void generateCsvFile(String fileName, ArrayList&lt;User&gt; users) { try { FileWriter writer = new FileWriter(fileName); writer.append("Email"); writer.append(','); writer.append("Name"); writer.append('\n'); for (User user in users) { writer.append(user.getEmail()); writer.append(','); writer.append(user.getName()); writer.append('\n'); } writer.flush(); writer.close(); } catch(IOException e) { e.printStackTrace(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload