FileDocCategorySizeDatePackage
CacheSessionListener.javaAPI DocGlassfish v2 API4173Fri May 04 22:35:28 BST 2007com.sun.appserv.web.taglibs.cache

CacheSessionListener

public class CacheSessionListener extends Object implements HttpSessionListener
HttpSessionListener which creates a cache for JSP tag body invocations and adds it as a session attribute in response to sessionCreated events, and clears the cache in response to sessionDestroyed events.

Fields Summary
Constructors Summary
public CacheSessionListener()
No-arg constructor

Methods Summary
public voidsessionCreated(javax.servlet.http.HttpSessionEvent hse)
Receives notification that a session was created, and adds newly created cache for JSP tag body invocations as a session attribute.

param
hse the notification event

      
        HttpSession session = hse.getSession();  
        ServletContext context = session.getServletContext();

        // Check if a cache manager has already been created and set in the
        // context
        CacheManager cm = (CacheManager)
            context.getAttribute(CacheManager.CACHE_MANAGER_ATTR_NAME);

        // Create a new cache manager if one is not present and use it
        // to create a new cache
        if (cm == null) {
            cm = new CacheManager();
        }

        Cache cache = null;
        try {
            cache = cm.createCache();
        } catch (Exception ex) {}

        // Set the cache as a session attribute
        if (cache != null) {
            session.setAttribute(Constants.JSPTAG_CACHE_KEY, cache);
        }
    
public voidsessionDestroyed(javax.servlet.http.HttpSessionEvent hse)
Receives notification that a session is about to be invalidated, and clears the session's cache of JSP tag body invocations (if present).

param
hse the notification event


        // Clear the cache
        HttpSession session = hse.getSession();  
        Cache cache = (Cache)session.getAttribute(Constants.JSPTAG_CACHE_KEY);
        if (cache != null) {
            cache.clear();
        }