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

CacheRequestListener

public class CacheRequestListener extends Object implements ServletRequestListener
ServletRequestListener which creates a cache for JSP tag body invocations and adds it as a request attribute in response to requestInitialized events, and clears the cache in response to requestDestroyed events.

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

Methods Summary
public voidrequestDestroyed(javax.servlet.ServletRequestEvent sre)
Receives notification that the request is about to go out of scope of the web application, and clears the request's cache of JSP tag body invocations (if present).

param
sre the notification event


        // Clear the cache
        ServletRequest req = sre.getServletRequest();
        Cache cache = (Cache) req.getAttribute(Constants.JSPTAG_CACHE_KEY);
        if (cache != null) {
            cache.clear();
        }
    
public voidrequestInitialized(javax.servlet.ServletRequestEvent sre)
Receives notification that the request is about to enter the scope of the web application, and adds newly created cache for JSP tag body invocations as a request attribute.

param
sre the notification event


        ServletContext context = sre.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 request attribute
        if (cache != null) {
            ServletRequest req = sre.getServletRequest();
            req.setAttribute(Constants.JSPTAG_CACHE_KEY, cache);
        }