Tutorials > Dynamic caching > Accept cache identifiers

< Previous | Next >


Accept cache identifiers using cookies

The use of cookies does limit this component to Web-specific scenarios. You cannot use this component with other protocols, such as webservices.


Procedure

  1. Add a component for a session parameter, called cookieComponent to the TopCategoriesDisplay page.


    1. Open the cachespec.xml file located at WC_EAR\Stores.war\WEB-INF.

    2. Add the following component to the <cache-id> for the TopCategoriesDisplay page:

      <component   type="cookie">
             
      <required>true</required>
             
      </component>
      

  2. Test the cachespec.xml.

    1. Use the cache monitor, look at the cache policies to verify that cookieComponent is a required component of the cache-id.

    2. Use the cache monitor to clear the entries from the cache.

    3. Hit the TopCategoriesDisplay page.

    4. Look at the cached entries in the cache monitor. Because there is no cookie named cookieComponent, there should not be an entry in the cache for TopCategoriesDisplay.

  3. Add the cookieComponent cookie. You will modify the jsp from Step 2 to do this.

    1. Open the file exercise4.jsp in: WC_EAR\Stores.war

    2. Add the following to the exercise4.jsp file:

      <%@ page language="java" %>
      
      <HTML>
      <%
              Cookie mycookie = new
      Cookie("cookieComponent","helloWorld");
              response.addCookie(mycookie);
      %>
             
      <BODY>
                     
      <h2>Setting the cookie</h2>
      
             
      </BODY>
      
      </HTML>
      

  4. Test the cachespec.xml again.

    1. Clear the cache.

    2. Hit http://localhost/webapp/wcs/stores/servlet/exercise4.jsp. The cookieComponent cookie now exists.

    3. Hit the TopCategoriesDisplay page.

    4. Look at the cached entries in the cache monitor. There is an entry in the cache for TopCategoriesDisplay .

  5. Reset the cachespec.xml.

    1. Remove the cookieComponent component from the cachespec, or set the <required> flag to false for this component.

< Previous | Next >


+

Search Tips   |   Advanced Search