2008. október 30., csütörtök

1001 ways to serve static content for your java web application

Okay, not 1001. That was the marketing part :) But here you can find a few.
 The problem originated from a job, where I had to handle uploading of user files.
  • Simply use the servlet container to serve up the content. This requires that the static content is at the top level of the WAR structure (or can be reached from there). Remember, anything inside WEB-INF won't be served as per the specification. There are two cons of this solution: one is that the content can be only located within the war, the second is that the user cannot upload new content (because of the WAR's read-only nature).
  • You create a new context called let's say "/files" and point this context to the directory where the content is located (remember to create a dummy WEB-INF/web.xml too).
  • You write a custom servlet to hand out the content. You have to be careful about server side caching, client side caching (http headers!) and proper checking of the servlet parameters, so your new servlet won't be a new way to display /etc/passwd remotely.
  • You place a symlink within the unextracted WAR structure to the real directory, where content is stored.
  • You configure the DefaultServlet of your container to serve up your content. This servlet is responsible for serving your static content inside the WAR, however, can be sometimes reconfigured to serve content from outside directories. Unfortunately this servlet is not standardized, which means your web.xml won't be cross-container.
  • You let your http server (apache) serve static content, and only dynamic pages go to your servlet container. (Obviously this will only work if you've got a http server in front of the servlet container).
Happy coding!