58

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

How do I load that file in a java class?

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

Luciano van der Veekens
  • 5,586
  • 4
  • 21
  • 29
Michael Wiles
  • 19,590
  • 17
  • 65
  • 98

3 Answers3

89

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

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?

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

Blaisorblade
  • 6,172
  • 40
  • 69
skaffman
  • 381,978
  • 94
  • 789
  • 754
6

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

Let's say I am trying to access web.xml in project/WebContent/WEB-INF/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 )

  2. Now let's use class loader:

    InputStream inStream = class.getClass().getClassLoader().getResourceAsStream("Web-INF/web.xml")
    
Flexo
  • 82,006
  • 22
  • 174
  • 256
webcom
  • 79
  • 1
  • 1
  • This is going to run into problem, as using class will use the system class. – Chad Jul 31 '18 at 02:12
  • This will copy all your web resources to `WEB-INF/classes` and you are still not accessing `WEB-INF/web.xml`, but `WEB-INF/classes/WEB-INF/web.xml`. So despite that configuring the source folder in the IDE will not be enough for many project setups, you will end up with an application which is twice as big and runs in all sorts of duplication problems. – Tobias Liefke Aug 07 '20 at 06:45
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".

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).

Hope this helps someone.

Ted_Zactly
  • 119
  • 1
  • 3
  • Quick update. This worked in eclipse, but failed upon an actual deploy to my tomcat instance. This is what worked in both places: – Ted_Zactly Jun 23 '17 at 20:24
  • Sorry, didn't add the edits in time. This worked in both Eclipse and when deployed to the tomcat instance: ServletContext context; DBConnection = DriverManager.getConnection("jdbc:sqlite:"+context.getRealPath("sample.db")); – Ted_Zactly Jun 23 '17 at 22:16