织梦CMS - 轻松建站从此开始!

abg欧博官网|登陆|游戏|

How to set Cache Control to no

时间:2025-09-09 21:35来源: 作者:admin 点击: 3 次
In order to set 'Cache-Control: no-cache' you will have to create a Custom filter, the ExpiresFilter is used to add 'Expires' and 'Cache-Control: max-

In order to set 'Cache-Control: no-cache' you will have to create a Custom filter, the ExpiresFilter is used to add 'Expires' and 'Cache-Control: max-age=' headers to HTTP response according to its 'Content-Type' and will not help in setting 'no-cache'

Follow the below steps

A custom filter should be written in order to set the headers, Package the filter inside a .jar

Put the .jar into $CATALINA_HOME/lib

Verify there are no 'ClassNotFoundException' once you add the jar in $CATALINA_HOME/lib

package nocache; import javax.servlet.Filter; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; public class CacheFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader("Cache-Control", "no-cache"); filterChain.doFilter(servletRequest, response); } }

Add the below content to the $CATALINA_HOME/conf/web.xml file

Sample web.xml deployment descriptor snippet

<filter> <filter-name>CacheFilter</filter-name> <filter-class>nocache.CacheFilter</filter-class> </filter> <filter-mapping> <filter-name>CacheFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>

Access the application and checkout the response headers.

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-09-11 07:09 最后登录:2025-09-11 07:09
栏目列表
推荐内容