轻云流火 chitry

你永远不知道未来的你有多强

控制动态资源(Servlet JSP)不要缓存的过滤器



2014年9月8日 16:03:33


动态资源(Jsp、Servlet等资源):为保证动态信息的时效性,动态资源不应该进行缓存,而应该时刻进行更新!



//控制动态资源(Servlet JSP)不要缓存的过滤器        
package cn.itcast.filter.example;
 
import java.io.IOException;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
//控制动态资源(Servlet JSP)不要缓存的过滤器
 
/*
过滤器的配置:
 
<filter>
<filter-name>NoCacheFilter</filter-name>
<filter-class>cn.itcast.filter.example.NoCacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
*/
public class NoCacheFilter implements Filter {
 
public void destroy() {
 
}
 
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
chain.doFilter(request, response);
}
 
public void init(FilterConfig filterConfig) throws ServletException {
}
 
}




//数据回显 Servlet        
package cn.itcast.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletDemo2 extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//拿到请求参数
String name = request.getParameter("name");
//把拿到的请求参数打印到界面上
PrintWriter out = response.getWriter();
out.write(name);
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}
2014年9月8日 16:03:33


动态资源(Jsp、Servlet等资源):为保证动态信息的时效性,动态资源不应该进行缓存,而应该时刻进行更新!



//控制动态资源(Servlet JSP)不要缓存的过滤器        
package cn.itcast.filter.example;
 
import java.io.IOException;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
//控制动态资源(Servlet JSP)不要缓存的过滤器
 
/*
过滤器的配置:
 
<filter>
<filter-name>NoCacheFilter</filter-name>
<filter-class>cn.itcast.filter.example.NoCacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
*/
public class NoCacheFilter implements Filter {
 
public void destroy() {
 
}
 
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
chain.doFilter(request, response);
}
 
public void init(FilterConfig filterConfig) throws ServletException {
}
 
}




//数据回显 Servlet        
package cn.itcast.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletDemo2 extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//拿到请求参数
String name = request.getParameter("name");
//把拿到的请求参数打印到界面上
PrintWriter out = response.getWriter();
out.write(name);
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29025139/article/details/46795945
个人分类: Web启示录 [基础篇]
上一篇Filter 统一全站编码
下一篇控制html、css、js等静态资源的缓存时间的过滤器
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭