Tutorials > Web services > Define an inbound Web service
Add caching support for a Web service response JSP page
This section is related to adding caching support for a Web service response JSP page.
Since the response of the Web service request is composed by a JSP page, you can use WebSphere Dynacache to provide full page or fragment caching of Web service responses. The portion of the JSP page that generates the XML file for product information can be placed in a separate fragment. Then pieces of the response can be derived from the cache instead of the page being continuously executed each time the service is requested.
By splitting up the JSP response into a main JSP page and a JSP page fragment, not only can you reuse existing JSP source for other Web service responses, but also you can specify a caching policy on those JSP pages. The JSP source in this section is a new version based on the JSP fragment approach. As shown, the main ShowProductInformation.jsp file iterates through the specified product SKU and calls the ProductInformation.jsp fragment to generate the product information XML file.
ShowProductInformation.jsp
<% response.setContentType("text/xml"); %> <% response.setCharacterEncoding("UTF-8"); %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <myco:ShowProductInformation xmlns:myco="http://www.mycompany.com/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mycompany.com/schema" > <% String[] productSKUs = request.getParameterValues("productSKU"); for (int i=0; i < productSKUs.length; i++) { %> <c:import url="ProductInformation.jsp"> <c:param name="productId" value="<%=productSKUs[i]%>"/> <c:param name="storeId" value="${param.storeId}"/> </c:import> <%-- Flush the buffer so this fragment JSP is not cached twice --%> <%out.flush();%> <% } %> </myco:ShowProductInformation>
ProductInformation.jsp
<%@ taglib uri="http://commerce.ibm.com/base" prefix="wcbase"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <wcbase:useBean id="product" classname="com.ibm.commerce.catalog.beans.ProductDataBean"> <c:set target="${product}" property="productID" value="${param.productId}" /> </wcbase:useBean> <Product> <ProductSKU><c:out value="${product.partNumber}" /></ProductSKU> <ProductName><c:out value="${product.description.name}" escapeXml="false" /></ProductName> <ShortDescription><c:out value="${product.description.shortDescription}" escapeXml="true" /></ShortDescription> <LongDescription><c:out value="${product.description.longDescription}" escapeXml="true" /></LongDescription> <Availability>In Stock</Availability> <SuggestedRetailPrice><c:out value="${product.listPrice}" escapeXml="true" /></SuggestedRetailPrice> </Product>
With the separation of the response JSP page, a caching policy can be defined in the WEB-INF/cachespec.xml of the Stores Web Module to cache the execution of the JSP based on the storeId and productId values. This will enhance performance when servicing Web service requests since a piece or the response can be cached. Here is the cachespec.xml entry that might define a caching policy for this JSP page.
<cache-entry> <class>servlet</class> <name>/webservices/MyCompany/ProductInformation.jsp</name> <property name="save-attributes">false</property> <cache-id> <component id="productId" type="parameter"> <required>true</required> </component> </cache-id> <cache-id> <component id="storeId" type="parameter"> <required>true</required> </component> </cache-id> </cache-entry>