java 属性文件作为 web.xml 中的 init-param

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15380817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:23:33  来源:igfitidea点击:

Properties file as init-param in web.xml

javajakarta-eeservletsservlet-filtersweb.xml

提问by TV Nath

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.

我正在将托管在 websphere 中的基于 Jsp-servlet 的 Java 项目迁移到 tomcat。init-param 位于过滤器定义内的 web.xml 中。我移动了类路径的属性文件 src 文件夹。如何更改 web.xml 中的以下内容。我可以将属性文件定义为 init-param 吗,因为我看到的大多数答案都使用 context-param 来定义属性文件。我不认为这对我来说是一个选择,因为现有的应用程序需要属性文件是 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,

它没有用。提前谢谢你,

回答by Harry.Chen

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

检查您的 servlet 实现,您会发现如下内容:

  1. get the context root path from ServletContext;
  2. append the property file path get from init-param;
  3. do some file operation
  1. 从 ServletContext 获取上下文根路径;
  2. 追加从 init-param 获取的属性文件路径;
  3. 做一些文件操作

As you asked,you can config the servlet as :

正如您所问,您可以将 servlet 配置为:

    <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
  1. 从 init-param 获取文件名
  2. 打开流 this.getClass().getClassLoader().getResourceAsStream("fileName");
  3. 做一些文件操作