Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8766871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:39:25+00:00 2026-06-13T16:39:25+00:00

The reason may be the the short of knowledge in java :( ,so I’m

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:39:26+00:00Added an answer on June 13, 2026 at 4:39 pm

    here is a class you can using to export to CSV:

    import java.io.FileWriter;
    import java.io.IOException;
    import User;
    
     public class GenerateCsv
     {
    
    
        private static void generateCsvFile(ArrayList<User> users)
        {
            String output = "Email, Name\n";
    
            for (User user in users) {
                output += user.getEmail() + ", " + user.getName() + "\n";
            }
    
            return output;
        }
     }
    

    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

    public class User {
    
        String name;
        String email;
    
        public static ArrayList<User> getAllUsers() {
            // returns all users
            ... 
        }
    
    }
    

    Then let’s say you have a servlet called UsersServlet which get these users:

    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<User> users = Users.getAllUsers();
    
            w.prinln(GenerateCsv.generateCsvFile(users));
    
            w.flush();
            w.close();
        }
    
        public void doPost (HttpServletRequest  req, HttpServletResponse  res)
            throws ServletException, IOException {
            ...
        }
    }
    

    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:

    <a href='/getCSV' > Export CSV </a>
    

    but please note that you have to link the servlet to the url using web.xml:

    <web-app>
        <servlet>
            <servlet-name>UsersServlet</servlet-name>
            <servlet-class>__package__.UsersServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>UsersServlet</servlet-name>
        <url-pattern>getCSV</url-pattern>
        </servlet-mapping>
    </web-app>
    

    EDIT: Writing to disk instead of sending to browser

     import java.io.FileWriter;
     import java.io.IOException;
     import User;
    
     public class GenerateCsv
     {
    
    
        private static void generateCsvFile(String fileName, ArrayList<User> 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();
               } 
          }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this may be an amateur question but for some reason I
The reason I am asking this as a separate question to the multitude of
This may well be a stupid question, but here it goes anyway. In objective
This may fall under you can't, and there's no reason to anyway, but I'm
I'm trying this and for some reason is not working. I'm suspecting it may
I'm afraid this may be a very stupid question. I want to refer people
Long story short Say I have the following code: // a class like this
To preface, I think I may have figured out how to get this code
This may seem obvious to most people, but I'm just trying to confirm that
I have this small snipped of code. I don't know ruby and I think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.