如何从servlet中可移植地读取配置数据 [英] How to portably read configuration data from a servlet

查看:102
本文介绍了如何从servlet中可移植地读取配置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java servlet,需要读取一些特定于站点的
配置数据;我希望系统管理员在部署时能够轻松访问/修改
。没有合理的默认值,
所以数据必须由网站管理员提供。

它包含一些字符串键/值对(想想属性) 。
它只会被读取一次(在初始化时)。

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 to be 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).

我知道这个问题
ServletContext .getInitParameter()机制,但就我的理解而言,只要
,它们需要将数据捆绑在
servlet包中(作为属性文件,或在
web.xml ),这使得升级servlet代码变得不方便。

I'm aware of this SO question and 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.

有没有servlet获取这种
键/值配置数据的任何标准接口?如果编程
接口在任何地方都相同,那就没问题了,但设置
配置数据的实际方法取决于所使用的实际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.

我最喜欢便携式解决方案,但我会满足于
仅适用于Tomcat和Jetty的东西。

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

推荐答案

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

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

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

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

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

For Jetty you can add the following to your jetty.xml to add the JNDI parameter 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> 

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

Then in your servlet you can read the JNDI parameter:

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");
}

设置JNDI值的方法因其他应用程序服务器而异,但要读取的代码配置总是一样的。

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

这篇关于如何从servlet中可移植地读取配置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆