Java 如何从 WebApp 读取 web.xml

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

How to read the web.xml from a WebApp

javaweb-servicesjakarta-eeglassfish

提问by doekman

For WebApps, web.xml can be used to store application settings. How can I read this file. My servlets run in a GlassFish v2 server.

对于 WebApps,web.xml 可用于存储应用程序设置。我怎样才能读取这个文件。我的 servlet 在 GlassFish v2 服务器中运行。

回答by kgiannakakis

Add an init-param:

添加一个初始化参数:

<init-param> 
    <param-name>InitParam</param-name> 
    <param-value>init param value</param-value> 
</init-param> 

Then read it from java code (within a servlet):

然后从java代码(在servlet内)读取它:

String initParam = getServletConfig().getInitParameter("InitParam");

回答by toolkit

Not sure I fully understand this question...

不确定我是否完全理解这个问题......

Assuming your Servlet extends HttpServlet?

假设您的 Servlet 扩展HttpServlet

HttpServletimplements ServletConfig, so you can find out servlet specific parameters using:

HttpServlet实现ServletConfig,因此您可以使用以下方法找出 servlet 特定参数:

In web.xml

在 web.xml 中

<servlet>
    <servlet-class>com.acme.Foo</servlet-class>
    <init-param>
        <param-name>my.init.param</param-name>
        <param-value>10</param-value>
    </init-param>
</servlet>

In servlet:

在 servlet 中:

int x = Integer.parseInt(getInitParameter("my.init.param"));

Similarly, you can get global (context-wide) settings using:

同样,您可以使用以下方法获取全局(上下文范围)设置:

<context-param>
    <param-name>my.context.param</param-name>
    <param-value>Hello World</param-value>
</context-param>

In servlet:

在 servlet 中:

String s = getServletContext.getInitParameter("my.context.param");

Of course, if you're using a framework along with your servlets, such as Spring, then you can use Spring's configuration files instead to inject settings into your web-app classes.

当然,如果您将框架与 servlet 一起使用,例如 Spring,那么您可以使用 Spring 的配置文件来将设置注入到您的 Web 应用程序类中。

回答by Handerson

Doekman, is it possible to explain why you want to read the web.xml file? The settings in this file are targeted to the WebContainer. If you want to pass configuration parameters to be loaded by your application, then just use Context Parameters:

Doekman,能否解释一下为什么要阅读 web.xml 文件?此文件中的设置针对 WebContainer。如果要传递应用程序要加载的配置参数,则只需使用上下文参数:

The optional context-param element declares a Web Application's servlet context initialization parameters. You set each context-param within a single context-param element, using and elements. You can access these parameters in your code using the javax.servlet.ServletContext.getInitParameter() and javax.servlet.ServletContext.getInitParameterNames() methods.

可选的 context-param 元素声明 Web 应用程序的 servlet 上下文初始化参数。您可以使用和元素在单个上下文参数元素中设置每个上下文参数。您可以使用 javax.servlet.ServletContext.getInitParameter() 和 javax.servlet.ServletContext.getInitParameterNames() 方法在代码中访问这些参数。

If you really needs to read the file, then I'm pretty sure you can try to load the file using Java IO. The only thing you need to know is the working path used by Glassfish when your application is running. You could try something like this System.getProperty("user.dir");

如果您确实需要读取文件,那么我很确定您可以尝试使用 Java IO 加载文件。您唯一需要知道的是 Glassfish 在您的应用程序运行时使用的工作路径。你可以试试这样的 System.getProperty("user.dir");

From there you can load the file using a relative path. Examples on www.exampledepot.com.

从那里您可以使用相对路径加载文件。www.exampledepot.com 上的示例。

回答by David Grant

The choice of container shouldn't be relevant to this question, as each container should implement the servlet containerspecification, be it Tomcat, Glassfish or one of the many others.

容器的选择不应该与这个问题相关,因为每个容器都应该实现servlet 容器规范,无论是 Tomcat、Glassfish 还是许多其他容器之一。