0

I got a situation that I must serve files from different folders then the one of the context my web app is running. As an example, suppose my web app is running in a servlet context on "/opt/tomcat/webapps/ROOT/" and I must serve files existent in "/opt/my_other_folder/". These folders can be changed in runtime by the client, so I can't simply add a new context pointing to these directories. I would like a solution that I wouldn't have to rewrite a web server only for that. Also, the product I work on is generic, so I can't have a solution specific to some servlet container.

Thanks!

Berne
  • 617
  • 1
  • 7
  • 17
  • Notice that my question is NOT the same as http://stackoverflow.com/questions/132052/servlet-for-serving-static-content – Berne Dec 28 '11 at 18:35

1 Answers1

0

If you're only serving files, I would consider fronting your servlet container with something like Apache HTTP Server, where you could simply use its various directives to provide a "virtual directory" pointing to an easily configured location.

Otherwise, you could write and configure a standard Java servlet that would do essentially the same thing - storing the actual path in a Java properties file that would be read by the servlet. But while this isn't a lot of work, it would be significantly more work that the above Apache HTTP Server solution. This would be very similar to several of the answers posted at Servlet for serving static content . Specifically, you could either use or extend upon Apache Tomcat's DefaultServlet. (There are some Tomcat-specific classes used in here, but they could be easily replaced with generic equivalents.) http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html looks even closer to what you'd be looking for, and it is completely generic - while still having some additional, significant features.

Either of these options would be very generic, and not specific to any particular servlet container.

Community
  • 1
  • 1
ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • Notice that fronting my web app with an http server is not an option, since the user will have the option of add / edit / delete a service folder. For the same reason, the answers in http://stackoverflow.com/questions/132052/servlet-for-serving-static-content aren't suittable for me. The post you linked is the closest to what I want, but it does seems to be rewriting an http server for me. But, even so, thanks for your answer... – Berne Dec 28 '11 at 18:52
  • Just to clarify, it's not rewriting a web or HTTP server. The server is still handling all the low-level aspects of the request, and providing a rather nice servlet API to work with. :-) – ziesemer Dec 28 '11 at 18:53
  • I may not have been so clear in what I sad. I didn't mean that would be rewriting the servlet container (which implement the servlet API). I meant it would be like rewriting a http server (as the Apache Http server, which does not implement the servlet API). – Berne Dec 28 '11 at 19:03