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.
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.
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.
WEB-INF folder is under public_html folder. You can configure and add your content to your site yourself with. class files.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With