Re: invalidate session after calling listeners
Hassan, How do I add an instance of the listener to each session? Can you please provide an example? I forgot to mention that I already have the following in the first JSP after the login is validated: jsp:useBean id=listener class=abcd.AbcdSessionListener scope=session / % session.setAttribute(sessionListener, listener); % What you say, ...and that object receives the event, it still knows its attributes. What do you mean by object? Which object? Thanks. Hassan Schroeder wrote: Franklin Phan wrote: I'm trying to code a method to clean up specifically named files inside a working dir (in Windows XP) whenever the session times out. Rather than a global listener approach, why not just add an instance of your listener *to each session*? When the session ends and that object receives the event, it still knows its attributes; from your example: public void valueUnbound(HttpSessionBindingEvent se) { AbcdUtil util = new AbcdUtil(); /* -- neither of these is necessary, as userId is still defined * HttpSession session = se.getSession(); * String userId = (String)session.getValue(userId); */ I use this approach to return items to stock from an abandoned (via session timeout) shopping cart, for instance. FWIW! - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: invalidate session after calling listeners
Hassan, Also, I don't understand the difference between a global and a non-global listener approach. Can you explain? Thanks. Thanks. Hassan Schroeder wrote: Franklin Phan wrote: I'm trying to code a method to clean up specifically named files inside a working dir (in Windows XP) whenever the session times out. Rather than a global listener approach, why not just add an instance of your listener *to each session*? When the session ends and that object receives the event, it still knows its attributes; from your example: public void valueUnbound(HttpSessionBindingEvent se) { AbcdUtil util = new AbcdUtil(); /* -- neither of these is necessary, as userId is still defined * HttpSession session = se.getSession(); * String userId = (String)session.getValue(userId); */ I use this approach to return items to stock from an abandoned (via session timeout) shopping cart, for instance. FWIW! - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: invalidate session after calling listeners
Hassan, I have found the solution. I think that a big part of what you were saying was something that I was already doing but neglected to mention (i.e., having a line of code in the JSP to bind the listener object to the session using setAttribute). Your commenting out my two lines of code did hint to me to take a closer look at the Servlet API Documentation. The API documentation for HttpSession interface says: When an application stores an object in or removes an object from a session, the session checks whether the object implements HttpSessionBindingListener. If it does, the servlet notifies the object that it has been bound to or unbound from the session. Reading that again did clarify for me what it means for an object to implement HttpSessionBindingListener and led me to add the following: session.getServletContext().getRealPath(XML_WORK_PATH) to the valueBound method to set an instance variable because that's where the session object is still valid. I then removed the two lines of code from valueUnbound as you indicated. Thanks. What threw me off in the first place was the poor API documentation for HttpSessionBindingListener interface. It says for valueUnbound: Notifies the object that it is being unbound from a session and identifies the session. It gave me the impression that the method itself notifies the object (which is the object that contains the implementation of HttpSessionBindingListener itself) and provides a reference to the session. The API documentation for that method should have said something like: This method is called *upon receiving notification* that this object is being unbound from the *invalidated* session. Thanks, again. Still, though, what is a global listener approach? Franklin Phan wrote: Hassan, How do I add an instance of the listener to each session? Can you please provide an example? I forgot to mention that I already have the following in the first JSP after the login is validated: jsp:useBean id=listener class=abcd.AbcdSessionListener scope=session / % session.setAttribute(sessionListener, listener); % What you say, ...and that object receives the event, it still knows its attributes. What do you mean by object? Which object? Thanks. Hassan Schroeder wrote: Franklin Phan wrote: I'm trying to code a method to clean up specifically named files inside a working dir (in Windows XP) whenever the session times out. Rather than a global listener approach, why not just add an instance of your listener *to each session*? When the session ends and that object receives the event, it still knows its attributes; from your example: public void valueUnbound(HttpSessionBindingEvent se) { AbcdUtil util = new AbcdUtil(); /* -- neither of these is necessary, as userId is still defined * HttpSession session = se.getSession(); * String userId = (String)session.getValue(userId); */ I use this approach to return items to stock from an abandoned (via session timeout) shopping cart, for instance. FWIW! - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: invalidate session after calling listeners
Franklin Phan wrote: I have found the solution. Cool. :-) What threw me off in the first place was the poor API documentation for HttpSessionBindingListener interface. It says for valueUnbound: Notifies the object that it is being unbound from a session and identifies the session. It gave me the impression that the method itself notifies the object (which is the object that contains the implementation of HttpSessionBindingListener itself) and provides a reference to the session. The API documentation for that method should have said something like: This method is called *upon receiving notification* that this object is being unbound from the *invalidated* session. Erm, yeah, the docs are sometimes a bit opaque. Still, though, what is a global listener approach? I was just making a distinction between having a single Context-wide process (listener) doing session-unbound tasks versus each /session/ having a bound object implementing HttpSessionBindingListener doing its own. It's the difference between Mom cleaning up after everyone, or all the kids cleaning their own rooms :-) HTH! -- Hassan Schroeder - [EMAIL PROTECTED] Webtuitive Design === (+1) 408-938-0567 === http://webtuitive.com dream. code. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: invalidate session after calling listeners
Franklin Phan wrote: I'm trying to code a method to clean up specifically named files inside a working dir (in Windows XP) whenever the session times out. Rather than a global listener approach, why not just add an instance of your listener *to each session*? When the session ends and that object receives the event, it still knows its attributes; from your example: public void valueUnbound(HttpSessionBindingEvent se) { AbcdUtil util = new AbcdUtil(); /* -- neither of these is necessary, as userId is still defined * HttpSession session = se.getSession(); * String userId = (String)session.getValue(userId); */ I use this approach to return items to stock from an abandoned (via session timeout) shopping cart, for instance. FWIW! -- Hassan Schroeder - [EMAIL PROTECTED] Webtuitive Design === (+1) 408-938-0567 === http://webtuitive.com dream. code. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
invalidate session after calling listeners
Is there a way to set Tomcat to call listeners before invalidate() is called on a session? I'm trying to code a method to clean up specifically named files inside a working dir (in Windows XP) whenever the session times out. I can't seem to find a way to do it. Apparently, invalidate() is called prior to calling listeners, and these specifically named files that I want to clean up are named after the user ID that is stored in the HttpSession object (meaning I'd need to access the session object. I understand Resin has a setting called invalidate-after-listener and am wondering whether Tomcat has same. Thanks. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: invalidate session after calling listeners
You could implement HttpSessionBindingListener and define your own valueBound and valueUnbound methods. DarekC On Fri, 2005-08-26 at 13:08, Franklin Phan wrote: Is there a way to set Tomcat to call listeners before invalidate() is called on a session? I'm trying to code a method to clean up specifically named files inside a working dir (in Windows XP) whenever the session times out. I can't seem to find a way to do it. Apparently, invalidate() is called prior to calling listeners, and these specifically named files that I want to clean up are named after the user ID that is stored in the HttpSession object (meaning I'd need to access the session object. I understand Resin has a setting called invalidate-after-listener and am wondering whether Tomcat has same. Thanks. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: invalidate session after calling listeners
Darek, I've tried your suggestion. As I've said before: I need to access the Session object. This is what I have: package abcd; import java.io.*; import javax.servlet.http.*; public class AbcdSessionListener implements HttpSessionBindingListener { private String userId; public void valueBound(HttpSessionBindingEvent se) { HttpSession session = se.getSession(); userId = (String)session.getValue(userId); } public void valueUnbound(HttpSessionBindingEvent se) { AbcdUtil util = new AbcdUtil(); HttpSession session = se.getSession(); // ---This is not possible because the session is already invalidated at this point. String userId = (String)session.getValue(userId); // Clean up the folder of old XML files by the same user from previous login (uses the AbcdUtil class) String XML_WORK_PATH = /WEB-INF/work_xml; File[] files = util.fileSearch(session.getServletContext().getRealPath(XML_WORK_PATH), userId + _*.*); for (int i = 0; i files.length; i++) { files[i].delete(); } } } // End AbcdSessionListener And I use the following in a JSP: jsp:useBean id=listener class=abcd.AbcdSessionListener scope=session / % session.setAttribute(sessionListener, listener); % All this fails with or without a listener element in the web.xml entry (and how should one decide whether or not to put one in web.xml?). If you read the Servlet API Documentation for the HttpSession Interface (http://jakarta.apache.org/tomcat/tomcat-5.5-doc/servletapi/index.html), it says: For session that are invalidated or expire, notifications are sent after the session has been invalidated or expired. Pretty useless, if you ask me. Any advice? Franklin Darek Czarkowski wrote: You could implement HttpSessionBindingListener and define your own valueBound and valueUnbound methods. DarekC On Fri, 2005-08-26 at 13:08, Franklin Phan wrote: Is there a way to set Tomcat to call listeners before invalidate() is called on a session? I'm trying to code a method to clean up specifically named files inside a working dir (in Windows XP) whenever the session times out. I can't seem to find a way to do it. Apparently, invalidate() is called prior to calling listeners, and these specifically named files that I want to clean up are named after the user ID that is stored in the HttpSession object (meaning I'd need to access the session object. I understand Resin has a setting called invalidate-after-listener and am wondering whether Tomcat has same. Thanks. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]