使用Context.xml存储动态变量值 [英] Use Context.xml to store dynamic variable value

查看:179
本文介绍了使用Context.xml存储动态变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web应用程序中,我必须使用context.xml注册要在JSP中使用的变量.例如,值为"xfromcontext"的变量X,这样我就可以在JSP中将其打印为out.println(X).如何从context.xml获取X的值?

In my webapp, I must use the context.xml to register a variable which is to be used in a JSP. For example, a variable X with the value "xfromcontext", so that I can print it in JSP as out.println(X). How do I get the X's value from context.xml?

推荐答案

您可以指定<Environment>条目,该条目与web.xml中的常规<env-entry>完全相同.

You can specify an <Environment> entry which does exactly the same as regular <env-entry> in web.xml.

例如

<Environment name="X" value="xfromcontext" type="java.lang.String" />

可通过

String X = (String) new InitialContext().lookup("java:comp/env/X");
// ...

我建议阅读一次并将其存储在webapp启动时的应用程序范围内.您可以为此使用ServletContextListener.然后,您可以通过${X}在JSP中访问它.在JSP中执行out.println()是一种相当古老的方法,十年前一直不鼓励这样做.

I'd suggest to read it once and store it in the application scope on webapp's startup. You can use a ServletContextListener for this. Then you can just access it in JSP by ${X}. Doing a out.println() in JSP is a rather old school way which has been discouraged over a decade ago.

这篇关于使用Context.xml存储动态变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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