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.
%>
<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>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
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>
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
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
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