Properties file as init-param in web.xml

2019-08-02 12:15发布

问题:

I am migrating a Jsp-servlet based Java project that was hosted in websphere to tomcat. Following init-param is in web.xml inside a filter definition. I moved the properties file src folder which is classpath. How to change the following in the web.xml. Can I define properties file as init-param because most of the answers I saw has used context-param to define properties file. I dont think its an option to me as the existing application needs the properties file to be init-param.

<init-param>
    <param-name>configPath</param-name>
    <param-value>/pws/WebSphere/AppServer/properties/fyp/filterConfig/filter.properties</param-value>
</init-param>

I tried

<init-param>
      <param-name>configPath</param-name>
      <param-value>classpath:filter.properties</param-value>
</init-param>

It did not work.Thank you in advance,

回答1:

Check your servlet implementation,you will find something like the following:

  1. get the context root path from ServletContext;
  2. append the property file path get from init-param;
  3. do some file operation

As you asked,you can config the servlet as :

    <init-param>
      <param-name>configPath</param-name>
      <param-value>filter.properties</param-value>
   </init-param>

then change your code to

  1. get the file name from init-param
  2. open the stream this.getClass().getClassLoader().getResourceAsStream("fileName");
  3. do some file operation