Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to return a file to jsp?

Posted on 2006-11-22
8
Medium Priority
?
993 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Assisted Solution

by:SamsonChung
SamsonChung earned 500 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 500 total points
ID: 18000444
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

598 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