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]