如何从Web存档的WEB-INF目录加载资源 [英] How to load a resource from WEB-INF directory of a web archive

查看:116
本文介绍了如何从Web存档的WEB-INF目录加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

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