Meaningless Drivel is fun!
The moose likes Servlets and the fly likes using HttpSessionBindingListener interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "using HttpSessionBindingListener interface" Watch "using HttpSessionBindingListener interface" New topic

using HttpSessionBindingListener interface

karim qazi

Joined: Mar 22, 2001
Posts: 20
I am implementing HttpSessionBindingListener interface and I want to know when sessions are being created and dropped and then write something to a file when this event happens.
I thought all I had to do was implement this interface with the two methods valueBound() and valueUnbound() and sessions being created and dropped would automatically trigger these two methods to execute.
Any help would be appreciated.
Here is my code:
public class Login extends User implements HttpSessionBindingListener {
private BufferedWriter log;

public Login() throws Exception {
log = new BufferedWriter( new FileWriter("c:\\dvd_log.txt", true));

private void log(String data) throws IOException{
log.newLine(); // create new line to write to next time
log.write(data); // write input to file
log.flush();//clear buffer

public void valueBound(HttpSessionBindingEvent event){
log("creating session...");
}catch(IOException e){

public void valueUnbound(HttpSessionBindingEvent event) {
log("destroying session...");
}catch(IOException e){
[This message has been edited by karim qazi (edited March 27, 2001).]
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
ACcording to the JavaDoc,
valueBound(HttpSessionBindingEvent event)
"Notifies the object that it is being bound to a session and identifies the session."
Apparently the only purpose is to notify an object when it is being bound (added?) to an HttpSession.
The opposite for
valueUnbound(HttpSessionBindingEvent event)

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Sandeep Jain
Ranch Hand

Joined: Oct 25, 2000
Posts: 124
Hi ,
HttpSessionBindingListener is used for the purpose of binding an object to the session .It basically notifies "the object that it is being bound to a session and identifies the session.
say for example .
Say u r implementing the connection pool urself and u do not depend on the webserver( not the application server) in this case when the session is invalidated then the code has to be written in such a way as to the connection should return back to the pool .
here u will write the code in valueUnbound and valueBound functions .

Sandeep Jain

Try and Try Till u succeed<br /> <br />Sandeep Jain
karim qazi

Joined: Mar 22, 2001
Posts: 20
why doesent my code work when i create a new session?
it should write to a log file i set up when creating or destroying a session.
I agree. Here's the link:
subject: using HttpSessionBindingListener interface
jQuery in Action, 3rd edition