首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ServletContextListener中变量的使用

ServletContextListener中变量的使用
EN

Stack Overflow用户
提问于 2010-07-12 02:17:41
回答 1查看 1.5K关注 0票数 1

有没有什么原因不能在Java ServletContextListener中创建一个变量,它的值设置和获取与其他变量一样。我在SCL中有一个ArrayList,另一个类中的方法经常使用SCL本身中的静态get和set方法更新ArrayList。这里我的首选是不使用ServletContext来存储ArrayList。

根本不会创建侦听器的新实例。

SCL中的代码类似于以下代码:

代码语言:javascript
复制
private static ArrayList<String> strList;

@Override
public void contextInitialized(ServletContextEvent contextEvent) { 
    ArrayList<String> temp = someOtherMethod(); 
    setStrList(temp);
}

@Override
public void contextDestroyed(ServletContextEvent contextEvent) {        
}

public static ArrayList<String> getStrList() {
   // ...
   return strList;
}

public static void setStrList(ArrayList<String> temp) {
   this.strList = temp;
   // ... 
}
EN

回答 1

Stack Overflow用户

发布于 2010-07-12 02:49:24

你的“变量”必须在你能找到的地方。

如果您在ContextListener中,那么您可以将一个对象putServletContext中,并在以后从任何其他可以访问同一个ServletContext的对象中将其get回来。得到它之后,你当然也可以更新它,如果它像ArrayList一样是可变的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3224151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档