Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>JSP is the wrong tool for the job of serving a file download. JSP is designed as a view technology with the intent to easily produce HTML output with taglibs and EL. Basically, with your JSP approach, your PDF file is cluttered with <code>&lt;!DOCTYPE&gt;</code>, <code>&lt;html&gt;</code> etc tags and therefore corrupted and not recognizable as a valid PDF file. This is by the way one of the reasons why <a href="https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202">using <em>scriptlets</em> is a bad practice</a>. It has namely completely confused you as to how stuff is supposed to work. In this particular case, that is using a normal Java class for the file download job.</p> <p>You should be using a <a href="https://stackoverflow.com/tags/servlets/info">servlet</a> instead. Here's a kickoff example, assuming that Servlet 3.0 and Java 7 is available:</p> <pre class="lang-java prettyprint-override"><code>@WebServlet("/foo.pdf") public class PdfServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File file = new File("/absolute/path/to/foo.pdf"); response.setHeader("Content-Type", getServletContext().getMimeType(file.getName())); response.setHeader("Content-Length", String.valueOf(file.length())); response.setHeader("Content-Disposition", "inline; filename=\"foo.pdf\""); Files.copy(file.toPath(), response.getOutputStream()); } } </code></pre> <p><em>(if Servlet 3.0 is not available, then map it in <code>web.xml</code> <a href="https://stackoverflow.com/tags/servlets/info">the usual way</a>, if Java 7 is not available, then use a read/write loop <a href="https://stackoverflow.com/questions/10142409/write-an-inputstream-to-an-httpservletresponse/10142601#10142601">the usual way</a>)</em></p> <p>Just copypaste this class in its entirety into your project and open the desired PDF file by <code>/contextpath/Saba_PhBill.pdf</code> instead of <code>/contextpath/youroriginal.jsp</code> (after having organized it in a package and autocompleted the necessary imports in the class, of course). </p> <p>E.g. as follows in a JSP where you'd like to show the PDF inline:</p> <pre class="lang-html prettyprint-override"><code>&lt;object data="${pageContext.request.contextPath}/Saba_PhBill.pdf" type="application/pdf" width="500" height="300"&gt; &lt;a href="${pageContext.request.contextPath}/Saba_PhBill.pdf"&gt;Download file.pdf&lt;/a&gt; &lt;/object&gt; </code></pre> <p><em>(the <code>&lt;a&gt;</code> link is meant as graceful degradation when the browser being used doesn't support inlining <code>application/pdf</code> content in a HTML document, i.e. when it doesn't have Adobe Reader plugin installed)</em></p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/1812244/simplest-way-to-serve-static-data-from-outside-the-application-server-in-a-java">Simplest way to serve static data from outside the application server in a Java web application</a></li> <li><a href="https://stackoverflow.com/questions/132052/servlet-for-serving-static-content/29991447#29991447">Abstract template for a static resource servlet supporting ETags, caching, etc</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload