Session name null from binding listner event
sgjava Jan 15, 2003 10:54 AMThe following code displays a null session ID in JBoss 3.0.5, but works in 3.0.4. Did something change in Jetty?
package com.fcci.servlet.velocity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/**
* HttpSessionBindingListener is used to clean up resources from Servlet
* session.
*
* @author Steven P. Goldsmith
* @version 1.0
*/
public class ServletSessionBindingListener
implements HttpSessionBindingListener {
/** Save a ServletContext to be used for its log() method */
private ServletContext context;
/** Map to store persistant database connections, etc */
private Map profile;
/**
* Constructor saves Servlet context and creates profile HashMap. Use
* profile to store session specific objects.
*
* @param context Velocity context
*/
public ServletSessionBindingListener(ServletContext context) {
this.context = context;
profile = new HashMap();
}
/**
* Return profile HashMap created in constructor.
*
* @return Map
*/
public Map getProfile() {
return profile;
}
/**
* Echos session ID to the log to prove session is bound. Remove logging
* code for real app.
*
* @see javax.servlet.http.HttpSessionBindingListener#valueBound(
* HttpSessionBindingEvent)
*/
public void valueBound(HttpSessionBindingEvent event) {
context.log(
"BOUND as "
+ event.getName()
+ " to "
+ event.getSession().getId());
}
/**
* Echos session ID to the log to prove session is unbound. Remove logging
* code for real app. Connection and prepared statement are closed if
* present in profile.
*
* @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(
* HttpSessionBindingEvent)
*/
public void valueUnbound(HttpSessionBindingEvent event) {
context.log(
"UNBOUND as "
+ event.getName()
+ " from "
+ event.getSession().getId());
// Clean up connection, this assumes that if a connection exists an
// opened PreparedStatement also exists.
try {
Connection connection = (Connection) profile.get("connection");
// If there is a connection, close the statement and connection.
if (connection != null) {
((PreparedStatement) profile.get("preparedStatement")).close();
connection.close();
}
} catch (Exception e) {
context.log(e.getMessage());
}
}
}