在 JSP 中添加 Expires 或 Cache-Control 标头

发布于 2024-09-05 13:26:27 字数 124 浏览 4 评论 0原文

如何在 JSP 中添加 ExpiresCache-Control 标头?我想在包含页面中为我的静态组件(例如图像、CSS 和 JavaScript 文件)添加一个远期到期日期。

How do you add an Expires or a Cache-Control header in JSP? I want to add a far-future expiration date in an include page for my static components such as images, CSS and JavaScript files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

大海や 2024-09-12 13:26:27

要禁用 JSP 页面的浏览器缓存,请创建一个映射到 *.jspurl-patternFilter,并在doFilter() 方法:

HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
httpResponse.setDateHeader("Expires", 0); // Proxies.

这样您就不需要将其复制粘贴到所有 JSP 页面上并使用 scriptlet 来混乱它们。

要为 CSS 和 JS 等静态组件启用浏览器缓存,请将它们全部放在一个公共文件夹中,例如 /static/resources 并创建一个 Filter它映射到 /static/*/resources/*url-pattern 上,并且在 doFilter 中基本上执行以下操作() 方法:

httpResponse.setDateHeader("Expires", System.currentTimeMillis() + 604800000L); // 1 week in future.

另请参阅:

To disable browser cache for JSP pages, create a Filter which is mapped on an url-pattern of *.jsp and does basically the following in the doFilter() method:

HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
httpResponse.setDateHeader("Expires", 0); // Proxies.

This way you don't need to copypaste this over all JSP pages and clutter them with scriptlets.

To enable browser cache for static components like CSS and JS, put them all in a common folder like /static or /resources and create a Filter which is mapped on an url-pattern of /static/* or /resources/* and does basically the following in the doFilter() method:

httpResponse.setDateHeader("Expires", System.currentTimeMillis() + 604800000L); // 1 week in future.

See also:

帅的被狗咬 2024-09-12 13:26:27
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
%>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
%>
尐偏执 2024-09-12 13:26:27
<%
    response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    response.setHeader("Pragma", "no-cache"); //HTTP 1.0
    response.setDateHeader("Expires", 0); //prevents caching at the proxy server
%>
<%
    response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    response.setHeader("Pragma", "no-cache"); //HTTP 1.0
    response.setDateHeader("Expires", 0); //prevents caching at the proxy server
%>
独自唱情﹋歌 2024-09-12 13:26:27

像 Tomcat 这样的 Servlet 容器带有一组预定义的过滤器。例如,请参阅过期过滤器。使用现有的过滤器可能比创建自己的类似过滤器更容易。

Servlet containers like Tomcat come with a set of predefined filters. See for example Expires Filter. It may be easier to use existing one than to create your own similar filter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文