Tutorials > Dynamic caching > Accept cache identifiers
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
- Add a component for a session parameter, called cookieComponent to the TopCategoriesDisplay page.
- Open the cachespec.xml file located at WC_EAR\Stores.war\WEB-INF.
- Add the following component to the <cache-id> for the TopCategoriesDisplay page:
<component type="cookie"> <required>true</required> </component>
- Test the cachespec.xml.
- Use the cache monitor, look at the cache policies to verify that cookieComponent is a required component of the cache-id.
- Use the cache monitor to clear the entries from the cache.
- Hit the TopCategoriesDisplay page.
- 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.
- Add the cookieComponent cookie. You will modify the jsp from Step 2 to do this.
- Open the file exercise4.jsp in: WC_EAR\Stores.war
- 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>
- Test the cachespec.xml again.
- Clear the cache.
- Hit http://localhost/webapp/wcs/stores/servlet/exercise4.jsp. The cookieComponent cookie now exists.
- Hit the TopCategoriesDisplay page.
- Look at the cached entries in the cache monitor. There is an entry in the cache for TopCategoriesDisplay .
- Reset the cachespec.xml.
- Remove the cookieComponent component from the cachespec, or set the <required> flag to false for this component.