前提
项目结构
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>javaWeb_31</display-name>
<welcome-file-list>
<welcome-file>a.jsp</welcome-file>
</welcome-file-list>
</web-app>
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>A.jsp</title>
</head>
<body>
<a href="b.jsp">TO B.jsp</a><br><br>
<img alt="" src="<%=request.getContextPath()%>/img/a.jpg">
</body>
</html>
b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>B.jsp</title>
</head>
<body>
<a href="a.jsp">TO A.jsp</a><br><br>
<img alt="" src="<%=request.getContextPath()%>/img/b.jpg">
</body>
</html>
运行结果
当我们修改a.jsp后
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>A.jsp</title>
</head>
<body>
<a href="b.jsp">TO B.jsp</a><br><br>
<img alt="" src="<%=request.getContextPath()%>/img/b.jpg">
</body>
</html>
图片依旧不变。
添加过滤器
项目结构
修改的页面web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>javaWeb_31</display-name>
<welcome-file-list>
<welcome-file>a.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Cache</filter-name>
<filter-class>com.dao.chu.Cache</filter-class>
</filter>
<filter-mapping>
<filter-name>Cache</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
添加的类
HttpFilter.java
package com.dao.chu;
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;
/**
*
* <p>
* Title: HttpFilter
* </p>
* <p>
* Description: http请求定制Filter
* </p>
*/
public abstract class HttpFilter implements Filter {
/**
* 用于保存init(FilterConfig filterConfig)的FilterConfig对象
*/
private FilterConfig filterConfig;
/**
* 直接返回init(FilterConfig filterConfig)的FilterConfig对象
*/
public FilterConfig getFilterConfig() {
return filterConfig;
}
/**
* 不建议子类直接覆盖,将可能会导致filterConfig成员变量初始化失败
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
init();
}
/**
* 供子类继承的初始化方法,可以通过getFilterConfig获取FilterConfig对象
*/
protected void init() {}
/**
* 原生的doFilter方法,在方法内部把ServletRequest和ServletResponse
* 转为了HttpServletRequest和HttpServletResponse并调用了 doFilter(HttpServletRequest
* httpRequest, HttpServletResponse httpResponse, FilterChain chain)方法
*
*
* 若编写Filter的过滤方法不建议直接继承该方法,而应该继承doFilter(ServletRequest request,
* ServletResponse response, FilterChain chain)
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
doFilter(httpRequest, httpResponse, chain);
}
/**
* 抽象方法,为http请求定制,必需实现的方法
*
*/
public abstract void doFilter(HttpServletRequest httpRequest,
HttpServletResponse httpResponse, FilterChain chain)
throws IOException, ServletException;
/**
* 空的destroy方法
*/
@Override
public void destroy() {}
}
Cache.java
package com.dao.chu;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Cache extends HttpFilter {
@Override
public void doFilter(HttpServletRequest httpRequest,
HttpServletResponse httpResponse, FilterChain chain)
throws IOException, ServletException {
System.out.println("cache doFilter");
httpResponse.setDateHeader("Expires", -1); // for IE
httpResponse.setHeader("Cache-Control", "no-cache"); // for 火狐 或 其他。
httpResponse.setHeader("Pragma", "no-cache"); // for 火狐 或 其他。
chain.doFilter(httpRequest, httpResponse);
}
}
添加完此过滤器后即可禁用游览器缓存的过滤器。