问题
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:
- get the context root path from ServletContext;
- append the property file path get from init-param;
- 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
- get the file name from init-param
- open the stream this.getClass().getClassLoader().getResourceAsStream("fileName");
- do some file operation
来源:https://stackoverflow.com/questions/15380817/properties-file-as-init-param-in-web-xml