<p>In my Servlet I would like to access the root of the context so that I can do some JavaScript minifying.</p>

<p>It would be possible to do the minify as part of the install process but I would like to do it on Servlet startup to reduce the implementation cost.</p>

<p>Does anyone know of a method for getting the context directory so that I can load and write files to disk?</p>

[{'answer_id': 57563, 'author': 'Walter Rumsby', 'author_id': 1654, 'author_profile': 'https://Stackoverflow.com/users/1654', 'pm_score': -1, 'selected': False, 'text': '<p>Do you mean:</p>\n\n<pre><code>public class MyServlet extends HttpServlet {\n\n    public void init(final ServletConfig config) {\n        final String context = config.getServletContext();\n        ...\n    }\n\n    ...\n}\n</code></pre>\n\n<p>Or something more complex?</p>\n'}, {'answer_id': 57595, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 5, 'selected': True, 'text': '<p>This should give you the real path that you can use to extract / edit files.</p>\n\n<p><a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)" rel="noreferrer">Javadoc Link</a></p>\n\n<p>We\'re doing something similar in a context listener.</p>\n\n<pre><code>public class MyServlet extends HttpServlet {\n\n    public void init(final ServletConfig config) {\n        final String context = config.getServletContext().getRealPath("/");\n        ...\n    }\n\n    ...\n}\n</code></pre>\n'}, {'answer_id': 1807990, 'author': 'diätpillen', 'author_id': 219965, 'author_profile': 'https://Stackoverflow.com/users/219965', 'pm_score': 0, 'selected': False, 'text': '<p>I was googling the result and getting no where. In JSP pages that need to use Java Script to access the current <em>contextPath</em> it is actually quite easy. </p>\n\n<p>Just put the following lines into your <em>html head</em> inside a <code>script</code> block.</p>\n\n<pre><code>// set up a global java script variable to access the context path\nvar contextPath = "${request.contextPath}" \n</code></pre>\n'}, {'answer_id': 1808295, 'author': 'BalusC', 'author_id': 157882, 'author_profile': 'https://Stackoverflow.com/users/157882', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p><em>In my Servlet I would like to access the root of the context so that I can do some JavaScript minifying</em></p>\n</blockquote>\n\n<p>You can also access the files in the WebContent by <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html#getResource%28java.lang.String%29" rel="nofollow noreferrer"><code>ServletContext#getResource()</code></a>. So if your JS file is for example located at <code>WebContent/js/file.js</code> then you can use the following in your <code>Servlet</code> to get a <code>File</code> handle of it:</p>\n\n<pre><code>File file = new File(getServletContext().getResource("/js/file.js").getFile());\n</code></pre>\n\n<p>or to get an <code>InputStream</code>:</p>\n\n<pre><code>InputStream input = getServletContext().getResourceAsStream("/js/file.js");\n</code></pre>\n\n<p>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 <code>Servlet</code> for this is a bad idea. Better use <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html" rel="nofollow noreferrer"><code>ServletContextListener</code></a> and do your thing on <code>contextInitialized()</code>.</p>\n'}]
['https://Stackoverflow.com/questions/57537', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4682/']