如何从 Web 存档的 WEB-INF 目录加载资源 [英] How to load a resource from WEB-INF directory of a web archive
问题描述
我有一个 Web 存档,其中一个文件位于 WEB-INF 目录中.
I have a web archive with a file placed in the WEB-INF directory.
如何在 java 类中加载该文件?
How do I load that file in a java class?
我知道我可以将它放在 classes 目录中并从那里加载它.它只会放在 WEB-INF 中.
I know I can put it in the classes directory and load it from there. It would just be put it in WEB-INF.
推荐答案
在 ServletContext 对象上使用 getResourceAsStream()
方法,例如
Use the getResourceAsStream()
method on the ServletContext object, e.g.
servletContext.getResourceAsStream("/WEB-INF/myfile");
您如何获得对 ServletContext 的引用取决于您的应用程序...您想从 Servlet 还是从 JSP 中获得它?
How you get a reference to the ServletContext depends on your application... do you want to do it from a Servlet or from a JSP?
已如果您在 Servlet 对象中,则调用 getServletContext()
.如果您使用的是 JSP,请使用预定义的变量 application
.
EDITED: If you're inside a Servlet object, then call getServletContext()
. If you're in JSP, use the predefined variable application
.
这篇关于如何从 Web 存档的 WEB-INF 目录加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!