Read properties file from outside the .war file

Discussions

Web tier: servlets, JSP, Web frameworks: Read properties file from outside the .war file

  1. Hello all,

    I have a problem reading properties file. I placed .war file in the webapps folder of tomcat container and my properties file is outside the .war file. How can I read the properties file inside war file? 

    I want to place both properties and .war files in the same folder. How can I do this? Or is there any other alternative?

    Thank you all in advance.

  2. For my part I usually pass the reference to a properties file (via a -D... switch) which then contains all external references I need. That file can also contain deployment-specific information such as database references, IP addresses... I then access that file either using a Properties object in my code or a PropertyPlaceholderConfigurer when using Spring.

    I'm not aware of any mean to access a file outside the war (appart from making dangerours assumptions as to what is the current working directory). If someone has a genereal answer to this I'd be glad to hear it.

    Hope this will help you.

  3. I hope it's not too late to clarify this.

    you can try to load your properties file when servlet context being initialized.

    For example: in web.xml code as below.

    <servlet>

                            <servlet-name>AppContextInitializerServlet</servlet-name>

                            <display-name>AppContextInitializer</display-name>

                            <servlet-class>xx.xx.xx. AppContextInitializerServlet</servlet-class>

                            <load-on-startup>1</load-on-startup>

      </servlet>

               

       <servlet-mapping>

          <servlet-name>AppContextInitializerServlet</servlet-name>

          <url-pattern>/initializer</url-pattern>

        </servlet-mapping>

     Create an AppContextInitializerServlet extend HTTP servlet.

    In Init() method try to load all your properties file using system.getProperty(“c:\\system
    file.properties”);

    pls check and let me know whether this helps.

     

    Regards,

    Gani.