Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

JSF Interview Questions and Answers

Ques. How to download PDF file with JSF?
Ans. This is an code example how it can be done with action listener of the backing bean.

Add the following method to the backing bean:


public void viewPdf(ActionEvent event) {
String filename = "filename.pdf";

// use your own method that reads file to the byte array
byte[] pdf = getTheContentOfTheFile(filename);

FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();

response.setContentType("application/pdf");
response.setContentLength(pdf.length);
response.setHeader( "Content-disposition", "inline; filename=\""+fileName+"\"");
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(pdf);
} catch (IOException e) {
e.printStackTrace();
}
faces.responseComplete();
}
This is a jsp file snippet:

<h:commandButton immediate="true" actionListener="#{backingBean.viewPdf}" value="Read PDF" />
Is it helpful? Yes No

Most helpful rated by users:

©2020 WithoutBook