how do i provide down a pdf document fecility on my web page using jsp and servlets?

Ads
 

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

any one can help me please??

Thanks&Regards Divya

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(); 
                    %>

Ads









Related Tutorials/Questions & Answers:
how do i provide down a pdf document fecility on my web page using jsp and servlets?
Show pdf's in web browser by using jsp?
Advertisements
how i open my web page
how do i upload a file by using servlet or jsp?
How to export web page to excel using java or jsp or servlets
How to provide navigation in quiz application using jsp?
How do i get the number of online users from a site using jsp?
generate pdf using jsp
how to display jsp page containing mysql query in particular division using ajax ?my code is below bt i cundt get it properly
How I can filling pdf file that crated by livecycle using itext
write data to a pdf file when i run jsp page
write data to a pdf file when i run jsp page
write data to a pdf file when i run jsp page
write data to a pdf file when i run jsp page
How do I install urllib2 library in my Python module
how to generate PDF file using JSP with net beans
how to create web page on jsp?
how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers
How to generate pdf file on click of the links using jsp..
How do I change the while loop in this code to the range with range list style display page for a resultSet() in jsp?
How to generate the pdf file using jsp - JSP-Servlet
how to generate the pdf report from jsp
How do i slow down the game (othello game)??
How do I launch my career as Data Analyst?
How to design https connection page using JSP
i want to do a project using jsp and servlet....What are all the materials i need to study
May I know how to create a web page?
I want to change my user name on your website ,how can i do this
how do i make a phone call from my app without quitting my application
How do i validate form using javascript and send data to database?
How i can send testing mail on my id using java?

Ads