I want to use cache to load JSP page. I have created a Dynamic Web Project using Java JSP Servlet.
In this Project I am getting the data (in JSON) from Rest API call and rendered this data into JSP page inside table, dynamically.
I am also getting the Key Name 'lastUpdate' with data time in API data.
For the first time I have to rendered data inside table then second time after calling the Rest API, I have to check if my 'lastUpdate' (which is available in cache) time is same as 'lastUpdate' which is coming from Rest API call then I have to show the same page using cache.
I am not sure it possible or not, I am new in cache management.
So please share your feedback and some examples or tutorial which I can used for reference.
You could make use of the dates by a header line like:
'If-Modified-Since': 'Fri, 06 Jun 2018 01:16:45 GMT'
This tells the browser to use browser cache for the data, if it is not modified since that time.
Server uses field to communicate when data is created:
'Last-Modified': 'Fri, 06 Jun 2018 10:15:25 GMT'
When you ask If-Modified-Since
you either get 304 (Not Modified) or 200 OK, with new Last-Modified
value.
This is what I understand from your question:
Assumption: The page is common for all customers, and there are no search queries etc. So, the only question is whether to render the page again or not. i.e., we are not going to hold several JSPs in cache. Only "return same jsp" or "reender it again"
Here are my thoughts:
If you are still interested to cache the JSP, this might work:
If data has changed / first time: Make a wrapper of HttpServletResponse which will give a replacement stream instead of real servlet output stream. (It should keep a copy and forward to real servlet output stream also. Use the response wrapper to call forward:
request.getRequestDispatcher(xyz.jsp").forward(req,resWrapper);
If data has not changed, take the backup from step1 and write to output:
PrintWriter out = response.getWriter(); out.write( backupStr );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With