views:

380

answers:

2

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.

+1  A: 

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.

Aaron Digulla
I was previously having Tomcat invoke code on all PDF requests; they were all dynamically built by iText. Now, I need Tomcat to pass static files through as well; won't that get caught by the servlet-mapping?
Dean J
Ah, you mean static PDF files (i.e. files which *match* your URL rule). Ok, can't help you there :)
Aaron Digulla
Yup. Yeah, seems like the only solution is to individually map all of the dynamic ones explicitly, without wildcards.
Dean J
Use `/generated/*.pdf` as the url-pattern. Then Tomcat will try to serve any other URL which ends with .pdf from static files.
Aaron Digulla
+3  A: 

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.

skaffman
I might give a naming convention a try; manually serving the static file seems less clean. Basically, we're moving from using iText to using XDP (XML+PDF) to produce dynamic documents, but the XDP requires a static PDF template be served up.
Dean J