How to download a resource from the WEB-INF directory of a web archive

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

How to load this file in java class?

I know that I can put it in the class directory and load it from there. It will just be placed in WEB-INF.

+54
java
Jul 10 '09 at 8:43
source share
3 answers

Use the getResourceAsStream() method for a ServletContext object, for example

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

How you get the link to ServletContext depends on your application ... do you want to do this from a servlet or from a JSP?

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

+82
Jul 10 '09 at 8:46
source share

Here's how it works for me without using a servlet.

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

  • In the properties of the Source-tab project, add the source folder by pointing to the parent container for the WEB-INF folder (in my case, WebContent)

  • Now you can use the class loader:

     InputStream inStream = class.getClass().getClassLoader().getResourceAsStream("Web-INF/web.xml") 
+5
Apr 09 '13 at 11:26
source share

The problem that I encountered with the sqlite db file created on my java server (jersey) was only related to the path. Some of the docs say that the jdbc connection url should look like "jdbc: sqlite: //path-to-file/sample.db". I thought the double slash was part of the htt protocol style path and displayed correctly during deployment, but it is actually an absolute or relative path. So, when I placed the file in the root of the WebContent folder (tomcat project), this uri worked like "jdbc: sqlite: sample.db".

The only thing that threw me away was that when I was going through the debugger, I got a message saying: "Opening db: ... permission denied". I thought this was a matter of file system permissions or, possibly, sql user rights. Upon learning that SQLite does not have a concept of roles / permissions such as MySQL, etc., I eventually changed the file permissions before I came to what I think was the right solution, but I think that it was just a bad message (i.e. allowed, instead of a file not found).

Hope this helps someone.

0
Jun 22 '17 at 21:17
source share



All Articles