In my Servlet I would like to access the root of the context so that I can do some JavaScript minifying
You can also access the files in the WebContent by ServletContext#getResource()
. So if your JS file is for example located at WebContent/js/file.js
then you can use the following in your Servlet
to get a File
handle of it:
File file = new File(getServletContext().getResource("/js/file.js").getFile());
or to get an InputStream
:
InputStream input = getServletContext().getResourceAsStream("/js/file.js");
That said, how often do you need to minify JS files? I have never seen the need for request-based minifying, it would only unnecessarily add much overhead. You probably want to do it only once during application's startup. If so, then using a Servlet
for this is a bad idea. Better use ServletContextListener
and do your thing on contextInitialized()
.