Home Answers Viewqa Java-Beginners how to create text file from jsp and provide downlode option

 
 


Neo Java Developer
how to create text file from jsp and provide downlode option
4 Answer(s)      2 years and 8 months ago
Posted in : Java Beginners

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 Pages:
how to create text file from jsp and provide downlode option
how to create text file from jsp and provide downlode option  HI... of this page in text file and also downlode option of this text field thanks... file so that pepole can downlode that text file or may be some other file formate
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
jsp drop down-- select Option
jsp drop down-- select Option  how to get drop down populated dynamically   Hi Friend, Create table country(country_id,country...;nbsp;</td> <select name="sel"><option value=""><---Select
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
Javascript get Date And Selected Option Text
explains you that how to display the date and the selected option text in Javascript...Javascript get Date And Selected Option Text In this tutorial we will learn about how to get Date from the textbox and displayed as Date as well as how
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
select option value
; Here is an example of dependent dropdown box in JSP. The values come from...("Select * from country"); while(rs.next()){ %> <option value...select option value  if i select a value of any drop down
select option value
select option value  if i select a value of any drop down then that value should be used in a select query of the next dropdown in jsp on same page????   Here is an example of dependent dropdown box in JSP. The values
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
To provide Help Option - Java Beginners
To provide Help Option  hi i am writing one small application there i wanted to provide "Help" button to user.From there they will be geting how... theire own help article writen..." how we can provide our Help writen for our
how do i provide down a pdf document fecility on my web page using jsp and servlets?
how do i provide down a pdf document fecility on my web page using jsp and servlets?  Hai, I need a program to provide download option for pdf file on my web page,the pdf file contains retrieved data from mysql table. I need
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 to read text file in Servlets
Create a Table in Mysql database through SQL Query in JSP...; This is detailed java code to connect a jsp page to mysql database and create a table of given.... First page is to provide link to create table and the next page for processing
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
display uploaded file option in editable form
display uploaded file option in editable form  how to show the uploaded file in editable mode on the jsp in struts2.0
display uploaded file option in editable form
display uploaded file option in editable form  how to show the uploaded file in editable mode on the jsp in struts2.0
How to create XML file - XML
How to create XML file  Creating a XML file, need an example. Thanks!!  To create XML file you need plain text editor like note pad. After... language has many API's to create xml file from program.Here is the code example
How to create XML from Swings
How to create XML from Swings  How to create XML using Swings. I have a Swing GUI and capturing all data from it.When i click on submit, an xml...; Here is a code that accepts the data from the user through swing
JSP Open File
an example from JSP Open File. To understand the example we create openFile.jsp... JSP Open File          JSP Open File is used to create a file and write
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 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... is when i am using this in jsp means i have create object to this class and used
How to read text file in Servlets
How to read text file in Servlets       This section illustrates you how to read text... from a file. Create a file message.properties in the /WEB-INF/ directory
How to make an image full text.
option to make an image full text so follow now. Open a file: First open... How to make an image full text.       Feel relief from color text, it's not tuff to design
how to download a file from a folder??
how to download a file from a folder??  i can upload any kind of files to a folder, and only the path of the file is saved into the database, now how a client can download the file from my folder. pls provide me the jsp code
Example of printing Text message passed from XML to JSP
are going to know how we can pass a text message from XML to JSP.  ... Example of printing Text message passed from XML to JSP... with a JSP page. This tutorial is only geared towards showing how to construct a Java
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
Write Text File to Table
the simple text file. For this, you must have to create a simple text file that have... Write Text File to Table     ... the records of a simple text file and write (insert) into a simple table in MySQL
JSP File
JSP File  Hi, What is JSP File? How to create JSP file? Thanks   Hi, JSP file is simple text file with .jsp extenstion. You can run JSP file on tomcat server. Read more at JSP tutorials section. 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 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
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 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
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text... to another text file. When that is done the output values of that file need...); if (returnVal == JFileChooser.APPROVE_OPTION) { File inFile
How to add download option for openwith and saveAs on exporting file to excel in servlet
How to add download option for openwith and saveAs on exporting file to excel... my requirement I need to give download option for openwith and saveAs from... data from database into .excel formate in which i have given hard coded path
How to add download option for openwith and saveAs on exporting file to excel in servlet
How to add download option for openwith and saveAs on exporting file to excel... my requirement I need to give download option for openwith and saveAs from... data from database into .excel formate in which i have given hard coded path
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 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
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 get the next option - Java Beginners
how to get the next option  i was getting values from the database it was bulk so i want to keep the next option how to do in the jsp  ... the following link: http://www.roseindia.net/jsp/navigation-database-table.shtml
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
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
how to create own tld and use it in jspx file
how to create own tld and use it in jspx file  I wanted to add fmt... Author : Administrator --> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> <jsp:directive.page contentType="text/html
how to create own tld and use it in jspx file
how to create own tld and use it in jspx file  I wanted to add fmt... Author : Administrator --> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> <jsp:directive.page contentType="text/html
How to create pdf file in struts2.
How to create pdf file in struts2. In this example, you will see the how to create PDF in struts, and how to write message in generated PDF file in struts2.  For this, we requires a jar file iText.jar. It is used in java
Block edit menu option - JSP-Servlet
Block edit menu option  I am doing a jsp project. I have to control number of characters in a text area upto a limit. How can i block pasting through edit menu and pasting through short cut key [ctrl+v
Jsp Option Value
Jsp Option Value          In this section, we are going to create a select box by retrieving the value from the database in jsp. For further processing
populating text box using jsp code
populating text box using jsp code  Sir, How to populate related values in a text box after selecting value from drop down list using JSP and mysql...].text; window.location.replace("http://localhost:8080/examples/jsp
Create and Save Excel File in JSP
Create and Save Excel File in JSP  ... and saving Excel file from JSP application. In this example we are going... we have to import the necessary packages to create the Excel sheet from jsp 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