+

Search Tips   |   Advanced Search

 

Use the DynamicContentProvider interface for dynamic cache

 

Use this task to configure the DynamicContentProvider interface for cached servlets and JavaServer Pages files. The dynamic cache service should be enabled and you should be using servlet caching. See Enabling the dynamic cache service and Configure servlet caching for more information.

 

Overview

A cacheable servlet or JavaServer Pages file might contain a state in the response that does not belong to the fragment for that servlet or JSP. When the state changes, the cached servlet or JSP is not valid for caching. Use the com.ibm.websphere.servlet.cache.DynamicContentProvider interface to make the fragment cacheable.

Servlets or JSP files that implement the DynamicContentProvider interface can add user exits in fragments that are cacheable by calling the addDynamicContentProvider(DCP) method on the wrapper response object. When the dynamic cache renders the page, it identifies the user exit and calls the dynamic content provider to add the dynamic content to the rendered page.

 

Procedure

  1. Provide an implementation class of the com.ibm.websphere.servlet.cache.DynamicContentProvider interface. An example of an implementation follows:

    class DynamicContentProviderImpl implements com.ibm.websphere.servlet.cache.
       DynamicContentProvider {
            DynamicContentProviderImpl() {}
            
            public void provideDynamicContent(HttpServletRequest request, OutputStream streamWriter)
               throws IOException {  
              String dynamicContent = System.currentTimeMillis();
              streamWriter.write(dynamicContent.getBytes());
            }
            public void provideDynamicContent(HttpServletRequest request, Writer streamWriter) 
               throws IOException {  
              String dynamicContent = System.currentTimeMillis();      
              streamWriter.write(dynamicContent.toCharArray());
            }
          }
    
    

  2. Add user exits to your servlet or JSP file by calling the addDynamicContentProvider(DCP) method on the wrapper response object. An example follows:

    public class DCPServlet extends CacheTestCase {
            public void performTest(HttpServletRequest request, HttpServletResponse response) 
               throws IOException, ServletException {
              out.println(“Testing the DCP feature begin ”+System.currentTimeMillis());
              DynamicContentProvider dcp = new DynamicContentProviderImpl();
              ServletCacheResponse scr = (ServletCacheResponse)(response);
              scr.addDynamicContentProvider(dcp);
                out.println("Testing the DCP feature end”+System.currentTimeMillis());
            }
          }
    
    

 

What to do next

See Task overview: Using the dynamic cache service to improve performance for more information about the other tasks that you can perform with the dynamic cache.


 

Related tasks


Configure servlet caching
Use servlet cache instances