How to open a pdf document from JSP [closed]
How do I open a PDF document from JSP? I have many links to PDF files. In Eclipse, when I click on the link, the PDF opens in PDF reader. But in a webbrowser like Firefox and Chrome, nothing happens.
I am generating the links to PDF files as F:\....\...pdf
the following way:
<%
while (iter.hasNext()) {
element = iter.next();
bookName = getBookName(element);
%>
<ul>
<li><a href="<%=element %>"><%=bookName %></a>
</ul>
<%开发者_如何学运维
}
%>
How is this problem caused and how can I solve it?
The links have to point to an URL, not to a local disk file system path or something. The average webbrowser doesn't swallow this due to security restrictions. And even then when it did, it would not work in production environment when the webbrowser runs at a physically different machine than the webserver and thus doesn't have access to F:
disk at all.
So, you should NOT use
<a href="F:\....\...pdf">link</a>
but you should rather use
<a href="http://example.com/filename.pdf">link</a>
or when it's for example in the same folder of the current JSP file
<a href="filename.pdf">link</a>
If you cannot put the PDF file in your own web project, then you need to look for different solutions.
- Add the external folder as another context of the servletcontainer.
- Move the external folder into deploy folder of the servletcontainer.
- Create a servlet which reads it from disk and writes it to response.
You can find the above solutions in detail in the answers of the following questions:
- Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag
- Simplest way to serve static data from outside the application server in a Java web application
精彩评论