Displaying both static and dynamic files in Tomcat / Spring?

I used the following in web.xml to configure the servlet to generate PDFs dynamically.

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

      

Now I have to serve multiple static PDFs as well. What's the cleanest way to set this up? I am currently working with only four or five dynamic files and do not expect this to grow, if it helps at all.

+2


source to share


2 answers


You don't need to configure static files at all; if Tomcat can find the file, it will serve it. Configuration is only needed when you want Tomcat to call some code.



+1


source


This is a surprisingly annoying problem that I haven't found a satisfactory solution yet.

The root of the problem, as I'm sure you know, is that your web.xml is configured to send the entire * .pdf request to your Spring servlet. The obvious thing to try is to force the servlet to recognize which requests are for static PDFs and then redirect the request inside that static file, but since the file will most likely end up with .pdf, the request will just return through the servlet again, ad nauseum.



The only workaround I've tried for this is to force the servlet to manually read the static PDF from the servlet context (using ServletContext.getResource()

) and write it to the servlet's output stream, making sure to set the various headers appropriately. This is not very pleasant.

The only thing I can think of is to make it url-pattern

a web.xml

little less broad so that only requests with a dynamic PDF will be redirected to the servlet and requests for static PDFs will be redirected to a file, but which would require some kind of naming convention for of your documents, which may not be an option.

+3


source







All Articles