在应用程序范围设置变量,以便在会话中共享 [英] Setting a Variable at Application Scope so it shared among Sessions
本文介绍了在应用程序范围设置变量,以便在会话中共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带有Spring插件的Struts2。我想设置一个变量值,它将在所有不同的会话之间共享。它只是一个字符串值,但如果一个会话更改它,我希望更改的值可用于所有会话。
I am using Struts2 with Spring plugin. I want to set a variable value which will be shared among all the different sessions. It will just be one string value but if one session changes it I want the changed value to be available to all the sessions.
这样做的最佳方法是什么?代码示例会很棒。
What will be the best way to do this? Code Example will be great.
推荐答案
在Servlet代码中:
In the Servlet code:
Object attr = getServletContext().getAttribute("ATTR_NAME");
// Do something with it and...
getServletContext().setAttribute("ATTR_NAME", attr);
这是通用的Java EE Servlet方式;)
This is the generic Java EE Servlet way ;)
这篇关于在应用程序范围设置变量,以便在会话中共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文