ServletContext.getRealPath()是否可用于Web片段? [英] Does ServletContext.getRealPath() work with web fragments?

查看:162
本文介绍了ServletContext.getRealPath()是否可用于Web片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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 a String argument and returns a String representation of a file on the local file system to which a path corresponds. Resources inside the META-INF/resources directory of JAR file must be considered only if the container has unpacked them from their containing JAR file when a call to getRealPath() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆