Solved

how to return a file to jsp?

Posted on 2006-11-22
8
951 Views
Last Modified: 2008-01-09
hello and happy holidays!

I have a java function in a normal javabean that returns a File object:  public File getSomeFile() {}

The file that's returned by getSomeFile() is a pdf.  

My question is i want to display this pdf on the front end jsp:
1) user clicks on a link
2) the jsp calls the javabean getSomeFile() function which in turn returns the pdf file
3) a popup window opens with the pdf file displayed

how do i do this? thanks!





0
Comment
Question by:gagaliya
8 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17997589
you need to add the following:

response.setContentType("application/pdf");
response.setHeader("Content-Disposition: ", "attachment; filename=" + filename); //filename is the name of the file.
OutputStream out = response.getOutputStream();
InputStream in = new FIleInputStream(filename); // can be replaced with other ways to create InputStream from file.
try{
    byte[] temp = new byte[2048];
    int read = in.read(temp);
    while(read>0) {
        out.write(temp, 0, read);
        read = in.read(temp);
    }
}finally {
    in.close();
}
0
 
LVL 1

Author Comment

by:gagaliya
ID: 17998020
hi ajay, can you tell me the workflow on where to add these code?  i assume that's in a servlet.  how do i pass the javabean function to a servlet then have the servlet return to the jsp?  below is what i have so far, can you please connect the dots on where to add your code and how to connect them:

javabean.java  
webpage.jsp


thank you
0
 
LVL 12

Accepted Solution

by:
enachemc earned 125 total points
ID: 17998855
You would need to make a servlet to serve the PDF. In the JSP page add a link to the PDF displaying servlet that will display the PDF as you want (for example open o popup having as source the PDF displaying servlet). In the JSP, put the file object in the session.

In the servlet, take the file from session. From this file, send the necessary content type headers (pdf in your case). Now just open the output (not writer) and stream the file to the user.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 6

Assisted Solution

by:SamsonChung
SamsonChung earned 125 total points
ID: 17999306
you need two pages,

your jsp, and another new jsp, where all it does is return the pdf file.

then you can easily put an iframe in your initial jsp and have it load the returning pdf jsp file in the iframe.

that way you can have full control and have it on the fly.

0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 125 total points
ID: 18000444
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 125 total points
ID: 18001458
> how do i pass the javabean function to a servlet then have the servlet return to the jsp?
call the javabean function from servlet. This logic should be there on the servlet.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now