Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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.

like image 830
Michael Wiles Avatar asked Jul 10 '09 08:07

Michael Wiles


People also ask

How do I read properties file from WEB-INF classes?

To be able to read a properties file stored under WEB-INF folder we will need to access ServletContext. We will do it by injecting the ServletContext into the Root Resource Class with the @Context annotation.

What is WEB-INF folder in Web application?

WEB-INF. This directory, which is contained within the Document Root, is invisible from the web container. It contains all resources needed to run the application, from Java classes, to JAR files and libraries, to other supporting files that the developer does not want a web user to access.

Where is WEB-INF folder in Linux?

WEB-INF folder is under public_html folder. You can configure and add your content to your site yourself with. class files.

How do I read a file from WEB-INF?

If its under your WEB-INF directory, one way to get it is via the ServletContext. This method can access any file in your web application.


1 Answers

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.

like image 149
skaffman Avatar answered Sep 21 '22 10:09

skaffman