Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an action when closing a JSP

Tags:

java

jsp

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

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?

Thanks in advance.

like image 550
Ecarrion Avatar asked Aug 04 '10 20:08

Ecarrion


2 Answers

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.

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.

E.g.

public class CleanupSession implements HttpSessionListener {

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

    // ...
}

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

    // ...
}
like image 107
BalusC Avatar answered Sep 23 '22 07:09

BalusC


On window.onunload call, using ajax, an action that deletes the file.

like image 21
Bozho Avatar answered Sep 22 '22 07:09

Bozho