Kyaw Htet Soe Kyaw Htet Soe - 1 year ago 135
Java Question

How to display dynamically generated pdf files in the browser, tomcat server, java webapp

I am creating a dynamic web application in Java using Eclipse. My web application basically creates PDF files by getting information from IOS app.

After creating PDFs, I want to show it inside the IOS app. I saved the generated PDF files dynamically into the

WebContent
directory of the Eclipse project.

In order to test in
localhost
that I can display PDF files in my IOS app, the app needs to know the URL of the PDF file.

However, the Tomcat server cannot display the PDF file in the browser. So how do I display the generated PDF files in the browser first? Do i need to do a servlet mapping which would be difficult as the PDF files are generated dynamically.

Here is my directory to store pdf

I followed what you suggested

Answer Source

The Tomcat server isn't going to display static resources (*.jpg, *.png, *.pdf etc) automatically unless it's been instructed to do so. In order to have Tomcat retrieve those resources, add the following to your web.xml

<servlet-mapping>   
    <servlet-name>default</servlet-name>
    <url-pattern>*.pdf</url-pattern>
</servlet-mapping>

Note that you're using a default servlet and not your custom servlet that handles dynamic content.

This example assumes that you store your PDFs to the file system (in a folder accessible to your web app). Just because they're created on-the-fly (dynamically) doesn't matter as long as you return an URL to the generated file.

UPDATE: Your servlet should return the URL to your PDF file, e.g

http://localhost:8080/data/img/012725.pdf

Now, when you click that link (either in the browser or your iOS app), the PDF will be downloaded to your client.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download