开发者

DB Cleanup on Session timeout and logout

开发者 https://www.devze.com 2023-02-14 05:53 出处:网络
Just a quick question. On login I put every user into a db because one time only one user can log in with the same username and password. If this user is already in the db I dont let the user in. When

Just a quick question. On login I put every user into a db because one time only one user can log in with the same username and password. If this user is already in the db I dont let the user in. When the user logs out I clear the dB table and invalidate the session in a Servlet. Now if the user forgot to logout, the session obviously will be expired at eg. 20 minutes so In web.xml I created this:

<listener>
    <listener-class>com.servlets.dbclean</l开发者_JAVA技巧istener-class>
</listener>

This dbclean class then implements HttpSessionListener and on the sessionDestroyed method I clear up the db. My question: is it enough to have this db cleanup happen only here, because when the user logs out manually the session will be invalidated and this method will be called or should I clean up the db at the /doLogout Servlet and rely on this Listener when the user forgot to log out? So is it a save way to use only this Listener?


The listener will be triggered when the session is invalidated - either by timeout or by calling invalidate()

If you don't need to support clustering, you can simply store the logged user in a Set inside the ServletContext (do it in on sessionCreated(..), and then remove it from that Set in sessionDestroyed(..). No need to go to the database - it can all stay in memory. (Using session replication you can do this in a cluster as well)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号