我有一个jsp文件pageshow.jsp和参数id,
有没有办法根据url参数在服务器端缓存jsp文件
请求页面 pageshow.jsp?id=100 从缓存获取而不是从服务器构建
请求页面 pageshow.jsp?id=200 从缓存获取而不是从服务器构建
以上两个页面由于参数不同,应该有不同的缓存内容
这可以避免在服务器端重建jsp文件并减少服务器负载
最佳答案
我会考虑使用类似于 AlexR 所提议的 CachingHttpFilter
,但考虑使用 HTTP header 来控制缓存,而不是将数据存储在自己的卷中 -内容缓存。
This article很好地解释了如何在后续请求 URL 时比较 If-Modified-Since
HTTP header 。然后由您的过滤器发回 HTTP 304
响应。
Comparison of dates:
The client asks the server to a specific page: if the client has already read the page, it sends a request (to server) containing the last modification date of its cached page (eg “If-Modified-Since: 21/07/2002 13:47:24 GMT”);There also the header If-Unmodified-Since;
The server compares this date given by the client with the last modified date of requested page:
if the page on the server has the same date, then the server informs the client that it can use the version’s page in its cache (“HTTP/1.1 304 Not Modified”). The exchange between client and server stops there;
if the page on the server is newer, then the server informs the client of the change (“Last-modified: 21/06/2012 08:45:37 p.m. GMT”) and sent this page to client. The browser stores the date of last change of the page.
您还需要查看 ETag
HTTP Header .
不幸的是,缓存听起来很简单,但通常很难做到正确。调整数据库查询通常是提高应用程序性能的最佳起点。
关于java - 基于URL参数缓存JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12172756/