Show PDF on the page via Servlet

Asked

Viewed 727 times

2

Hello, I am developing a web application using Servlets and Jsps. I still don’t have much experience with it.

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 step as parameter the id with request.getParameter("id"), but the problem is that it is not enough to make a request for this my Servlet.

To illustrate, my select this way:

SELECT pdf from pdf WHERE id = ?

When I give a request.getParameter("id") and step the id as a parameter in this query doesn’t quite work, but if I change the query already passing a parameter as:

SELECT pdf from pdf WHERE id = 1

Then the PDF click on the page.

In short: I have a JSP, which contains only one button that makes one request for a Servlet to download a BD file. Works perfectly and I have a <object> to display a PDF that is also stored in the BD, but this does not work as I explained.

What I think might be happening is that the button works because when I give submit he does the request to Servlet, but the PDF is already to be loaded on the screen when the page opens and I do not have much experience with programming Java for the WEB, I am not able to make a request to the Servlet when the page loads.

Can someone help me with this? I need some Javascript or similar function to request and pass the parameter to my PDF Servlet?

1 answer

1


To display a PDF on the page using <object> you nay should 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 solely responsible for generating the PDF and return to the browser will go into action.

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

  • I did as you said, but I haven’t gotten any results yet

  • Can you post an example of what the Servlet code should look like for the answer? No Headers? -

  • I did. Actually a little different, but it helped me a lot your reply! Thank you very much!!

  • @I’m glad you made it. I’m sorry I didn’t return before, but I’m in another zone. Hug.

  • If you could post your answer would be good, I’m with the same diffusion

Browser other questions tagged

You are not signed in. Login or sign up in order to post.