open pdf file in new window using servlet

19,351

Solution 1

In case if you are using GET request to make a servlet call

GET set the target of link to target="_blank"

<a href="/url/to/servlet" target="_blank"/>

POST

<form method="post" action="url/to/servlet"
  target="_blank">

so browser will make a new GET/POST request in new window/tab and then set the header Content-disposition to inline to render pdf inline instead of prompting a download window

Solution 2

/*create jsp page*/   
<form action="OpenPdfDemo" method="post" target="_blank">
        <input type="submit" value="post">
    </form>
/* create servlet (servlet name = OpenPdfDemo)*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");



        ServletOutputStream  outs =  response.getOutputStream ();
//---------------------------------------------------------------
// Set the output data's mime type
//---------------------------------------------------------------
response.setContentType( "application/pdf" );  // MIME type for pdf doc
//---------------------------------------------------------------
// create an input stream from fileURL
//---------------------------------------------------------------

File file=new File("X://Books/struts book/sturts_Books/struts-tutorial.pdf");


//------------------------------------------------------------
// Content-disposition header - don't open in browser and
// set the "Save As..." filename.
// *There is reportedly a bug in IE4.0 which  ignores this...
//------------------------------------------------------------
response.setHeader("Content-disposition","inline; filename=" +"Example.pdf" );

BufferedInputStream  bis = null; 
BufferedOutputStream bos = null;
try {

    InputStream isr=new FileInputStream(file);
    bis = new BufferedInputStream(isr);
    bos = new BufferedOutputStream(outs);
    byte[] buff = new byte[2048];
    int bytesRead;
    // Simple read/write loop.
    while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
        bos.write(buff, 0, bytesRead);
    }
} 
catch(Exception e)
{
    System.out.println("Exception ----- Message ---"+e);
} finally {
    if (bis != null)
        bis.close();
    if (bos != null)
        bos.close();
}


    }
Share:
19,351
user3150808
Author by

user3150808

Updated on June 26, 2022

Comments

  • user3150808
    user3150808 almost 2 years

    I am generating a pdf file for gate pass from my web application through servlet. I want to open this newly generated pdf in new window/tab and user should come back to the application from servlet. How can i open pdf in new window/tab? I am generating pdf from itext api. My servlet code snippet is:

            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control","must-revalidate, post-check=0,precheck=0");
            response.setHeader("Pragma", "public");
            response.setContentType("application/pdf");
            response.setContentLength(baos.size());
            OutputStream os = response.getOutputStream();
            baos.writeTo(os);
            os.flush();
            os.close(); 
    
  • Stealth Rabbi
    Stealth Rabbi over 7 years
    why are you setting the response's content type twice? Which is correct?
  • NITHIN RAJ T
    NITHIN RAJ T over 4 years
    Hi, I have a problem which is related to this. Suppose the requested resource not available and I have to display an error message, without making to calls to back-end(one for checking the resource availability and another for getting the resource), how can I achieve this?