本文介绍了从Servlet访问Tomcat上下文路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Servlet中,我想访问上下文的根,以便可以进行一些JavaScript精简.
In my Servlet I would like to access the root of the context so that I can do some JavaScript minifying.
可以在安装过程中进行最小化,但我想在Servlet启动时进行以减小实现成本.
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.
有人知道获取上下文目录的方法,以便我可以将文件加载和写入磁盘吗?
Does anyone know of a method for getting the context directory so that I can load and write files to disk?
推荐答案
这应该为您提供可用于提取/编辑文件的真实路径.
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("/");
...
}
...
}
这篇关于从Servlet访问Tomcat上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!