How to delete data from the database after closing the browser

When a user logs into my web application, I create a session:

session.setAttribute("SessionNumber","100"); 

And his username is added to the table named ONLINE_USERS. Other Internet users will be able to see it, they see all online users.

When the user clicks the logout button, I delete this row from the table and then delete the session using:

 session.invalidate(); 

But let's say that the user existed in the browser, his session will not be, but the row will remain in the database as an online user, how can this be avoided?

I use JSP servlets on Netbeans.

+4
source share
3 answers

You can enable a custom HttpSessionListener to delete a table row after an invalid session.

 public class YourHttpSessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { //put row in the database } public void sessionDestroyed(HttpSessionEvent event) { //delete the row from database } } 

Declare a listener in your web.xml:

 <listener> <listener-class>YourHttpSessionListener</listener-class> </listener> 

Please note that there will be a delay between the moment the user exits the browser and his session on the server. But the session end time is configurable. You should find a suitable expiration timeout: not too long so that you do not display too many offline users as online, but not too short to allow connected users downtime.

I think this is a good compromise for a chat application developed using the basic servlet and jsp technologies.

+6
source

As I understand it, you want to see the users who are currently working on the website, the problem with the HttpSessionListener is that the session can live for a long time before it is destroyed, so it may happen that the user does not use the web for a long time site, it is destroyed. (see http://www.smartsoftwarebits.com/qaa/46-misc/82-how-to-set-session-timeout-for-tomcat )

Solution . You can add a column to the database, where you save the time stamp of the last request that the user made. To update this column, use the servlet filter. ( http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html )

To clear online users, add a timer job (e.g. using quartz) where you delete lines (online users) that are older (e.g.) 5 minutes (so when the last interaction is older than 5 minutes) ( http: // quartz -scheduler.org/ )

Using this, you are now absolutely certain if the user is "still there" or not.

Alternatively, you can add a client-side javascript timer to periodically make an ajax call. You can deal in this way when the situation where the user has not closed the browser is inactive for some time.

+1
source

First of all, you need to catch an event when the browser is closed.

You can try under the code snippet in jsp to get into the js function that will call the ajax function to hit the server side component. Then simply use the session API to cancel the session and add code to remove the record from the table.

 window.onbeforeunload = WindowClose; function WindowClose() { //Write a AJAx request here to hit the server side servlet to invalidate the session } 

Or use

 <body onunload="WindowClose(); > 

In server side code use

HttpSession session = request.getsession (); session.setMaxInactiveInterval (0); // or session.invalidate ();

A good approach is to determine the default session timeout value in the web.xml file so that the browser crashes, the sessions are invalid after a specified period of time.

 <session-config> <session-timeout>30</session-timeout> </session-config> 
+1
source

All Articles