Skip to content
November 27, 2012 / danilo

Call a document in a new tab

A few months ago we had a client requirement where upon some processing a document should open automatically, today we will demonstrate how to do this with a PDF document.

The ADF framework supports a listener that downloads files upon user click, but our approach is to open a file without user interaction. To do so we need to create a HTTP Servlet that opens our file, add servlet mapping in the web.xml file, and create  a taskflow example with navigation between two pages.

Step 1 – Create a HTTP Servlet

To create a new HTTP Servlet in JDeveloper choose HTTP Servlet via New Gallery, define its name and, on the next screen, define the mapping for your servlet. This will be registered on web.xml file.

A new Java class is created for the servlet but we will come back later to this. Now lets create a new taskflow for the servlet. In the property inspector window the URL Invoke field must be url-invoke-allowed, and the URL in URLView must match the pattern defined in web.xml

Step 2 – Create a taskflow example

Now lets create a taskflow to run the example. In our example we have two views with navigation and a method call, that calls our servlet, in between. Our idea is to have a button on our main page that its action is to navigate to another page, the method call is used to execute the servlet.

The Java code in our managed bean to call the servlet is as follows,

public void openDocument() {

byte[] fileInBytes = readFileToByteArray("D:\\work\\examples\\files\\document.pdf");

final String taskflowId="ShowPdfFlow";

String taskflowDocument="/WEB-INF/ShowPdfFlow.xml";
FacesContext fctx = FacesContext.getCurrentInstance();
Map<String, Object> params = new HashMap<String, Object>();
FacesContext ctx = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession)ctx.getExternalContext().getSession(false);
session.setAttribute("documentToOpen", fileInBytes);

String taskflowURL = ControllerContext.getInstance().getTaskFlowURL(false,new TaskFlowId(taskflowDocument,taskflowId), params);
ExtendedRenderKitService erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class);
StringBuilder script = new StringBuilder();
erks.addScript(FacesContext.getCurrentInstance(), script.toString());


We are reading a document to a byte[] and store it in session and this will be accessed from our servlet.

Step 3 – Changes to Servlet class

The changes to our servlet class is as follows, we are reading the byte[], for the document, from session and write it on the outputstream.


byte[] doc = (byte[])request.getSession().getAttribute("documentToOpen");

ByteArrayInputStream bais = new ByteArrayInputStream(doc);
 response.setHeader("Content-Length", String.valueOf(doc.length));
 "filename=\"" + "document.pdf" + "\"");

BufferedInputStream input = null;
 BufferedOutputStream output = null;

 try {
 input = new BufferedInputStream(bais, DEFAULT_BUFFER_SIZE);
 output =
 new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
 int length;
 while ((length = > 0) {
 output.write(buffer, 0, length);


} finally {


The example application is available here for download.



Leave a Comment
  1. Habib Eslami / Aug 25 2015 7:05 am

    Hi, thanks,
    I use this way to display a pdf report in a new tab, but the pop-up blocker appears.
    how we can call a pdf in new tab without appearing pop-up blocker( also I don’t want to use go link).


    • danilo / Aug 25 2015 9:11 am

      Hi Habib, the popup blocker should be a browser issue and not related to ADF.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: