Question

I was using the following in web.xml to configure a servlet to dynamically generate PDFs.

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

Now, I also have to serve a few static PDF files. What's the cleanest way to configure that? I'm currently only serving four or five dynamic files, and don't expect that to increase, if that helps at all.

Was it helpful?

Solution

You don't need to configure static files at all; if Tomcat can find a file, it will serve it. The configuration is only necessary when you want Tomcat to invoke some code.

OTHER TIPS

This is a surprisingly irritating problem that I've yet to find a satisfactory solution to.

The basis of the problem, as I'm sure you're aware, is that your web.xml is configured to send all request for *.pdf to your Spring servlet. The obvious thing to try is for the servlet to recognise which requests are for static PDFs, and to then forward the request internally to that static file, but because the file will likely end with .pdf, the request will just go back through the servlet again, ad nauseum.

The only workaround for this that I've tried is to have the servlet manually read the static PDF from the servlet context (using ServletContext.getResource()), and write it to the servlet output stream, making sure to set the various headers properly. It's not very nice.

The only option I can think of is to make the url-pattern in web.xml a bit less broad, so that only dynamic PDF requests get routed to the servlet, and requests for static PDFs get routed to the file, but that would require some kind of naming convention for your documents, which may not be an option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top