如何在会话对象被销毁之前调用方法?
开发 JSP 应用程序时,可以定义会话超时值,例如 30 分钟。
超时后,会话对象将被销毁。 此外,我可以通过编程方式调用 session.invalidate() 使会话无效。
由于我在 HTTP 会话中保存了一个复杂的 Java 对象,因此在使会话无效或让 Tomcat 应用服务器过期之前,我需要调用保存的对象方法来释放一些内存。当然,当用户单击注销按钮时,我可以以编程方式执行此操作。
我想做的是在 Tomcat 应用程序服务器要销毁所有过期会话(30 分钟或自定义)时拦截 Tomcat 应用程序服务器,以便我可以预处理会话中保存的 Java 对象,调用特定方法来释放内存。
是否可以?
When developing a JSP application it's possible to define a session timeout value, say 30 minutes.
After that timeout, the session object is destroyed.
Moreover I can programmatically invalidate a session calling session.invalidate() .
Since I'm saving a complex Java object inside the HTTP session, before invalidate the session or let it expire by the tomcat app server, I need to call a saved object method to release some memory. Of course I can do it programmatically when the user click a logout button.
What I would like to do is intercepting the Tomcat app server when it is going to destroy all expired sessions (30 minutes or custom), so that I can pre-process Java objects saved in the session calling a specific method to release memory.
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的。您可以使用
HttpSessionListener
并在sessionDestroyed()
方法中完成工作,或者您可以让存储为会话属性的复杂对象实现
HttpSessionBindingListener
并执行以下操作valueUnbound()
方法中的作业。每当要从会话中删除对象时(通过 HttpSession#removeAttribute() 显式删除或通过会话失效/过期)都会调用它。
Yes, that's possible. You could use
HttpSessionListener
and do the job insessionDestroyed()
method,Or you could let the complex object which is been stored as a session attribute implement the
HttpSessionBindingListener
and do the job invalueUnbound()
method.It will be called whenever the object is to be removed from the session (either explicitly by
HttpSession#removeAttribute()
or by an invalidation/expire of the session).