File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Open a new window programmatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Open a new window programmatically" Watch "Open a new window programmatically" New topic

Open a new window programmatically

D. Formenton
Ranch Hand

Joined: Mar 20, 2008
Posts: 48
I need to display a file. pdf in a new window (or, rather, in a new tab), other than that used by the application.

The following function shows the file correctly, but unfortunately overwrite the main application window.

What changes should I make to the code to make the pdf file opens in a new window?

Thank you.

John Burton

Joined: Mar 26, 2012
Posts: 5
You could change the target of the command link you are calling that code from. I think "_blank" will open a new window.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

You should not attempt to use JSF to render pages that are not in HTML format (such as PDFs and Excel spreadsheets). Use a servlet to render the PDF.

Normally, thanks to the Eolas lawsuit, an attempt to open a PDF in a Microsoft Internet Explorer window will instead result in a new, non-IE window being opened, so unless you're using a non-Microsoft browser, if it replaced what was in your browser window, what you got wasn't a true PDF, it was PDF corrupted by the things JSF does because it thought you were attempting to output an HTML web page.

To reliably open a PDF in an external window, output the PDF from a plain-vanilla servlet and set the "target" attribute on the commandLink of the JSF form that provides the servlet URL to fetch/create the PDF and set your Content-Disposition header appropriately. Note that "target" is not an option on the JSF commandButton, so if you want a "button", tart up a commandLink with suitable button-looking graphics and CSS settings.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Open a new window programmatically
It's not a secret anymore!