Getting SessionScoped bean from HttpSessionListener?

前端 未结 1 835
生来不讨喜
生来不讨喜 2021-01-16 04:47

Hey guys. I\'m trying to get a session bean in a HttpSessionListener so that when the user logs out or the session expires I can delete some files that the user created in

相关标签:
1条回答
  • 2021-01-16 05:38

    This code should work fine. Note that the FacesContext isn't necessarily available there since there's not necessarily means of a HTTP request in the thread running at that point, but you already outcommented that. Are you sure that you was actually running the code as shown in the question? Clean, rebuild, redeploy, etc.

    An alternative is to let your UserSessionBean implement HttpSessionBindingListener and then do the job in the valueUnbound() method.

    @ManagedBean
    @SessionScoped
    public class UserSessionBean implements HttpSessionBindingListener {
    
        @Override
        public void valueUnbound(HttpSessionBindingEvent event) {
            for (File file : files) {
                file.delete();
            }
        }
    
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题