how to create text file from jsp and provide downlode option

how to create text file from jsp and provide downlode option

HI Deepak ,

thanks for your post u really doing great work for people who is new in java/j2ee tech.

my question is who do i make a jsp output in the form of text file so that pepole can downlode that text file or may be some other file formate

this my jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.sql., java.util."%> <% Connection con = null; Statement st = null; ResultSet rs = null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:LeaveMgmtSys"); st = con.createStatement(); rs = st.executeQuery("SELECT e.EMPID, e.EMPNAME, e.PROJECTID, r.ROLEID, r.PERMISSION FROM ROLE AS r INNER JOIN EMPLOYEEMASTER AS e ON r.ROLEID=e.ROLEID WHERE (((r.PERMISSION)='RoleApproved'))"); // close all the connections.

%> View Roles

    <table width="100%" border="0">
        <tr bgcolor="royalblue">
            <td>
                <center>
                    <font color="white">View Delegation</font>
                </center>
            </td>
        </tr>
        </table>
                <table width="100%" border="1">
        <tr>
                <td align="right"><a href="role.jsp" style ="text-decoration:none;"><font size="2" color="red"> Back</font></a>&nbsp;
                <a href="Home.jsp" style ="text-decoration:none;"><font size="2" color="red">Home</font></a>&nbsp;
                <a href="Logout.jsp" onclick="fun()" style ="text-decoration:none;"> <font size="2" color="red"> Logout</font></a>&nbsp;
                </td>
        </tr>
        </table><br>
<center>
    <TABLE BORDER="10" CELLPADDING="6" CELLSPACING="2" WIDTH="70%">
        <tr bgcolor="royalblue">
            <td><b>Employee ID</b></td>
            <td><b>Employee Name</b></td>
            <td><b>Project ID</b></td>
            <td><b>Role ID</b></td>
            <td><b>Permission</b></td>
        </tr>
<%while (rs.next()) {%>
        <tr >
            <td><font size="2"><%=rs.getString(1)%></font></td>
            <td><font size="2"><%=rs.getString(2)%></font></td>
            <td><font size="2"><%=rs.getString(3)%></font></td>
            <td><font size="2"><%=rs.getString(4)%></font></td>
            <td><font size="2"><%=rs.getString(5)%></font></td>
        </tr>
<% }%>
<% %>


    <%

    try{
        if(rs != null){
            rs.close();
        }
        if(st != null){
            st.close();
        }
        if(con != null){
            con.close();
        }
    }
    catch(Exception e2){
        out.println("Unable to close connection: "+e2.getMessage());
    }%>

    </table>
    <a href="/documents/large_document.pdf">Download the  document</a>
    </center>

</body>

i am using msaccess data base..........i need output of this page in text file and also downlode option of this text field

thanks in advance

View Answers

October 22, 2010 at 3:55 PM

Hi Friend,

If you want to put the output in text file then try the following code:

1)data.jsp:

<%@ page import="java.sql.*, java.io.*"%>
<% Connection con = null;
Statement st = null;
ResultSet rs = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:access");
st = con.createStatement();
rs = st.executeQuery("SELECT * from Employee"); 
%> 
        <script type="text/javascript">
        function fun(){
            alert("You have Successfully Loggedout");
        }
        </script>
     <table width="100%" border="0">
        <tr bgcolor="royalblue">
            <td>
                <center>
                    <font color="white">View Delegation</font>
                </center>
            </td>
        </tr>
        </table>
                <table width="100%" border="1">
        <tr>
                <td align="right"><a href="role.jsp" style ="text-decoration:none;"><font size="2" color="red"> Back</font></a> 
                <a href="Home.jsp" style ="text-decoration:none;"><font size="2" color="red">Home</font></a> 
                <a href="Logout.jsp" onclick="fun()" style ="text-decoration:none;"> <font size="2" color="red"> Logout</font></a> 
                </td>
        </tr>
        </table><br>
    <center>
    <TABLE BORDER="10" CELLPADDING="6" CELLSPACING="2" WIDTH="70%">
        <tr bgcolor="royalblue">
            <td><b>Employee ID</b></td>
            <td><b>Employee Name</b></td>
            <td><b>Employee Address ID</b></td>
            <td><b>Salary</b></td>

            </tr>
<%
String f="C:/data.txt";
FileWriter writer=new FileWriter(f,true);
BufferedWriter bw=new BufferedWriter(writer);
while (rs.next()){
    String st1=rs.getString(1);
String st2=rs.getString(2);
String st3=rs.getString(3);
String st4=rs.getString(4);
bw.write(st1+" "+st2+" "+st3+" "+st4);
bw.newLine();
bw.newLine();
    %>        <tr >
            <td><font size="2"><%=st1%></font></td>
            <td><font size="2"><%=st2%></font></td>
            <td><font size="2"><%=st3%></font></td>
            <td><font size="2"><%=st4%></font></td>
        </tr>

<% 
}
bw.close();
System.out.println(f);
%>
    </TABLE>
    <a href="http://localhost:8080/examples/modified/download.jsp?ff=<%=f%>">Download the  document</a>
    </center>

October 22, 2010 at 3:56 PM

continue..

2)download.jsp:

<%@ page import="java.util.*,java.io.*"%>
<%@ page import="java.net.*"%>
<%!
public static String getMimeType(String fileUrl)
    throws java.io.IOException, MalformedURLException 
  {
    String type = null;
    URL u = new URL(fileUrl);
    URLConnection uc = null;
    uc = u.openConnection();
    type = uc.getContentType();
    return type;
  }

%>
<%
    String file=request.getParameter("ff");
    File f = new File (file);
    String filename=f.getName();
    String type=getMimeType("file:"+file);

    response.setContentType (type);
    response.setHeader ("Content-Disposition", "attachment;     filename=\""+filename+"\"");

    String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
    InputStream in = new FileInputStream(f);
        ServletOutputStream outs = response.getOutputStream();

        int bit = 256;
        int i = 0;
            try {
                    while ((bit) >= 0) {
                        bit = in.read();
                        outs.write(bit);
                    }
                        } catch (IOException ioe) {
                        ioe.printStackTrace(System.out);
                    }
                        outs.flush();
                    outs.close();
                    in.close(); 
            %>

Thanks


October 22, 2010 at 4:13 PM

Hi Friend,

There are different ways to write data into different type of files. So if you want to put output into pdf file. then try the following code:

data.jsp:

<%@ page import="java.sql.*, java.io.*"%>
<%@page import="com.lowagie.text.*,com.lowagie.text.pdf.*"%>;
<% Connection con = null;
Statement st = null;
ResultSet rs = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:access");
st = con.createStatement();
rs = st.executeQuery("SELECT * from Employee"); 
%> 
        <script type="text/javascript">
        function fun(){
            alert("You have Successfully Loggedout");
        }
        </script>
     <table width="100%" border="0">
        <tr bgcolor="royalblue">
            <td>
                <center>
                    <font color="white">View Delegation</font>
                </center>
            </td>
        </tr>
        </table>
                <table width="100%" border="1">
        <tr>
                <td align="right"><a href="role.jsp" style ="text-decoration:none;"><font size="2" color="red"> Back</font></a> 
                <a href="Home.jsp" style ="text-decoration:none;"><font size="2" color="red">Home</font></a> 
                <a href="Logout.jsp" onclick="fun()" style ="text-decoration:none;"> <font size="2" color="red"> Logout</font></a> 
                </td>
        </tr>
        </table><br>
    <center>
    <TABLE BORDER="10" CELLPADDING="6" CELLSPACING="2" WIDTH="70%">
        <tr bgcolor="royalblue">
            <td><b>Employee ID</b></td>
            <td><b>Employee Name</b></td>
            <td><b>Employee Address ID</b></td>
            <td><b>Salary</b></td>

            </tr>
<%

Document document = new Document(PageSize.LETTER.rotate());
String f="C:/table.pdf";
PdfWriter.getInstance(document, new FileOutputStream(new File(f)));
document.open();

String[] headers = new String[] {"Emp ID", "Emp Name", "Emp Address", "Salary"};
PdfPTable table = new PdfPTable(headers.length);
            for (int i = 0; i < headers.length; i++) {
                String header = headers[i];
                PdfPCell cell = new PdfPCell();
                cell.setGrayFill(0.9f);
                cell.setPhrase(new Phrase(header.toUpperCase(), new Font(Font.HELVETICA, 10, Font.BOLD)));
                table.addCell(cell);
            }
            table.completeRow();
while (rs.next()){
    String st1=rs.getString(1);
String st2=rs.getString(2);
String st3=rs.getString(3);
String st4=rs.getString(4);
table.addCell(st1);
table.addCell(st2);
table.addCell(st3);
table.addCell(st4);
table.completeRow();
    %>        <tr >
            <td><font size="2"><%=st1%></font></td>
            <td><font size="2"><%=st2%></font></td>
            <td><font size="2"><%=st3%></font></td>
            <td><font size="2"><%=st4%></font></td>
        </tr>

<% 
}
document.add(table);
document.close();
System.out.println(f);
%>
    </TABLE>
    <a href="http://localhost:8080/examples/modified/download.jsp?ff=<%=f%>">Download the  document</a>
    </center>

The code of download.jsp file remains same.

Thanks


May 4, 2011 at 1:09 PM

Thanks dude!! but m facing a problem while doing this... the code is running fine... View contents is also fine...but the contents of data.txt is not getting refreshed if i try to run the program twice. It is adding new contents to the old one.. can u suggest something for this problem? Thanks









Related Tutorials/Questions & Answers:
how to create text file from jsp and provide downlode option
how to create text file from jsp and provide downlode option  HI... file so that pepole can downlode that text file or may be some other file... data base..........i need output of this page in text file and also downlode
how to create xls file and give download option using jsp/servlet?
how to create xls file and give download option using jsp/servlet?  Hi, how to create file and give download option to user,so that the user can save the file on defined path
Advertisements
Create text file at client's directory from server.
Create text file at client's directory from server.  Need java code to create text file at client's directory from server..... Please Help
Read Text file from Javascript - JSP-Servlet
Read Text file from Javascript  plz send the code How to Retrieve the data from .txt file thru Javascript? And how to find the perticular words in that file
create login page using data from text file
create login page using data from text file  I want to create login page using data store in textfile(data submit from regiter page to textfile) using jsp and servlet. Thanks
how to match the key word from a text file
how to match the key word from a text file  p>Hi all, I have the code to match the key word and from the text. I have input like this reader.txt... want to get the value from the called file and get the result. String regex1
How to create a .mdf file from script (.sql)
How to create a .mdf file from script (.sql)  Hi, I have a sql database in 2008 server. now i want it to convert to 2005. so i script it. just help me to create a .mdf file back from the script
How to create file from input values in Jframe ?
How to create file from input values in Jframe ?  hi i m doing my project using java desktop application in netbeans.i designed a form to get... to take these details and make a file that can be appended each time.how
select option with text facility - JSP-Servlet
select option with text facility  Hello. My doubt is regarding how to provide facility to add text in a drop down box ie(select tag in html )in a jsp... the select in html and if the user wants also to directly type the option instead
How to write .properties file from jsp
How to write .properties file from jsp  Hi i new to java i stuck here... to implement/write .properties file from jsp. Regards, Venkatesh Gurram.ADS... is when i am using this in jsp means i have create object to this class and used
how to display data from jsp file into database
how to display data from jsp file into database  this is a jsp file...(); in the below example. the error is "cannot convert from java.sql.Statement..." contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page
How to create one xml file from existing xml file's body?
How to create one xml file from existing xml file's body?  Hi, i'm working with content optimization system.I want to know how we can take all data from an xml doc's body to develope another xml with that content.I'm using JDOm
how to display image and text in single jsp page from the mysql database
how to display image and text in single jsp page from the mysql database  hello please help me to display the image and text in single jsp page from mysql database if have any reference code please send me Thanks in advance
How to create a zip file and how to compare zip size with normal text file
How to create a zip file and how to compare zip size with normal text file  Hi, how are you?I hope you are fine.I want program like how to create zip file based on one text file.i saw the code in the below link which was provided
How to read and display data from a .properties file from a jsp page
How to read and display data from a .properties file from a jsp page  I have a .properties file. I have to create a jsp page such that it reads the data from this .properties file and display it in table format. Ex:by using
How to get data from DB in to Text box by using Jsp & Ajax
How to get data from DB in to Text box by using Jsp & Ajax   I want to get the data from database in to text box in a jsp page by using Ajax. If I... with a and from that i need to select the required value and i should store
how to create a bar chart in jsp by fetching value from oracle databse?
how to create a bar chart in jsp by fetching value from oracle databse?  i want to show the population of various states in a bar chart in my jsp page by fetching the data from my oracle table. i am using my eclipse as my IDE
how to generate the pdf file with scroolbar from jsp age - JSP-Servlet
how to generate the pdf file with scroolbar from jsp age  How to generate the pdf file with scroolbar from jsp.i am not able to see the all the columns in pdf file now .it is very urgent for me plz help
How to create file and save it into user defined path using jsp/servlet?
How to create file and save it into user defined path using jsp/servlet?  Hi.. Onclick event I have created one file.When file will create it should asked where to save file(like browse option
How to make a list from file text in J2ME - Java Beginners
How to make a list from file text in J2ME  I was trying to make a method that read file from text and make a list of it, I have tried ReadHelpText...) { } return null; } kamus.txt file contains: iconSebuah lambang kecil berupa gambar
How to extract a specific line from a text file? - IoC
How to extract a specific line from a text file?  Hi all, i'm trying to write a code that return a specific line from a text file. so my first... 100 to 120 for example. My Text file contain numbers and words. How i have
how to display data from mysql table in text box using jsp??
how to display data from mysql table in text box using jsp??  <p>hi, i have a written a code to display data from a mysql table into txtboxes... at line: 113 in the jsp file: /Cat1.jsp The local variable v_RGPC may not have
how to create a php script to download file from database mysql
how to create a php script to download file from database mysql  how to create a php script to download file from databse mysql
how to create a php script to download file from database mysql
how to create a php script to download file from database mysql  how to create a php script to download file from databse mysql
How to create a new text file that contains the file names of the copied files in a new directory? - Java Beginners
How to create a new text file that contains the file names of the copied files in a new directory?  Okay so my question is: How to create a new text... text file that has the file names of those files that I copied in a list
JSP Open File
;The Tutorial illustrate an example from JSP Open File. To understand the example we create... JSP Open File          JSP Open File is used to create a file and write
a jsp code for creating a text file
a jsp code for creating a text file  Hello,i need jsp code for creating a new text file for each user, when they login in to the website for creating a new data file. So i need a jsp code for following options. when user login
How to provide navigation in quiz application using jsp?
How to provide navigation in quiz application using jsp?  hi everyone. i am using jsp with mysql connectivity. i have to retrieve questions from db one at a time. on clicking the next it should go the next question
How to backup a selected file from the dropDown Menu in JSP?
How to backup a selected file from the dropDown Menu in JSP?  I am trying to create a dropdown menu list for some type of files contained ina... file into the backup directory. I need the jsp code that can generate
Read Lines from text file
read from the text file and displays the output as desired. Unable to read the rest...Read Lines from text file  Here's a brief desc of what my Java code does .. I'm using BufferedReader to read lines from a text files and split each
Create JSP file
Create a JSP file To create a JSP file for struts, Some special (Struts specific) tags are used. And to use those tags in JSP you need to import there tag... for StrutsPrepareAndExecuteFilter in web.xml file. as <filter> <filter-name>struts2
how read data from doc file in same formate in jsp
how read data from doc file in same formate in jsp  how we can read and display data on jsp page, from doc file with the same formatting
How to build .XML file, please provide an example.
How to build .XML file, please provide an example.  Hello, I want to know how to build .XML file. If you can provide an example, that would be great... Thanks
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload  Dear Sir, My issue is: How display a image on servlet from file upload I receive your answer today...: Unknown File Type From: localhost button - Find, Save, Cancel after clicking
how to save an input to jsp page in a text file?ave an input given b
how to save an input to jsp page in a text file?ave an input given b  how to save the input given by the user in jsp page to a text file
How display a Image on servlet from file upload - JSP-Servlet
How display a Image on servlet from file upload  Dear Sir, I were ask a question that How display the Image on servlet through file upload. Today I... both jar file commons-fileupload-1.2.1.jar and commons-io-1.4.jar and set
how to prevent no from unroundin off on clicking text box again in JSP and Jquery
how to prevent no from unroundin off on clicking text box again in JSP and Jquery  I am rounding off numbers entered in a text box in jsp but when I... on clicking outside It rounds off again, How can i prevent it from un-rounding off
Java to create table in jsp file that include other jsp file
Java to create table in jsp file that include other jsp file  String jspContent = "table" += "tr" += "td" += "jsp:include page='fileSource... Please Provide me the solution (or example
how to update the text file?
how to update the text file?  if my text file contains a string and integer in each line say,: aaa 200 bbb 500 ccc 400 i need a java code to update the integer value if my input String matches with the string in file. please
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
How display a Image on Servlet from file upload - JSP-Servlet
How display a Image on Servlet from file upload  Dear Sir, My requirement is I want to display a Image on Servlet from File Upload...); // Create a new file upload handler System.out.println(isMultipart
how to create using jsp
how to create using jsp  code 1: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>...;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
how to update the text file?
how to update the text file?  my textfile with name list.txt: Rice... i want to update the quantity in my text file, if the item i entered matches...=new float[n1]; for(int i=0;i File file ; FileReader fr=null
I need jsp code for how to write text field value into property file.
I need jsp code for how to write text field value into property file.  Hi , I need to set the text field value into property file using jsp code. Example : Username : Valar , I have entered the value "Valar" in the text field
How to save run time created text-file on a disk using jsp/servlet?
How to save run time created text-file on a disk using jsp/servlet?  I have a JSP page with save button.In that I created the file(e.g a.txt) at run-time,when I click on the save button,it will ask where to save the file
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload   Dear Sir, My Question is: How display a image on servlet from file upload Your Answer: Hi... that an attempt to open the file denoted by a specified pathname has failed
How to Create JSP Page
How to Create JSP Page       In this section we will show you how you can create JSP page and then test... can be used. In this example I will show you how to create a simple JSP page
Java search word from text file
Java search word from text file In this tutorial, you will learn how to search a word from text file and display data related to that word. Here, we have created a text file student.txt which consists of id, name and marks of few

Ads