Correction,
The following JSP statement
<jsp:useBean id="MyListener" scope="session" class="MyBindingListener" />
wouldn't work as it because the class example I gave did not contain a
default constructor.
The correct way is to:
1. Add a default constructor
2. Add a setter/getter method for ServletContext
3. Use the usebean tag in conjunction with the setProperty tag.
Albert
-----Original Message-----
From: Albert Wong [mailto:[EMAIL PROTECTED]]
Sent: Thursday, September 07, 2000 11:54 AM
To: [EMAIL PROTECTED]
Subject: Re: session
Hi
You can do the following:
1. Create a class implementing the interface:
import javax.servlet.*;
import javax.servlet.http.*;
public class MyBindingListener implements HttpSessionBindingListener {
// Save a reference to your ServletContext assuming you put your
// Database connection pool there.
ServletContext context;
public MyBindingListener(ServletContext context) {
this.context = context;
}
public void valueBound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
DBConnectionPool = (DBConnectionPool)context.getAttribute("DBPool");
DBConnection connection = pool.getConnection();
Statement statement = connection.createStatement();
statement.executeInsert("Recording start of session for " +
session.getId());
connection.close();
}
public void valueUnbound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
DBConnection connection = pool.getConnection();
Statement statement = connection.createStatement();
statement.executeInsert("Recording end of session for " +
session.getId());
connection.close();
}
}
2. Add the object to your session:
HttpSession session = req.getSession(true);
session.setAttribute("MyListener", new
MyBindingListener(getServletContext());
Alternatively if your adding it using JSP:
<jsp:useBean id="MyListener" scope="session" class="MyBindingListener" />
3. That's it. Behind the scenes the following events happens in the
HttpSession:
public void setAttribute(String name, Object value) {
// Remove the attribute if it already exists
// store the attribute in the session (generally it's a hashtable)
// if "value" is an instanceof HttpSessionBindingListener
// call it's valueBound method
}
public void removeAttribute(String key) {
// Retreive the object from session (eg. hashtable)
// Remove the object from the session
// If the object is an instance of HttpSessionBindingListener
// call it's valueUnbound method
}
Albert
-----Original Message-----
From: Angela Chow [mailto:[EMAIL PROTECTED]]
Sent: Thursday, September 07, 2000 9:28 AM
To: [EMAIL PROTECTED]
Subject: Re: session
thanks for your reply :)
i am very new to this concept. Is there anywhere that
i can learn a little bit more about this? I've notice
that Tomy Wong has put some code on his reply:
HttpSession session = HttpServletRequest.getsession();
session.putValue("listenerobjcet",listener);
HttpSessionBindingEvent sessionbind = new
HttpSessionBindingEvent(session,"listener");
session.removeValue("listener");
I don't know where to put this code, is the jsp page,
or in the bean? If in the jsp page, is it on every
page? How do i set the length of time for the
time-out? I am getting little confused. Please help
me out!
angela
--- Albert Wong <[EMAIL PROTECTED]> wrote:
> Consider using
> javax.servlet.http.HttpSessionBindingListener.
>
> When a session is invalidated (either the user
> logged out resulting in your
> explictly invalidating their session, or because
> their session timed out)
> and is about to be removed, the servlet container
> iterates through the
> contents of your user's session, calling
> HttpSessionBindingListener.ValueUnBound on any
> objects in the session that
> implement the HttpSessionBindingListener interface.
>
> So, in your case if the user does not log out, then
> their session will
> eventually expire. When that happens, if you put an
> object implementing
> HttpSessionBindingListener into the session, it's
> ValueUnBound method get's
> called. Within this callback, you can do your JDBC
> update)
>
> Albert
>
>
> -----Original Message-----
> From: JSP Insider [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, September 06, 2000 5:54 PM
> To: [EMAIL PROTECTED]
> Subject: Re: session
>
>
> Do you have cookies enabled?
>
> On the close out question. You can use Javascript,
> put code in the document
> close event. This code can trigger opening another
> page which then performs
> the required operation. (the other page can then
> close itself in the open
> event.) We have coded something similiar for an ASP
> app we built a while
> ago, so I know, that it should work. At least in
> theory...
> Other than coding a trick like this, you cant know
> when the using just shuts
> the window. one of the disadvantages of a web
> application.
>
> Casey Kochmer
> [EMAIL PROTECTED]
>
>
> >From: Angela Chow <[EMAIL PROTECTED]>
> >Reply-To: A mailing list about Java Server Pages
> specification and
> > reference <[EMAIL PROTECTED]>
> >To: [EMAIL PROTECTED]
> >Subject: session
> >Date: Wed, 6 Sep 2000 16:43:56 -0700
> >
> >hi,
> >
> >i need some help with the session. For some
> reason,
> >the session id is not unique everytime i bring up
> the
> >page when i use session.getId(); How do i make it
> >unique? Another problem, i have to update the
> database
> >when the user has logged out, i could do it when
> they
> >click to the logout.jsp page, but what if they just
> >close up the window. How do i know that they close
> up
> >the window and thus update the database?
> >
> >
> >thanks in advance!
> >
> >angela
> >
> >
> >__________________________________________________
> >Do You Yahoo!?
> >Yahoo! Mail - Free email you can access from
> anywhere!
> >http://mail.yahoo.com/
> >
>
>===========================================================================
> >To unsubscribe: mailto [EMAIL PROTECTED] with
> body: "signoff
> >JSP-INTEREST".
> >Some relevant FAQs on JSP/Servlets can be found at:
> >
> > http://java.sun.com/products/jsp/faq.html
> > http://www.esperanto.org.nz/jsp/jspfaq.html
> >
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >
>
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
>
_________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at
> http://www.hotmail.com.
>
> Share information about yourself, create your own
> public profile at
> http://profiles.msn.com.
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with
> body: "signoff
> JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>
>
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with
> body: "signoff JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
__________________________________________________
Do You Yahoo!?
Yahoo! Mail - Free email you can access from anywhere!
http://mail.yahoo.com/
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets