在servlet-2.3中,Filter会过滤一切请求
转自:http://www.iteye.com/problems/61651
在servlet-2.3中,Filter会过滤一切请求,包括服务器内部使用forward转发请求和<%@ include file="/index.jsp"%>的情况。
到了servlet-2.4中Filter默认下只拦截外部提交的请求,forward和include这些内部转发都不会被过滤,
在要weblogic8.1,servlet2.3下让filter不要拦截内部转发的请求,可以让Filter扩展OncePerRequestFilter. 实现只处理外部请求
以下代码在weblogic8.1测试通过
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<description>
Servlet and JSP Examples.
</description>
<display-name>Servlet and JSP Examples</display-name>
<filter>
<filter-name>first</filter-name>
<filter-class>filters.TestFilter</filter-class>
</filter>
<filter>
<filter-name>first1</filter-name>
<filter-class>filters.TestFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>first</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>first1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
package filters;
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;
/**
* 直接实现 Filter
* 默认过滤 forward和include 内部请求
* @author mojiezhong05180 2012-06-08
*
*/
public class TestFilter implements Filter {
private static int count = 0;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println((count++));
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
package filters;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;
/**
* 扩展OncePerRequestFilter
* 只拦截外部提交的请求,forward和include这些内部转发都不会被过滤
* @author mojiezhong05180 2012-06-08
*
*/
public class TestFilter1 extends OncePerRequestFilter {
private static int count = 0;
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
System.out.println("<TestFilter1>" + (count++));
filterChain.doFilter(request, response);
}
}
2012年6月08日 10:40