Hi,
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.
Hi,
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.
On window.onunload call, using ajax, an action that deletes the file.
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();
    }
    // ...
}