Best practices to store and access resource files in java web application

16,861

It's perfectly fine to load static resources using the classloader. That's what ResourceBundle does to load the internationalized properties files for example.

Put them in WEB-INF/classes along with your class files, or in a jar inside WEB-INF/lib, and load them with the ClassLoader as indicated by the answers you already read.

That doesn't forbid you to place these files in a separate directory from the Java source files in your project. The build process should just make sure to put them in the appropriate location for runtime. The Maven and Gradle convention is to put the source files under src/main/java and the resource files under src/main/resources.

Share:
16,861
Alexander
Author by

Alexander

Updated on June 30, 2022

Comments

  • Alexander
    Alexander about 2 years

    I have a bunch of text, xml and other files (i.e. resources) that I need to access using servlets in java web app. For example, there is an xml file, a part of which is returned with a servlet by a user query. I am using Tomcat. What is the best practice to store these files and access them from java code? 1) What are the default folders where should I put them, do I need to put them into Web archive or into one of the Jars? 2) How to access the files from java code? How can I set the path to them so it will work in any environment? P.S. I've read a number of posts related to this topic, most of which recommend to store resources in jars and access them using java.lang.Class.getResourceAsStream(String). It seems strange because classes and data should be separated.

  • Alexander
    Alexander over 11 years
    1) What if I already have resources in WEB-INF/resources folder? They are used by JavaScript in HTML page. The page is WEB-INF/index.html and the script refers to resources using relative path. 2)Where should I store the path to the resources folder? Is there a place in web.xml or other static resource?
  • JB Nizet
    JB Nizet over 11 years
    Resources in WEB-INF are not accessible from the outside, so it's not possible to load a JS file in WEB-INF from the browser. You can use ServletContext.getResourceAsStream() to load resources from anywhere in the webapp (including WEB-INF), but that forces the loading class to have access to the ServletContext. I don't understand your second question. It's a static resource. Why don't you hard-code the path in some constant?
  • Alexander
    Alexander over 11 years
    One needs an instance of a servlet to getContext and then get resource or a folder path. Is there a way to do it static without instantiating anything? My second question was related to the place where I should store such constants. Is there a place in some web-related resources such as web.xml (instead of having a constant in one of the class source code)?
  • JB Nizet
    JB Nizet over 11 years
    You could add a filter that would store the current request in a ThreadLocal, and use this ThreadLocal in the code to get access t the request, and then the servlet context. But I woul just use the class loader. What would you gain by externalizing the path instead of using a constant? You would now have two possibilities for a bug: placing the file at the wrong place, or setting a bad value of not value at all in the web.xml. If you really want to do it, then use a context-param (which requires the servletcontext), or an env-entry (that you can lookup using JNDI or get injected as a resource)