如何读取非 servlet java 文件中的上下文参数/web.xml 值?

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

How can I read context parameter/web.xml values in a non-servlet java file?

javaservletsweb.xml

提问by Kirn

I've got a regular java file that I use to update and query a mysql database but I need to take configurable options in that file (like host name, password, etc) and put it in the web.xml file (or perhaps another file if that's an option, but ideally in web.xml).

我有一个常规的 java 文件,用于更新和查询 mysql 数据库,但我需要在该文件中采用可配置的选项(如主机名、密码等)并将其放入 web.xml 文件(或者可能是另一个文件,如果这是一个选项,但最好在 web.xml 中)。

But I don't know how to get access to web.xml values from a regular non-servlet java file.

但我不知道如何从常规的非 servlet java 文件访问 web.xml 值。

Or would I need to read the xml (like any other xml file... or is there a shortcut route to this...)

或者我是否需要阅读 xml(像任何其他 xml 文件一样......或者是否有一个快捷方式......)

回答by Jigar Joshi

One way is to read xml file and parse it.

一种方法是读取 xml 文件并解析它。

You can put it on some static map in after parsing in ServletContextListener

你可以在解析后把它放在一些静态地图上 ServletContextListener

回答by stjohnroe

You need to put the required parameters in env-entry entries of your web.xml file:

您需要将所需的参数放在 web.xml 文件的 env-entry 条目中:

<env-entry> 
    <env-entry-name>dbhost</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>localhost</env-entry-value> 
</env-entry>

and then access them via the jndi context

然后通过 jndi 上下文访问它们

import javax.naming.Context;
import javax.naming.InitialContext;
...
// Get the base naming context
Context env = (Context)new InitialContext().lookup("java:comp/env");

// Get a single value
String dbhost = (String)env.lookup("dbhost");

回答by Ralph

You could use context-parameters in your web.xml and a javax.servlet.ServletContextListener to populate some static fields.

您可以在 web.xml 和 javax.servlet.ServletContextListener 中使用上下文参数来填充一些静态字段。

In you normal java class you read this this static fields.

在普通的 Java 类中,您阅读了这个静态字段。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
...
<context-param>
    <description>Prameter</description>
    <param-name>myParam</param-name>
    <param-value>123456790</param-value>
</context-param>
...
</web-app>

You can access this context parameter with ServletContext.getInitParameter

您可以访问此上下文参数 ServletContext.getInitParameter

回答by Eugene Kuleshov

Create a static class that would be initialized from one of the servlets init.

创建一个静态类,该类将从servlet 中的一个 init 初始化

回答by Hank

Implement a ServletContextListener:

实施一个ServletContextListener

package util;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyConfigListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext ctx = sce.getServletContext();

        String hostname = ctx.getInitParameter("my.config.hostname");

        // now go and do something with that
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {}

}

And don't forget to register it in web.xml:

并且不要忘记在以下位置注册它web.xml

<context-param>
  <param-value>somewhere.example.org</param-value>
  <param-name>my.config.hostname</param-name>
</context-param>
<listener>
  <listener-class>util.MyConfigListener</listener-class>
</listener>