Solved

how to return a file to jsp?

Posted on 2006-11-22
8
987 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

726 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