Question : How to get download csv file using java servlet ?

I need to download the data file from database with csv format using java/servlet.I would like to download the csv file when the download button.

Here with attahed the source code , please let know the code should add here.

Please correct the source the code here.What are the changes want to do here ?
Attachments:
 
csv servlet source code
 

Answer : How to get download csv file using java servlet ?

Try the following:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
   protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        String connectionURL = "jdbc:mysql://localhost/csv";
        String url = request.getParameter("WEB_URL");
        Statement stmt = null;
        Connection con = null;

        try {
            String filename = "SearchCases.csv";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(connectionURL, "root", "root");
            stmt = con.createStatement();

            String qry = "select * from user_managecase";
            ResultSet rst = stmt.executeQuery(qry);
            response.setContentType("text/csv");
            response.setHeader("Content-disposition", "attachment; filename=" + filename);
	    resultSetToCsv(rst, response.getWriter());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.close();
        }
    }
Random Solutions  
 
programming4us programming4us