简体   繁体   English

关闭JSP时调用操作

[英]Call an action when closing a JSP

I'm new to the java web world, so forgive me if I say something stupid. 我是java网络世界的新手,如果我说些愚蠢的话,请原谅我。

I'm working with struts 2 and I need to delete a file (which is located on the server) when a jsp is closed. 我正在使用struts 2,我需要在关闭jsp时删除一个文件(位于服务器上)。

Does any body knows how to do this? 有没有人知道怎么做?

Thanks in advance. 提前致谢。

The window.onunload suggestion is nice, but there is no guarantee that the ajax request will ever hit the server. window.onunload建议很好,但不能保证ajax请求会命中服务器。 As far as I recall, only certain IE versions with certain configurations will send the ajax request successfully. 据我所知,只有某些具有某些配置的IE版本才能成功发送ajax请求。 Firefox and others won't do that. Firefox和其他人不会这样做。 And then I don't talk about the cases when the user has JS disabled. 然后我不讨论用户禁用JS的情况。

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. 您可以在HttpSessionListenerHttpSessionBindingListener帮助下执行此操作,因为它涉及会话的(现有)属性。

Eg 例如

public class CleanupSession implements HttpSessionListener {

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

    // ...
}

(register it in web.xml as a <listener> ) (在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();
    }

    // ...
}

window.onunload ,使用ajax调用删除文件的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM