Sevlet2.4规范 可以支持 <jsp:include page="URL"> 中匹配的URL,通过Fitler 配置如下
<filter>
<filter-name>Cache</filter-name>
<filter-class>prx.cache.filter.CacheFilter</filter-class>
<init-param>
<!-- 过期时间设置,默认为60秒 -->
<param-name>refreshPeriod</param-name>
<param-value>120</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Cache</filter-name>
<url-pattern>/index.jsp</url-pattern>
<dispatcher>request</dispatcher>
<!-- 使得页面中通过 include 方式嵌入的匹配页面也可以通过该Filter -->
<!-- 但是在Fitler中却取不到 include 指定的 url 值,只能取到原始含有该include的URL -->
<dispatcher>include</dispatcher>
</filter-mapping>
main.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
main context
<jsp:include page="./index.jsp?type=test"></jsp:include>
</body>
</html>
在Filter中可以可以取到 index.jsp?type=test 传入的参数type的值"test",却得不到"/index.jsp"这个 URL
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
//取得的值为:/main.jsp,而不是/index.jsp;怎么取到 /index.jsp呢?
String url = httpRequest.getRequestURI();
//可以取得传参:test
String type = httpRequest.getParameter("type");
chain.doFilter(request, response);
}