Hi! I have a servlet that talks with my server to get the pages to show. For the server to know how to generate the pages from the database it stores one data object for every session. But how do I know when the sessions end? (either when the user logs off, are inactive for 30min or closes the browser). I then want to remove the object on the server and also "log off" the user. Can I get an event or is some method automatically called when an session ends? I use servlets with tea and tomcat. I hope you can help me soon!! /Andreas
public void valueUnbound(HttpSessionBindingEvent event) { String sqlStatement = new String("delete from session_data "); sqlStatement += " where session_id = '" + event.getSession().getId() + "' " ; super.sendCommand(sqlStatement); super.sendCommand("commit"); System.out.println("REMOVED session " + event.getSession().getId() ); } }
Originally posted by Andreas Johansson: Hi! I have a servlet that talks with my server to get the pages to show. For the server to know how to generate the pages from the database it stores one data object for every session. But how do I know when the sessions end? (either when the user logs off, are inactive for 30min or closes the browser). I then want to remove the object on the server and also "log off" the user. Can I get an event or is some method automatically called when an session ends? I use servlets with tea and tomcat. I hope you can help me soon!! /Andreas
Frances Teng
Greenhorn
Joined: Oct 29, 2001
Posts: 9
posted
0
You can add your session listener to you session HttpSession session = request.getSession(false); //... if (session.getValue("SESSION_TRACKER") == null) { session.putValue("SESSION_TRACKER",new SessionTracker()); }
or you can directly add your object to the session session.putValue("NAME",myObj); This way you don't have to worry about listning to the HttpSessionBindingEvent Frances Teng
Just wanted to thank for your replies! =) We will change to Tomcat 4.0 which supports the Servlet 2.3 API. And in that they seem to have a new listener "HttpSessionListener" which have a sessionDestroyed() method! Very nice... But thanks again! /Andreas
jawwad ahmed
Ranch Hand
Joined: Dec 01, 2001
Posts: 179
posted
0
Well there is interface named HttpSessionAttributeListener in servlet 2.3 and has following methods by which imlementing this u can know many things. public void attributeAdded(HttpSessionBindingEvent ev); public void attributeRemoved(HttpSessionBindingEvent ev); public void attributeReplaced(HttpSessionBindingEvent ev); remember by useing this u should make changes in web.xml file. <listener> <listener-class>implemented class name<listener-class> </listener> Value bound and unbound is used when object that is value is added or removed while HttpSessionAttributeListener is used when changes take place in the key.As all attributes are maintaining hashtable implicitly.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.