在 Web 应用程序中保存设置文件

发布于 2024-09-15 22:18:50 字数 186 浏览 1 评论 0原文

对于如何在 J2EE 项目中保留应用程序级别设置/配置文件,是否有任何建议?我有一个 Web 项目,它具有必须写入日志的依赖项和一个本地设置文件。无论如何,是否可以将设置文件存储在本地目录中并使其可写?

[一些依赖项是:hibernate 和 gridgain,因此它们会生成大量日志]

我宁愿不对设置文件的位置进行硬编码。

Are there any recommendations on how to keep an application level settings/configuration file in a J2EE project? I have a web project that has dependences that must write to the logs, and a local settings file. Is there anyway to store the settings file in the local directory and have it writable?

[Some of the dependencies are: hibernate and gridgain, so they produce a lot of logs]

I would rather not hard code the location of the settings file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呆萌少年 2024-09-22 22:18:50

您应该将配置文件放在 WEB-INF 文件夹下。您可以创建一个目录 /WEB-INF/config。如果它们应该位于类路径中,请将它们放入 /WEB-INF/classes 中。通过这种方式,该位置不是硬编码的,而是位于应用程序众所周知的位置。

将它们放入 /WEB-INF 中非常重要,因此浏览器无法“访问”这些文件。


另一种方法可能是向 web.xml 配置添加一个参数,您可以在其中指定配置文件的位置,以便它们不在 webapps 目录内。您可以通过添加 context-param 来做到这一点。例如:

<context-param>
    <param-name>configuration</param-name>
    <param-value>/var/myapp/config</param-value>
</context-param>

该值将作为名为 configuration 的 servlet 上下文初始化参数对您的 Web 应用程序可见。然后,您可以将其用作读取配置文件的基本路径(通过从 ServletContext 调用 getInitParameter 方法获取值)。

但这仅适用于配置文件,而类路径中不需要这些文件。

You should put the configuration files under the WEB-INF folder. You could create a directory /WEB-INF/config. If they should be in the classpath put them to /WEB-INF/classes. By doing it this way, the location is not hardcoded, but at a well known place for your application.

Putting them inside /WEB-INF is important, so the files are not "accessible" by the browser.


Another way could be to add a parameter to the web.xml configuration, where you specify the location of your configuration files so they are not inside the webapps directory. You could do this by adding a context-param. For example:

<context-param>
    <param-name>configuration</param-name>
    <param-value>/var/myapp/config</param-value>
</context-param>

The value will be made visible to your web application as a servlet context initialization parameter named configuration. You can then use it as a base path to read your configuration files (Get the value by calling the getInitParameter method from the ServletContext).

But this will only work for config files, which are not needed in the classpath.

习ぎ惯性依靠 2024-09-22 22:18:50

只需将配置文件的路径添加到 Web 应用程序的类路径即可。这是常见的方法,许多 API 也被设计为从类路径加载配置。对于手动加载这些文件的情况,您可以使用 ClassLoader#getResourceAsStream() 获取类路径资源的 InputStream

首先,在 webapp 目录之外的某个位置创建一个固定路径来放置配置文件,例如 /var/webapp/config。然后配置 servlet 容器以在类路径中包含此路径。对于 Tomcat,您可以在 /conf/catalina.properties 文件的 shared.loadercommon.loader 属性中指定它。

Just add the path to the configuration files to the webapp's classpath. That's the common approach and many API's are also designed to load the configuration from the classpath. For the case you're loading those files manually, you can use ClassLoader#getResourceAsStream() to get an InputStream of the classpath resource.

First, create a fixed path somewhere outside the webapp directory to drop the configurationfiles in, e.g. /var/webapp/config. Then configure the servletcontainer to include this path in the classpath. In case of Tomcat, you can specify it in the shared.loader or common.loader property of the /conf/catalina.properties file.

北陌 2024-09-22 22:18:50

同意 Soundlink 的观点。

您还可以将您的设置存储在数据库中。当您的 servlet 启动时,应从数据库加载设置数据。

  • 优点:无需维护
    非 Java EE 的配置文件
    标准。
  • 缺点:servlet 启动后才起作用
    向上。

Agreed with Soundlink.

You also could store your setting in database. When your servlet starts up, the setting data should be loaded from database.

  • pro: No need to maintain
    configuration files which are not Java EE
    standard.
  • con: It works after servlet starts
    up.
骄傲 2024-09-22 22:18:50

由于应用程序需要支持集群,因此将设置保留在数据库中会更好

Keeping settings in database is better as the application needs to support clustering

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文