在 JBoss 中部署的 java servlet 中加载属性文件作为战争 [英] Load properties file in a java servlet deployed in JBoss as a war

查看:29
本文介绍了在 JBoss 中部署的 java servlet 中加载属性文件作为战争的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JBoss 4.0.2 中部署了一个 servlet.我有一个已部署应用程序的属性文件.我应该把这个文件放在哪里?jboss serverdefaultconf文件夹下的conf目录下?如何以可移植的方式加载该属性文件?

I have a servlet deployed as a war in JBoss 4.0.2. I have a properties file for the deployed application. Where should I put this file? Under the conf directory in the jboss serverdefaultconf folder? How do I load that properties file in a portable manner?

推荐答案

要以可移植方式加载该属性文件,最好的方法是将其放在 Web 应用程序的类路径中(在 WEB-INF/lib/ 下或 WEB-INF/classes/ 下的 JAR 或应用服务器类路径中,如果您希望能够在不编辑该文件的情况下重新打包您的 Web 应用程序)并使用 Class#getResourceAsStream(String).

To load that properties file in a portable manner, the best way would be to put it on the classpath of the web application (either in a JAR under WEB-INF/lib/ or under WEB-INF/classes/ or on the app server classpath if you want to be able to edit that file without repackaging your web application) and to use Class#getResourceAsStream(String).

以下代码获取一个属性文件的 InputStream,该文件与执行代码的 servlet 位于同一个包中:

The following code gets an InputStream for a property file which resides in the same package as the servlet in which the code is executed:

InputStream inStream = Thread.currentThread().getContextClassLoader()
                 .getResourceAsStream("myfile.properties");

然后,load(InputStream) 把它变成 Properties 对象(跳过异常处理):

Then, load(InputStream) it into a Properties object (skipping Exception handling):

Properties props = new Properties();
props.load(inStream);

这篇关于在 JBoss 中部署的 java servlet 中加载属性文件作为战争的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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