Content Caching - JSP

Consider the pages that you have seen that are doing select statements on your data source.They are pulling data out of the database, formatting it, and presenting it in a JSP. Every request for these JSPs will involve a hit on the database, even though the page created might be the same for multiple users. Also, after the data has been extracted once, it could be cached for use by other pages.The next section will explore the concept of content caching and how current JSP technology enables content caching to take place.

Caching JSP Output

One way of caching is to cache the output from the JSP servlets so that when the page is requested again, the content comes from a cache rather than being dynamically created over again.

As part of the Jakarta Taglibs project, there is a cache tag library being developed. In fact, only two tags are in the library: cache and invalidate. More information can be obtained from This caching mechanism can be very attractive because not only are you reducing the load on the database, you are also reducing the processing load as the servlets will not always need to generate the output. However, this mechanism is not always sufficient. If you have any personalization in your site, pages will frequently be different for different people within your site. For sites where the output is fairly static for different users, and the database content does not change much, this can be of significant benefit.

Caching Database Content

An alternative to caching the JSP output is to cache the content of the database.This is a popular strategy because it reduces the hit on the database without restricting the dynamic content in any way. One of the easiest ways to do this is with the use of JavaBeans. JavaBeans can be accessed relatively easily from pages using the JavaBean-specific standard actions:

<jsp:useBean><jsp:setProperty><jsp:getProperty>

If the data is cached within Beans, not only does it reduce the load on the database, but it is also a relatively straightforward process to access the data from your JavaServer Pages. To demonstrate this, the shopping-cart application from Topic,“Session Tracking Using JSP,” will be modified. If you recall, the application is a basic shopping site that has the capability to log in and purchase products. In Chapter 8, the products were set up in JavaBeans and the users were defined in web.xml.We will now modify the application as follows:

  • User information will be stored in a database
  • Product information will be stored in a database
  • Product information will be extracted and cached by the application as JavaBeans

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JSP Topics