Java 从 Servlet 访问 Tomcat 上下文路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Tomcat Context Path from Servlet
提问by Casey Watson
In my Servlet I would like to access the root of the context so that I can do some JavaScript minifying.
在我的 Servlet 中,我想访问上下文的根,以便我可以做一些 JavaScript 缩小。
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.
可以将 minify 作为安装过程的一部分进行,但我想在 Servlet 启动时进行,以降低实现成本。
Does anyone know of a method for getting the context directory so that I can load and write files to disk?
有谁知道获取上下文目录以便我可以加载文件并将文件写入磁盘的方法?
采纳答案by ScArcher2
This should give you the real path that you can use to extract / edit files.
这应该为您提供可用于提取/编辑文件的真实路径。
We're doing something similar in a context listener.
我们正在上下文侦听器中做类似的事情。
public class MyServlet extends HttpServlet {
public void init(final ServletConfig config) {
final String context = config.getServletContext().getRealPath("/");
...
}
...
}
回答by Walter Rumsby
Do you mean:
你的意思是:
public class MyServlet extends HttpServlet {
public void init(final ServletConfig config) {
final String context = config.getServletContext();
...
}
...
}
Or something more complex?
或者更复杂的东西?
回答by di?tpillen
I was googling the result and getting no where. In JSP pages that need to use Java Script to access the current contextPathit is actually quite easy.
我在谷歌上搜索结果却一无所获。在需要使用 Java Script 访问当前contextPath 的JSP 页面中,它实际上很容易。
Just put the following lines into your html headinside a script
block.
只需将以下行放入script
块内的html 头部。
// set up a global java script variable to access the context path
var contextPath = "${request.contextPath}"
回答by BalusC
In my Servlet I would like to access the root of the context so that I can do some JavaScript minifying
在我的 Servlet 中,我想访问上下文的根,以便我可以做一些 JavaScript 缩小
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:
您还可以通过 访问 WebContent 中的文件ServletContext#getResource()
。因此,如果您的 JS 文件位于例如,WebContent/js/file.js
那么您可以使用以下内容Servlet
来获取File
它的句柄:
File file = new File(getServletContext().getResource("/js/file.js").getFile());
or to get an InputStream
:
或获得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()
.
也就是说,您需要多久缩小一次 JS 文件?我从未见过基于请求的缩小的必要性,它只会不必要地增加很多开销。您可能只想在应用程序启动期间执行一次。如果是这样,那么Servlet
为此使用 a是一个坏主意。更好地使用ServletContextListener
和做你的事情contextInitialized()
。