Recently a co-worker created a jar with common utilities in it including some JSP tags. The tag was easy enough to deal with but how do you load CSS/JS from a jar file? His solution was pretty simple; create a servlet that lives in the JAR that loads resources off of the classpath. All you had to do then was map the servlet in the webapp where you want to use it and it would snag requests and “serve” content from the JAR. This was good enough for static files but what about the CSS with paths that changed based on environment?
Normally, there’s EL in the CSS file and we just map *.css to the JSP servlet for our container. This works fantastically well when the application has the CSS files exploded but doesn’t work at all when the CSS is streamed to the JSP’s writer; enter Velocity. Rather than having Velocity start up and filter all requests, you can start the engine up just to process certain files, then shut it down after you’re done. Since Velocity tags are in the same format as EL you don’t have to modify the CSS files at all.
This is a great start and will get you going but there’s a better way that doesn’t rely on third party libraries, I’ll cover that in a future post!