【发布时间】:2013-04-12 02:28:50
【问题描述】:
在我的 servlet 中:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
new Action();
}
在我的动作类中:
Action(){
System.out.println("--> " + this.getClass().getResource("/").toString());
}
我有结果,这个位置:
--> file:/C:/Users/saad/Desktop/apache-tomcat-7.0.37/wtpwebapps/Serveur/WEB-INF/classes/
但我想像这样访问我的 webApp 的根目录:
file:/C:/Users/saad/Desktop/apache-tomcat-7.0.37/wtpwebapps/Serveur/
我坚持 Action 类不继承自 HttpServlet,所以我不能使用 ServletContext.getResource("") 。
我该怎么做?
【问题讨论】:
-
如果您无法修改您的
Action类以将请求作为参数获取(我看不出有什么好的理由不...),那么您已经有了您的类文件夹的路径。您应该能够从中计算出您的路径。虽然,如果它是用于在 Web 应用程序中存储文件,正如您在我的回答中所建议的那样,我建议您另作考虑。 -
您究竟需要这些信息做什么?请不要说你需要这个来写入文件。
标签: java jakarta-ee servlets web-applications getresource