In my servlet, I would like to access the context root so that I can execute some kind of JavaScript shortening
You can also access files in WebContent using ServletContext#getResource() . Therefore, if your JS file, for example, is located in WebContent/js/file.js , you can use the following in Servlet to get the File descriptor:
File file = new File(getServletContext().getResource("/js/file.js").getFile());
or get an InputStream :
InputStream input = getServletContext().getResourceAsStream("/js/file.js");
However, how often do you need to minimize JS files? I have never seen the need to minimize queries, this would only unnecessarily increase the overhead. You will probably want to do this only once during the launch of the application. If so, using Servlet for this is a bad idea. Better use ServletContextListener and do your thing on contextInitialized() .
Balusc
source share