How to end sessions automatically if user closes the browser

java, jsp, servlets, session

Solution

Avoid manual code for that.

simply in `web.xml` `//this will applied for whole application`

<session-config>
<session-timeout>10</session-timeout>
</session-config>

Detect browser close event and call invalidate method

if(session!=null) { 
session.invalidate();   
}   

Problem

I need to end the session automatically when the user is inactive for some particular time say for 10 minutes. We have a method in ``` HttpSession session=request.getSession(); session.setAttribute("User", au); session.setAttribute("name", firstname); response.sendRedirect("doLogin.jsp"); session.setMaxInterval(); ``` but this will end the session even if the user is active for 10 minutes. How can I end the session when the user closes the browser?

Original source

Related problems