Search code examples
javacachingjspjsp-tags

Jsp cache problem


I use javascript and css to build a multi-level drop down menu with following markups:

<ul>
   <li>menu item 1</li>
       <ul>
         <li><a href="#">sub menu menu item 1</a></li>
         ................. 

This markup is generated by a custom JSP tag <mui:menu .../> which loads menu data from a database.

I hope my jsp page can behave like this:

  • if menu data has not changed since last time I visited the page, just use browser's cache
  • otherwise load from database...

How can I do it? I don't know much detail about cache mechanism.


Solution

  • Having a client cache means that the browser won't go to the server for content if what is stored in the cache is still fresh (hasn't expired yet). If you want to play with browser's cache for your menu then you have to start learning about it.

    But IMHO I think you are not approaching this the right way. Your menu is generated by a JSP custom tag. JSP runs on the server, so every time you request for the resource containing the <mui:menu .../> tag, the tag pulls data from your database.

    I guess you could have this tag in a separate resource that you can query with a non changing GET (so that browser can cache result) from the client, and I guess you could respond with a HTTP 304 Not Modified to signal that the cached value is still valid, but the fact is, in your particular case, it is better to handle cache on the server side not the client side.

    Let the JSP tag handle the cache and only go to the database if something changed.

    Just my two cents!