title: Opening PDF file using window.open()
body: <p>I have a method which generates a PDF file and stores it in <code>/temp</code> folder. I am using tomcat. I want to open the PDF file. I have tried this using <code>window.open()</code> method in the JavaScript. But on clicking the hyperlink it says the requested resource was not found.</p>
  <p>I store the PDF file in <code>/temp</code> folder using following line (of course the file gets generated and saved in the location):</p>
  <pre><code>inputMap.put(TableProperties.PDF_PATH, &quot;/temp/report.pdf&quot;);
  </code></pre>
  <p>Now in a JSP I try the following:</p>
  <pre class="lang-jsp prettyprint-override"><code>&lt;tr&gt;
    &lt;td&gt;
      &lt;a href=&quot;&quot; onclick=&quot;javascipt:window.open('/temp
  /report.pdf');&quot; class=&quot;popup&quot;&gt;Click to open.&lt;/a&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  <p>but its showing the following error:</p>
  <pre><code>The requested resource was not found. 
  
  http://localhost:8080/temp/report.pdf
  </code></pre>
  <p><strong>EDIT:</strong></p>
  <p>Tried the following:</p>
  <p>in the JSP page, a download link to open the file:</p>
  <pre class="lang-jsp prettyprint-override"><code>&lt;tr&gt;
    &lt;td&gt;
      &lt;a href='&lt;s:url action='gotoDownloadPdf'&gt; &lt;/s:url&gt;'&gt;
        download
      &lt;/a&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  <p><strong>in <code>struts.xml</code>:</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;action name=&quot;gotoDownloadPdf&quot; class=&quot;com.stp.portal.view.SearchServicePortlet&quot; method=&quot;gotoDownloadPdf&quot;&gt;
              &lt;result name=&quot;success&quot;&gt;/WEB-INF/view/pdfDownload.jsp&lt;/result&gt;   
          &lt;/action&gt;
  </code></pre>
  <p>a JSP page which contains the JavaScript to download the PDF file:</p>
  <pre class="lang-jsp prettyprint-override"><code>&lt;%@ page import=&quot;java.util.*,java.io.*&quot;%&gt;
  &lt;%@ page language=&quot;java&quot;%&gt;
  &lt;!--Assumes that file name is in the request objects query Parameter --&gt;
  &lt;%
      //response.setHeader (&quot;Cache-Control&quot;,&quot;no-cache&quot;);
      //response.setHeader (&quot;Pragma&quot;,&quot;no-cache&quot;);
      //response.setHeader (&quot;Expires&quot;,0);
      //read the file name.
      try
      {
          String fpath=&quot;/temp/&quot;; 
          String fileName=&quot;report.pdf&quot;; 
          fpath = fpath + fileName;  
          String fileType=&quot;pdf&quot;;
  
          File f = new File (fpath);
          
      
          //set the header and also the Name by which user will be prompted to save
          response.setHeader (&quot;Content-Disposition&quot;, &quot;attachment;filename=\&quot;&quot;+fileName+&quot;\&quot;&quot;);
          //get the file name
          String name = f.getName().substring(f.getName().lastIndexOf(&quot;/&quot;) + 1,f.getName().length());
  
          //OPen an input stream to the file and post the file contents thru the 
          //servlet output stream to the client m/c
          InputStream inputStream = new FileInputStream(f);
          ServletOutputStream servletOutputStream = response.getOutputStream();
          int bit = 256;
          int i = 0;
          try 
          {
              while ((bit) &gt;= 0) 
              {
                  bit = inputStream.read();
                  servletOutputStream.write(bit);
              }
              //System.out.println(&quot;&quot; +bit);
  
  
              }
              catch (Exception ioe) 
              {
                  //ioe.printStackTrace(System.out);
              }
                      System.out.println( &quot;\n&quot; + i + &quot; bytes sent.&quot;);
                      System.out.println( &quot;\n&quot; + f.length() + &quot; bytes sent.&quot;);
              servletOutputStream.flush();
              //outs.close();
              inputStream.close();    
      }
      catch(Exception e)
      {
                  
      }
                          
  %&gt;
  </code></pre>
  <p>Now when I click on the download link, nothing happens, it redirects to the JSP page, but the pop up window for download does not appear.</p>