Show PDF on page via Servlet

2

Hello, I'm developing a web application using Servlets and JSPs. I do not have much experience yet.

My page contains a button that downloads a BD file and a <object> html to display a PDF that is in the BD.

The button works normally, but the PDF does not appear. It is not appearing because I pass as a parameter the id with request.getParameter("id") , but the problem is that it does not even make a request for this my Servlet.

For example, my select looks like this:

SELECT pdf from pdf WHERE id = ?

When I give request.getParameter("id") and I pass id as a parameter in this query does not work, but if I change query by passing a parameter like:

SELECT pdf from pdf WHERE id = 1

Then the PDF loads on the page.

In short: I have a JSP, which contains just one button that request for a Servlet to download a DB file. It works perfectly and I have <object> to display a PDF that is also stored in the DB, but it does not work as I explained.

What I think may be happening is that the button works because when I give submit it requests the Servlet, but the PDF is already loaded on the screen when the page opens and I do not have much programming experience yet Java for the WEB, I can not make a request to the Servlet when the page loads.

Could anyone help me with this? Do I need some Javascript function or something similar to make the request and pass the parameter to my PDF Servlet?

    
asked by anonymous 12.08.2015 / 13:34

1 answer

1

To display a PDF on the page using <object> you not must generate the PDF when rendering the page. Just create a URL for the page that generates the PDF.

Example:

<object data="/ver-pdf/?id=123" type="application/pdf"></object>

When the browser finds this tag on your page it will make a new request to the server and then a page exclusively responsible for generating the PDF and returning to the browser will take effect. >

This page is exactly the same as download , but without the headers generally used to force the browser to download the file.

    
12.08.2015 / 16:14