Home Servlets Export data into CSV File using Servlet

Related Tutorials


 
 

Share on Google+Share on Google+

Export data into CSV File using Servlet

Advertisement
In this section, you will learn how to Export data into CSV file using Servlet. We have created file "JdbcCsvFile.java" to export data from this .java file..

Export data into CSV File using Servlet

     

In this section, you will learn how to Export data into  CSV file using Servlet. We have created  file "JdbcCsvFile.java" to export data from this .java file..

Brief description for the flow of application :

A CSV file is commonly known as a Comma Delimited File or a Character Separated File. It is a text file that has a specific format which allows saving of text in organized manner. This format known as a flat table, is very simple. Each row contains one record of information. The character used to distinguish each piece of data within each record is most commonly used a comma ",".

*)Create a folder "csv" in the C:\ Drive:.
*)Create a file "CsvFile.java" that creates a CSV file "myfile.csv".
*)Download the "Tomcat apache Server With Latest version" and run with the URL
   http://localhost:8080/servletcsv/csvFile.


Step 1: Create a Servlet "CsvFile.java"

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CsvFile extends HttpServlet { 
public void doGet (HttpServletRequest request,
    HttpServletResponse response) 
throws ServletException,IOException  {
try
{
  PrintWriter out = response.getWriter();
  String filename = "c:\\csv\\myfile.csv";
  FileWriter fw = new FileWriter(filename);
 
  fw.append("Employee Code");
  fw.append(',');
  fw.append("Employee Name");
  fw.append(',');
  fw.append("Employee Address");
  fw.append(',');
  fw.append("Employee Phone");
  fw.append(',');
  fw.append("Employee ZipCode");
  fw.append('\n');

  fw.append("E1");
  fw.append(',');
  fw.append("Vineet");
  fw.append(',');
  fw.append("Delhi");
  fw.append(',');
  fw.append("224277488");
  fw.append(',');
  fw.append("110085");
  fw.append('\n');

  fw.append("E2");
  fw.append(',');
  fw.append("Amar");
  fw.append(',');
  fw.append("Delhi");
  fw.append(',');
  fw.append("257765758");
  fw.append(',');
  fw.append("110001");
  fw.append('\n');

  fw.append("E3");
  fw.append(',');
  fw.append("Amit");
  fw.append(',');
  fw.append("Delhi");
  fw.append(',');
  fw.append("257685858");
  fw.append(',');
  fw.append("110005");
  fw.append('\n');

  fw.append("E4");
  fw.append(',');
  fw.append("Suman");
  fw.append(',');
  fw.append("Delhi");
  fw.append(',');
  fw.append("266447678");
  fw.append(',');
  fw.append("110081");
  fw.append('\n');
 
  
  fw.flush();
  fw.close();
  out.println("<b>Csv file Successfully created.</b>");


catch (Exception ex) {
ex.printStackTrace ();
}
}
}

 

Step 2:Create a "Web.xml" for mapping the Servlet "CsvFile.java". 

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>csvfile</servlet-name>
<servlet-class>CsvFile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>csvfile</servlet-name>
<url-pattern>/csvFile</url-pattern>
</servlet-mapping>
</web-app>


Output :


"myfile.csv"

Download Source Code

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 26, 2008

Related Tutorials

Discuss: Export data into CSV File using Servlet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
kumar
July 7, 2011
Export data in to multiple csv files

How can I export in to multiple csv files
Anthony Raj S
December 27, 2011
export data to csv

How to make the csv file available for download like pdf?
Anthony Raj S
December 27, 2011
export data to csv

How to make the csv file available for download like pdf?
Andreza
August 25, 2012
obryDtnFYYSNz

Your best bet is to store the data on a host in whatever dasaabte you want to use and then access it remotely. If FatCow won't allow remote access then find a host that does and move to that one. Many do so instead of being forced to be locked out of your own data find someone new. The type of programming you use won't make any difference.I don't recommend hosts since I sound like a shill for them. Suffice it to say there are many that will allow your client app to access the db remotely. Confirm with their tech support before you buy.I don't even consider a host unless that's an option.
DMCA.com