?
Solved

how to return a file to jsp?

Posted on 2006-11-22
8
Medium Priority
?
990 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…
Suggested Courses

777 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