关闭JSP时调用动作 [英] Call an action when closing a JSP

查看:72
本文介绍了关闭JSP时调用动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java网络世界的新手,所以如果我说些蠢话,请原谅我.

I'm new to the java web world, so forgive me if I say something stupid.

我正在使用struts 2,并且在关闭jsp时需要删除文件(位于服务器上).

I'm working with struts 2 and I need to delete a file (which is located on the server) when a jsp is closed.

任何人都知道该怎么做吗?

Does any body knows how to do this?

谢谢.

推荐答案

window.onunload的建议很好,但是不能保证ajax请求会到达服务器.据我所知,只有具有特定配置的某些IE版本才能成功发送ajax请求. Firefox和其他人不会这样做.然后我不谈论用户禁用JS的情况.

The window.onunload suggestion is nice, but there is no guarantee that the ajax request will ever hit the server. As far as I recall, only certain IE versions with certain configurations will send the ajax request successfully. Firefox and others won't do that. And then I don't talk about the cases when the user has JS disabled.

您不想依靠它.而是挂钩会话到期.您可以在 HttpSessionListener 或在涉及时 HttpSessionBindingListener 会话的(现有)属性.

You don't want to rely on that. Rather hook on the session expiration. You can do that with help of HttpSessionListener or maybe HttpSessionBindingListener when it concerns an (existing) attribute of the session.

例如

public class CleanupSession implements HttpSessionListener {

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        new File(somePath).delete();
    }

    // ...
}

(在web.xml中将其注册为<listener>)

或者,对于已登录用户"示例(存储在会话范围中):

Or, in case of an "logged-in user" example (which is stored in the session scope):

public void User implements HttpSessionBindingListener {

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        new File(somePath).delete();
    }

    // ...
}

这篇关于关闭JSP时调用动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆