title: How to Display a PDF document with a Servlet and JSP?
body: <p>I looking to display a PDF document from a database to a browser, I wish the browser to open it but it's also ok if its prompt for it to be download. I know this question has been asked here and other forums but I'm still not winning with this task.</p>
  
  <p>I have looked at these:
  <a href="https://stackoverflow.com/questions/4614935/how-do-i-display-a-pdf-file-using-servlets-and-jsp">JSP n Servlets</a>
  <a href="http://balusc.blogspot.com/2007/07/fileservlet.html" rel="nofollow noreferrer">Display PDF via JSP n Servlet tutorial</a></p>
  
  <p>My current code.</p>
  
  <p>OBJ/Entity:``</p>
  
  <pre><code> public class Attach
     {
      private String filename = null;
      private InputStream attach = null;
      private int ContentLength = 0;
  
      public String getFilename() {
  
          return filename;
      }
      public void setFilename(String filename) {
  
          this.filename = filename;
      }
      public InputStream getAttach() {
  
          return attach;
      }
      public void setAttach(InputStream attach) {
  
          this.attach = attach;
      }
  
      public int getContentLength() {
  
          return ContentLength;
      }
      public void setContentLength(int contentLength) {
  
          ContentLength = contentLength;
      }
  
      public Attach() {
  
      }
  
      public Attach(String filename, InputStream attach) {
          this.attach = attach;
          this.filename = filename;
      }
  
  
      }
  </code></pre>
  
  <p>Method to retrieve PDF from DB:</p>
  
  <pre><code>public Attach getPDFData(String filename) {
  
      try {
          pstmt = conn.prepareStatement("SELECT * FROM FUNC_AREA WHERE FILE_NME = ?");
          pstmt.setString(1, filename);
          rs = pstmt.executeQuery();
          if (rs.next()) {
              newattach.setFilename(rs.getString(3));
              newattach.setAttach(rs.getBinaryStream(5));
          }
  
      } catch (SQLException e) {
          e.printStackTrace();
      }
      return newattach;
  }
  </code></pre>
  
  <p>My Servlet:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          showAttach(request, response);
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          showAttach(request, response);
      }
  
      public void showAttach(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           RepoOperations repops = new RepoOperations();
          Attach newattachobj = repops.getPDFData("bond.pdf");
  
          try {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
              int inputStreamLength = 0;
              int length = 0;
              while ((length = newattachobj.getAttach().read(buffer)) &gt; 0) {
                  inputStreamLength += length;
                  baos.write(buffer, 0, length);
              }
  
              if (inputStreamLength &gt; newattachobj.getContentLength()) {
                  newattachobj.setContentLength(inputStreamLength);
              }
  
              if (response instanceof HttpServletResponse) {
                  HttpServletResponse httpResponse = (HttpServletResponse) response;
                  httpResponse.reset();
                  httpResponse.setHeader("Content-Type", "application/pdf");
                  httpResponse.setHeader("Content-Length", String.valueOf(newattachobj.getContentLength()));
                  httpResponse.setHeader("Content-Disposition", "inline; filename=\"" + newattachobj.getFilename() + "\"");
              }
  
              response.getOutputStream().write(baos.toByteArray(), 0, (int)newattachobj.getContentLength());
  
              //finally
              response.getOutputStream().flush();
  
              //clear
              baos = null;
  
              System.out.println(newattachobj.getFilename());
          } finally {
              // TODO Auto-generated catch block
              close(response.getOutputStream());
              close(newattachobj.getAttach());
          }
  
      }
  
      private void close(Closeable resource) throws IOException {
          if (resource != null) {
              resource.close();
          }
      }
  </code></pre>
  
  <p>JSP:</p>
  
  <pre><code> &lt;form action="ShowAttach"&gt;
      &lt;a href="ShowAttach"&gt;click here&lt;/a&gt;
      &lt;br/&gt;
      &lt;input type="submit" value="submit" id="submit"&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p><i>I'm looking to have a Hyperlink on the JSP page to open the PDF document.</i> Thank You</p>
  
  <p><b>The problem  is that when I click the button on the JSP page, it gives me a 404 error.</b></p>