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









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?
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
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 get the number of online users from a site using jsp?
How to export web page to excel using java or jsp or servlets
How do i retain values in the drop down - Struts
How to provide navigation in quiz application using jsp?
How I can filling pdf file that crated by livecycle using itext
How do I install urllib2 library in my Python module
How to set the pdfptable as pdf page header when generating the pdf from jsp page. - JSP-Servlet
how to generate PDF file using JSP with net beans
how can i add wirecard option(or button ) at my web page plz tell all steps? View Answers
How do I launch my career as Data Analyst?
How do I change the while loop in this code to the range with range list style display page for a resultSet() in jsp?
can i remopve a jsp web page - JSP-Interview Questions
How to generate pdf file on click of the links using jsp..
how i do url encoding process - JSP-Servlet
i want to do a project using jsp and servlet....What are all the materials i need to study
How to print contents of a web page in jsp?
How to insert a single page PDF file into another multipage PDF file using iText? - Java Beginners
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 generate the pdf report from jsp
how do i make a phone call from my app without quitting my application
I want to put user guide in pdf and word in side WEB-INF and need to give link on the home page
How to generate the pdf file using jsp - JSP-Servlet
how i can add an horizontal scrollbar at my PdfAnnotation ? - JSP-Servlet
linking jsp with database using classes and methods and then access them all in to my jsp page - JSP-Servlet
how to add the scrollbar to the pdf page when generating the pdf file from jsp - JSP-Servlet
how to create web page on jsp?
How do i start to create Download Manager in Java - JSP-Servlet
How to launch my web application from my desktop without opening Netbeans IDE everytime i run the application?
How i can send testing mail on my id using java?
How do i validate form using javascript and send data to database?
Add music to my web page
How save,get picture from database in my jsp page?
How to design https connection page using JSP
May I know how to create a web page?
how do i update my database with the help of update syntax in html <text/javascript>? How to write 'where' statement in this?
how can i display a pdf file in a jtextarea
How to add google page in my webpage using jquery ajax.
How to provide timeout condition in my project

Ads