Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet how do i provide down a pdf document fecility on my web page using jsp and servlets?

Have Programming Question? Ask it here!
 
 
 


sridivya pathaneni
how do i provide down a pdf document fecility on my web page using jsp and servlets?
1 Answer(s)      3 years and 4 months ago
Posted in : JSP-Servlet


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 this program by using jsp-servlets.

any one can help me please??

Thanks&Regards Divya


Advertisement
View Answers

May 13, 2011 at 3:37 PM


1)createpdf.jsp:

<%@page import=" java.io.*"%>
<%@page import=" java.sql.*"%>
<%@page import=" com.lowagie.text.*"%>
<%@page import=" com.lowagie.text.pdf.*"%>
<%
String file="C:/new.pdf";
       Document document=new Document();
       PdfWriter.getInstance(document,new FileOutputStream(file));
       document.open();
       PdfPTable table=new PdfPTable(2);
       table.addCell("Name");
       table.addCell("Address");

       Class.forName("com.mysql.jdbc.Driver");
       Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
       Statement st=con.createStatement();
       ResultSet rs=st.executeQuery("Select * from data");
       while(rs.next()){

       table.addCell(rs.getString("name"));
       table.addCell(rs.getString("address"));
       }
       document.add(table);
       document.close();

%>
<a href="download.jsp?f=<%=file%>">Download File<a>

2)download.jsp:

<%@page import="java.io.*"%>
<%
String ff=request.getParameter("f");
File f = new File(ff);
    String filename=f.getName();
    response.setContentType("application/pdf");
    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(); 
                    %>


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com