如何从WebApp中读取web.xml [英] How to read the web.xml from a WebApp
问题描述
对于WebApps,web.xml可用于存储应用程序设置。我如何阅读这个文件。我的servlets运行在GlassFish v2服务器上。
不知道我完全理解这个问题...
假设您的Servlet扩展了 HttpServlet
?
在web.xml中 在servlet中: 同样,您可以使用以下方式获取全局(上下文范围)设置: 在servlet中: 当然,如果你使用的是像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 In web.xml In servlet: Similarly, you can get global (context-wide) settings using: In servlet: 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屋! HttpServlet implements
ServletConfig
,因此您可以使用以下方法找到servlet特定的参数:
< servlet>
< servlet-class> com.acme.Foo< / servlet-class>
< init-param>
< param-name> my.init.param< / param-name>
< param-value> 10< /参数值>
< / init-param>
< / 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>
String s = getServletContext.getInitParameter(my.context.param);
HttpServlet
?HttpServlet
implements ServletConfig
, so you can find out servlet specific parameters using:<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>
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>
String s = getServletContext.getInitParameter("my.context.param");