java 如何从 servlet 可移植地读取配置数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3804396/
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-30 03:26:55  来源:igfitidea点击:

How to portably read configuration data from a servlet

javaservletspropertiesservletconfig

提问by Riccardo Murri

I'm writing a Java servlet that needs to read some site-specific configuration data; I would like it to be easily accessible/modifiable by the sysadmins at deployment time. There is no sensible default, so the data has tobe provided by the site admin.
It consists of a few string key/value pairs (think Properties). It would only be read once (at initialization time).

我正在编写一个 Java servlet,它需要读取一些特定于站点的配置数据;我希望系统管理员在部署时可以轻松访问/修改它。没有合理的默认值,因此数据必须由站点管理员提供。
它由几个字符串键/值对组成(想想属性)。它只会被读取一次(在初始化时)。

I'm aware of this SO questionand the ServletContext.getInitParameter()mechanism, but as far as my understanding goes, they require the data to be bundled in the servlet package (either as a properties file, or specified in the web.xml), which makes it inconvenient to upgrade the servlet code.

我知道这个 SO 问题ServletContext.getInitParameter()机制,但据我所知,它们要求将数据捆绑在 servlet 包中(作为属性文件,或在 中指定 web.xml),这使得升级不方便servlet 代码。

Is there any "standard" interface for a servlet to get this kind of key/value configuration data? It would be ok if the programming interface is the same everywhere, but the actual way of setting the configuration data depends on the actual servlet container being used.

servlet 是否有任何“标准”接口来获取这种键/值配置数据?如果编程接口在任何地方都相同就可以了,但是设置配置数据的实际方式取决于所使用的实际 servlet 容器。

I'm looking preferably at portable solutions, but I'd be content with something that only works in Tomcat and Jetty.

我更喜欢便携式解决方案,但我对仅适用于 Tomcat 和 Jetty 的东西感到满意。

回答by vanje

The recommended way to configure an application server for a web application is per JNDI.

为 Web 应用程序配置应用程序服务器的推荐方法是按 JNDI。

Every application server (including Jetty and Tomcat) allows you to configure JNDI parameters.

每个应用程序服务器(包括 Jetty 和 Tomcat)都允许您配置 JNDI 参数。

For Jetty you can add the following to your jetty.xml to add the JNDI parameter param.file:

对于 Jetty,您可以将以下内容添加到您的 jetty.xml 以添加 JNDI 参数param.file

<!--  JNDI java:comp/env --> 
<New id="param.file" class="org.mortbay.jetty.plus.naming.EnvEntry">
  <Arg>param.file</Arg> 
  <Arg type="java.lang.String"><SystemProperty name="jetty.home" default="."/>etc/config.properties</Arg> 
  <Arg type="boolean">true</Arg> 
</New> 

Then in your servlet you can read the JNDI parameter:

然后在您的 servlet 中,您可以读取 JNDI 参数:

import javax.naming.InitialContext;
import javax.naming.NamingException;

...

public Object readJndi(String paramName) {
  Object jndiValue = null;
  try {
    final InitialContext ic = new InitialContext();
    jndiValue = ic.lookup("java:comp/env/" + paramName);
  } catch (NamingException e) {
    // handle exception
  }
  return jndiValue;
}


public String getConfigPath() {
  return (String) readJndi("param.file");
}

The way to set JNDI values differs for other application servers but the code to read the configuration is always the same.

其他应用程序服务器设置 JNDI 值的方式不同,但读取配置的代码始终相同。

回答by mhaller

The Servlet init parameters are the right (and standardized) wayof defining properties which can be configured by the administrator. Many of the application servers provide a GUI backend where the parameters can be configured.

servlet初始化参数是正确的(和标准化)的方式定义其可以由管理员配置属性。许多应用程序服务器都提供了一个 GUI 后端,可以在其中配置参数。

For an example for Tomcat, see Defining Tomcat servlet context parameters

有关 Tomcat 的示例,请参阅定义 Tomcat servlet 上下文参数

回答by Bozho

  • Configure the external location of the properties - either via a jvm argument (when starting the servlet container), or in the web.xml

  • in the external location use config.propertiesand read it with java.util.Properties

  • 配置属性的外部位置 - 通过 jvm 参数(启动 servlet 容器时),或在 web.xml 中

  • 在外部位置使用config.properties并阅读它java.util.Properties

回答by PeterMmm

You may take Preferences or hack with user.home, user.dir, etc. But for a few key/value keep things simple.

您可以通过 user.home、user.dir 等使用 Preferences 或 hack。但是对于一些键/值,请保持简单。

Write a small Singleton to wrap around Properties and load them from a fix & absolute location

编写一个小的单例来环绕属性并从固定和绝对位置加载它们

public class LocalConfig extends Properties {

  public static LocalConfig $ = new LocalConfig();

  private LocalConfig() throws IOException {
    load(new File("/etc/myconfig.properties"));
  }

}