java - 基于URL参数缓存JSP

标签 java performance jsp caching

我有一个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/

相关文章:

java - 如何将对象从获取 Controller 提交到后置 Controller ?

java - 处理 JSP 页面时发生异常...服务器遇到内部错误,无法满足此请求

java - JCR在运行时查找强制属性

java - 拒绝 j_security_check 上的 GET 方法

java - 找不到与 www.billiving.com 匹配的主题备用 DNS 名称。为什么会这样,如何解决?

java - 在 Java(1.5 或更高版本)中,从 Set 中获取(任何)元素的最佳执行方式是什么?

c++ - 声明空堆栈的最有效方法是什么?

java - jsoup 的类型不匹配 Eclipse

java - JxBrowser 还要多久才能发布最新版本的 Chromium(版本 77)?

java - 如果设备将参数作为 .asp 发送到 apache tomcat 服务器,我想在 jsp 页面上访问这些参数