+

Search Tips   |   Advanced Search

Use the DynamicContentProvider interface for dynamic cache


Use this task to configure the DynamicContentProvider interface for cached servlets and JSPs.

The dynamic cache service should be enabled and you should be using servlet caching. See Use the dynamic cache service and Set servlet caching for more information.

A cacheable servlet or JSP 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 JSPs 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.

 

  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 the 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());
            }
          }
    
    

 

Next steps

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

 

Related tasks


Set servlet caching
Use servlet cache instances