如何从Web存档的WEB-INF目录加载资源

时间:2022-12-14 17:17:19

I have a web archive with a file placed in the WEB-INF directory.

我有一个Web档案,文件放在WEB-INF目录中。

How do I load that file in a java class?

如何在java类中加载该文件?

I know I can put it in the classes directory and load it from there. It would just be put it in WEB-INF.

我知道我可以将它放在classes目录中并从那里加载它。它只会被放在WEB-INF中。

3 个解决方案

#1


86  

Use the getResourceAsStream() method on the ServletContext object, e.g.

在ServletContext对象上使用getResourceAsStream()方法,例如

servletContext.getResourceAsStream("/WEB-INF/myfile");

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?

如何获得对ServletContext的引用取决于您的应用程序...您想从Servlet还是从JSP中执行此操作?

EDITED: If you're inside a Servlet object, then call getServletContext(). If you're in JSP, use the predefined variable application.

编辑:如果你在Servlet对象中,那么调用getServletContext()。如果您在JSP中,请使用预定义的变量应用程序。

#2


6  

Here is how it works for me with no Servlet use.

以下是它对我的工作原理,没有使用Servlet。

Let's say I am trying to access web.xml in project/WebContent/WEB-INF/web.xml

假设我正在尝试访问project / WebContent / WEB-INF / web.xml中的web.xml

  1. In project property Source-tab add source folder by pointing to the parent container for WEB-INF folder (in my case WebContent )

    在项目属性Source-tab中,通过指向WEB-INF文件夹的父容器(在我的WebContent中)添加源文件夹

  2. Now let's use class loader:

    现在让我们使用类加载器:

    InputStream inStream = class.getClass().getClassLoader().getResourceAsStream("Web-INF/web.xml")
    

#3


0  

The problem I had accessing the sqlite db file I created in my java (jersey) server had solely to due with path. Some of the docs say the jdbc connect url should look like "jdbc:sqlite://path-to-file/sample.db". I thought the double-slash was part of a htt protocol-style path and would map properly when deployed, but in actuality, it's an absolute or relative path. So, when I placed the file at the root of the WebContent folder (tomcat project), a uri like this worked "jdbc:sqlite:sample.db".

我访问我在java(jersey)服务器上创建的sqlite db文件的问题完全归结为路径。一些文档说jdbc connect url应该看起来像“jdbc:sqlite://path-to-file/sample.db”。我认为双斜杠是htt协议风格路径的一部分,并且在部署时会正确映射,但实际上,它是绝对路径或相对路径。因此,当我将文件放在WebContent文件夹(tomcat项目)的根目录下时,像这样的uri工作“jdbc:sqlite:sample.db”。

The one thing that was throwing me was that when I was stepping through the debugger, I received a message that said "opening db: ... permission denied". I thought it was a matter of file system permissions or perhaps sql user permissions. After finding out that SQLite doesn't have the concept of roles/permissions like MySQL, etc, I did eventually change the file permissions before I came to what I believe was the correct solution, but I think it was just a bad message (i.e. permission denied, instead of File not found).

抛出我的一件事是,当我踩过调试器时,我收到一条消息,上面写着“打开db:...权限被拒绝”。我认为这是文件系统权限或sql用户权限的问题。在发现SQLite没有像MySQL这样的角色/权限的概念之后,我确实最终在我认为是正确的解决方案之前更改了文件权限,但我认为这只是一个坏消息(即权限被拒绝,而不是找不到文件)。

Hope this helps someone.

希望这有助于某人。

#1


86  

Use the getResourceAsStream() method on the ServletContext object, e.g.

在ServletContext对象上使用getResourceAsStream()方法,例如

servletContext.getResourceAsStream("/WEB-INF/myfile");

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?

如何获得对ServletContext的引用取决于您的应用程序...您想从Servlet还是从JSP中执行此操作?

EDITED: If you're inside a Servlet object, then call getServletContext(). If you're in JSP, use the predefined variable application.

编辑:如果你在Servlet对象中,那么调用getServletContext()。如果您在JSP中,请使用预定义的变量应用程序。

#2


6  

Here is how it works for me with no Servlet use.

以下是它对我的工作原理,没有使用Servlet。

Let's say I am trying to access web.xml in project/WebContent/WEB-INF/web.xml

假设我正在尝试访问project / WebContent / WEB-INF / web.xml中的web.xml

  1. In project property Source-tab add source folder by pointing to the parent container for WEB-INF folder (in my case WebContent )

    在项目属性Source-tab中,通过指向WEB-INF文件夹的父容器(在我的WebContent中)添加源文件夹

  2. Now let's use class loader:

    现在让我们使用类加载器:

    InputStream inStream = class.getClass().getClassLoader().getResourceAsStream("Web-INF/web.xml")
    

#3


0  

The problem I had accessing the sqlite db file I created in my java (jersey) server had solely to due with path. Some of the docs say the jdbc connect url should look like "jdbc:sqlite://path-to-file/sample.db". I thought the double-slash was part of a htt protocol-style path and would map properly when deployed, but in actuality, it's an absolute or relative path. So, when I placed the file at the root of the WebContent folder (tomcat project), a uri like this worked "jdbc:sqlite:sample.db".

我访问我在java(jersey)服务器上创建的sqlite db文件的问题完全归结为路径。一些文档说jdbc connect url应该看起来像“jdbc:sqlite://path-to-file/sample.db”。我认为双斜杠是htt协议风格路径的一部分,并且在部署时会正确映射,但实际上,它是绝对路径或相对路径。因此,当我将文件放在WebContent文件夹(tomcat项目)的根目录下时,像这样的uri工作“jdbc:sqlite:sample.db”。

The one thing that was throwing me was that when I was stepping through the debugger, I received a message that said "opening db: ... permission denied". I thought it was a matter of file system permissions or perhaps sql user permissions. After finding out that SQLite doesn't have the concept of roles/permissions like MySQL, etc, I did eventually change the file permissions before I came to what I believe was the correct solution, but I think it was just a bad message (i.e. permission denied, instead of File not found).

抛出我的一件事是,当我踩过调试器时,我收到一条消息,上面写着“打开db:...权限被拒绝”。我认为这是文件系统权限或sql用户权限的问题。在发现SQLite没有像MySQL这样的角色/权限的概念之后,我确实最终在我认为是正确的解决方案之前更改了文件权限,但我认为这只是一个坏消息(即权限被拒绝,而不是找不到文件)。

Hope this helps someone.

希望这有助于某人。