how to insert data in database before session timeouts within given time limit using java?

こ雲淡風輕ζ 提交于 2019-12-11 12:23:35

问题


If I am mentioning the session timeout in descriptor page

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

Before terminating session I want to do insert operation in database. Is there any possibility for insertion. I have a thought that when user logins the page and I can use trigger to insert the data for 30 minutes. But if anyone else changes to -1 or more than 30 mins without noticing the trigger is running when user logins at that case what can I do for it?


回答1:


The remaining time for a session timeout changes every time the client makes interaction, so timing this for 30 minutes is a wrong solution.

Instead use an HttpSessionListener. A registered HttpSessionListener is called every time an HTTP session is created or destoryed (invalidated). This does not depend on the configured session timeout. The sessionDestroyed() method is always called right before a session is invalidated and discarded.

public class MySessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {}

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        // Here session is destoryed.
        // You can insert data to your database here.
        // You can access the session like this:
        HttpSession sess = se.getSession();
    }
}

Here is how you can register your HttpSessionListener it in your web.xml:

<web-app>
    <listener>
        <listener-class>com.dummy.MySessionListener</listener-class>
    </listener>
</web-app>


来源:https://stackoverflow.com/questions/25241852/how-to-insert-data-in-database-before-session-timeouts-within-given-time-limit-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!