Solved

how to return a file to jsp?

Posted on 2006-11-22
8
971 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 18
thymeleaf natural templating vs JSP 2 95
difference of if loops 23 49
Windows 10 IE Certificate Issue 10 45
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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

816 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

8 Experts available now in Live!

Get 1:1 Help Now