Properties file as init-param in web.xml

纵饮孤独 提交于 2019-12-24 14:05:55

问题


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


来源:https://stackoverflow.com/questions/15380817/properties-file-as-init-param-in-web-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!