Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load properties file in a java servlet deployed in JBoss as a war

I have a servlet deployed as a war in JBoss 4.0.2. I have a properties file for the deployed application. Where should I put this file? Under the conf directory in the jboss server\default\conf folder? How do I load that properties file in a portable manner?

like image 484
randomperson25 Avatar asked Jan 06 '10 18:01

randomperson25


People also ask

Can we deploy WAR file in JBoss?

After you configure all properties files in WAR files, you must deploy these WARs through the app server. IMPORTANT: The following configuration is same for all app servers, except the deployment paths for app servers. To deploy configured console .

How does JBoss deploy war?

So whenever we place a war file in the deployment folder, it's deployed automatically. JBoss creates the . deployed marker file automatically which indicates that the content has been deployed. However, if we remove the previous deployment before copying a new war file to the deployment folder, JBoss will create an .


4 Answers

To load that properties file in a portable manner, the best way would be to put it on the classpath of the web application (either in a JAR under WEB-INF/lib/ or under WEB-INF/classes/ or on the app server classpath if you want to be able to edit that file without repackaging your web application) and to use Class#getResourceAsStream(String).

The following code gets an InputStream for a property file which resides in the same package as the servlet in which the code is executed:

InputStream inStream = Thread.currentThread().getContextClassLoader()
                 .getResourceAsStream("myfile.properties");

Then, load(InputStream) it into a Properties object (skipping Exception handling):

Properties props = new Properties();
props.load(inStream);
like image 180
Pascal Thivent Avatar answered Oct 31 '22 11:10

Pascal Thivent


Just get hold of the servletContext and then

InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/log4j.properties");
Properties props = new Properties();
props.load(stream);

This will always work, regardless of whether you deploy a war or exploded war.

like image 36
julius Avatar answered Oct 31 '22 10:10

julius


If the properties file can be deployed along with the application make it part of your source tree. This will result in the properties file to be in the WEB-INF/classes folder.

This can then be read using

Properties properties = loadProperties("PropertyFileName.properties", this.getClass());
...

public static Properties loadProperties(String resourceName, Class cl) {
    Properties properties = new Properties();
    ClassLoader loader = cl.getClassLoader();
    try {
        InputStream in = loader.getResourceAsStream(resourceName);
        if (in != null) {
            properties.load(in);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return properties;
}
like image 2
Vinodh Ramasubramanian Avatar answered Oct 31 '22 10:10

Vinodh Ramasubramanian


The best place to put it is under the web-apps' own doc-root, like "./WEB-INF/myapp.properties", i.e. relative to where the servlet container unpacked your .war or .ear file. You can provide the properties file directly in the .war.

The ServletContext has a method getRealPath(String path) that returns the actual path in the filesystem. Using the real path you can load it in a Properties collection.

Update The code in your comment tries to lookup real path for "/", you should ask for the relative path to your properties file, as in:

String propertiesFilePath = getServletContext().getRealPath("WEB-INF/application.properties");
Properties props = properties.load(new FileInputStream(propertiesFilePath));
like image 1
rsp Avatar answered Oct 31 '22 11:10

rsp