如何从WebApp中读取web.xml [英] How to read the web.xml from a WebApp

查看:132
本文介绍了如何从WebApp中读取web.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于WebApps,web.xml可用于存储应用程序设置。我如何阅读这个文件。我的servlets运行在GlassFish v2服务器上。

解决方案

不知道我完全理解这个问题...



假设您的Servlet扩展了 HttpServlet



HttpServlet implements ServletConfig ,因此您可以使用以下方法找到servlet特定的参数:

在web.xml中

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

在servlet中:

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

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

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

在servlet中:

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

当然,如果你使用的是像servlet一样的框架,比如Spring,那么你可以使用Spring的配置文件来将设置注入到Web应用程序类中。


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.

解决方案

Not sure I fully understand this question...

Assuming your Servlet extends HttpServlet?

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

In 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:

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:

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.

这篇关于如何从WebApp中读取web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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