ServletContext.getRealPath()是否可用于Web片段? [英] Does ServletContext.getRealPath() work with web fragments?
问题描述
我使用Web片段(Servlet 3规范),因此可以加载例如通过ServletContext.getResourceAsStream("access.xml")
在我的WAR的/WEB-INF/lib/
中的库中的META-INF/resources/access.xml
文件.
I use web fragments (servlet 3 spec) and thus can load e.g. META-INF/resources/access.xml
file which is in a library in /WEB-INF/lib/
of my WAR via ServletContext.getResourceAsStream("access.xml")
.
对ServletContext.getRealPath("access.xml")
进行相同操作不起作用(=> null
).
Doing the same with ServletContext.getRealPath("access.xml")
doesn't work (=> null
).
规范指出:
getRealPath
方法采用一个String
参数,并返回该路径所对应的本地文件系统上文件的String
表示形式.仅当调用getRealPath()
时容器已将其从其包含的JAR文件中解压缩时,才必须考虑JAR文件的META-INF/resources
目录中的资源. 完成后,在这种情况下,必须返回打开包装的位置.
The
getRealPath
method takes aString
argument and returns aString
representation of a file on the local file system to which a path corresponds. Resources inside theMETA-INF/resources
directory of JAR file must be considered only if the container has unpacked them from their containing JAR file when a call togetRealPath()
is made, and in this case MUST return the unpacked location.
我的容器(Tomcat)没有打开罐子的包装,这似乎是问题所在? Tomcat如何解压缩罐子.包装WAR时,我应该打开罐子的包装吗?
My container (Tomcat) didn't unpack the jars, this seems to be the problem? How can Tomcat unpack the jars. Should I unpack the jars when packaging the WAR?
推荐答案
是的,getResourcesAsStream()
始终有效.但是getRealPath()
不会.
Yes, getResourcesAsStream()
always works. But getRealPath()
doesn't.
答案:不要尝试将getRealPath()
与Web片段一起使用.
Answer: Don't try to use getRealPath()
together with web fragments.
这篇关于ServletContext.getRealPath()是否可用于Web片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!