当用户 session 过期时,我需要更改 ServletContext 中的属性。
这里如何建议how-to-call-a-method-before-the-session-object-is-destroyed ,我在我的 java 类中实现了 valueUnbound(HttpSessionBindingEvent event) 方法,该方法允许在使用事件引用销毁 Session 对象之前访问它。
在这个方法中,我需要更改 ServletContext 中的属性数组中的值。我该怎么办?
public class myClass implements HttpSessionBindingListener {
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
int userid = Integer.valueOf((Integer) event.getSession().getAttribute("IDplayer"));
boolean[] id_used = (boolean[]) getServletContext().getAttribute("id_used");
}
问题是 getServletContext().getAttribute() 未找到,即使我包含“import javax.servlet.*”也是如此。
如何从 session 关闭之前调用的方法访问 ServletContext 属性?
最佳答案
通过 HttpSessionBindingEvent 访问它。 使用这个:
event.getSession().getServletContext().getAttribute("id_used");
关于java - 从 valueUnbound(HttpSessionBindingEvent event) 方法修改 ServletContext 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27110577/