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?
how do i use sql like query in my jsp page
Advertisements
how i open my web page
Show pdf's in web browser by using jsp?
how do i upload a file by using servlet or jsp?
How do i retain values in the drop down - Struts
How to export web page to excel using java or jsp or servlets
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
how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers
How do i get the number of online users from a site using jsp?
How to provide navigation in quiz application using jsp?
generate pdf using jsp
How do I launch my career as Data Analyst?
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 to set the pdfptable as pdf page header when generating the pdf from jsp page. - JSP-Servlet
How to print contents of a web page in jsp?
how to generate PDF file using JSP with net beans
How to launch my web application from my desktop without opening Netbeans IDE everytime i run the application?
Add music to my web page
how do i make a phone call from my app without quitting my application
I want to change my user name on your website ,how can i do this
How do i slow down the game (othello game)??
how to create web page on jsp?
how i can add an horizontal scrollbar at my PdfAnnotation ? - JSP-Servlet
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 report from jsp

Ads