How do I display a PDF file using servlets and JSP

2019-01-12 08:20发布

Can anyone tell how to display a PDF file which is stored in my database using servlets and JSP? Are there any specific jar files to be imported?

标签: jsp pdf servlets
1条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-12 08:52

Just get it as InputStream from DB and write it to OutputStream of the response along a correct set of headers. Here's a snippet assuming you're using JDBC to interact with DB.

response.setContentType("application/pdf");
// ...
InputStream input = resultSet.getBinaryStream("columnname");
OutputStream output = response.getOutputStream();
// Write input to output the usual way.
// ...

When mapped on an url-pattern of for example /pdfservlet, then you can just call the servlet using <a> link

<a href="pdfservlet?id=123">click here</a>

or by <object> if you want to embed it in HTML.

<object data="pdfservlet?id=123" type="application/pdf" width="600" height="400">
</object>

You don't need any additional libraries. A complete kickoff example can be found here.

查看更多
登录 后发表回答