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.
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.
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, from a JSP?
If you're inside a Servlet object, then call getServletConfig().getServletContext()
. If you're in JSP, I think you can just refer to the servletContext
variable, it's automatically there.
Your classes
directory is the default home directory of the application, and the first place it looks for files. I haven't touched this stuff for a while, but I think you can either include ".." in the application's classpath argument, or just access the file as "../myfile".