The reason may be the the short of knowledge in java 🙁 ,so I’m asking this question,
Here in this piece of code I’m getting dynamic value(from a jsp page) :
<form action="">
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/apps","root","root");
Statement stmt = con.createStatement();
String sql = "select * from info;";
ResultSet rs = stmt.executeQuery(sql);
System.out.println(sql);
System.out.println("hi Tirtha");
%>
<center>
<h3>Information of User's</h3>
<table cellpadding="4" cellspacing="2" border="1" bgcolor="">
<tr>
<th>User Name</th>
<th>Email Id</th>
</tr>
<tr>
<%while(rs.next()){%>
<td><input type="text" name="name" value="<%= rs.getString(1)%>" readonly="readonly">
<td><input type="text" name="email" value="<%= rs.getString(2)%>" readonly="readonly">
</tr>
<%}%>
</table>
</center>
</form>
Now I want to save this data in a csv file(having an export option in it).
Any inputs will be appreciated.
here is a class you can using to export to CSV:
Working the MVC way
Here is how your code should be written:
Let’s say you have a class called. User.java inside of which there is a static function called get all users
Then let’s say you have a servlet called UsersServlet which get these users:
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:
but please note that you have to link the servlet to the url using web.xml:
EDIT: Writing to disk instead of sending to browser