有没有什么原因不能在Java ServletContextListener中创建一个变量,它的值设置和获取与其他变量一样。我在SCL中有一个ArrayList,另一个类中的方法经常使用SCL本身中的静态get和set方法更新ArrayList。这里我的首选是不使用ServletContext来存储ArrayList。
根本不会创建侦听器的新实例。
SCL中的代码类似于以下代码:
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;
// ...
}发布于 2010-07-12 02:49:24
你的“变量”必须在你能找到的地方。
如果您在ContextListener中,那么您可以将一个对象put到ServletContext中,并在以后从任何其他可以访问同一个ServletContext的对象中将其get回来。得到它之后,你当然也可以更新它,如果它像ArrayList一样是可变的。
https://stackoverflow.com/questions/3224151
复制相似问题