我们来做个测试,写一个过滤器,指定过滤器的资源为b.jsp,然后我们在浏览器中直接访问b.jsp,你会发现过滤器执行了。
但是,当我们在a.jsp中request.getRequestDispathcer("/b.jsp").forward(request, response)时,就不会再执行过滤器了,也就是说,默认情况下,只能直接访问目标资源才会执行过滤器,而forward执行目标资源,不会执行过滤器。
1.第一种默认dispatcher标签配置为REQUEST
2.forward服务器端跳转默认是不经过拦截器的是需要配置,如下:
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
<filter-mapping>
<filter-name>helloFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
<!--拦截 -->
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
3.INCLUDE前端跳转
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%-- 包含--%>
<jsp:include page="/index.jsp"></jsp:include>
</body>
</html>
<filter-mapping>
<filter-name>helloFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
4.ERROR跳转
注意:1除0是错误的浏览器会500error,所以会跳转到配置的error的界面当中,这种方式跳转也是不经过filter的,所以需要配置ERROR.
<html>
<head>
<title>$Title$</title>
</head>
<body>
<!--1除0会报500-->
<%1/0%>
</body>
</html>
<filter-mapping>
<filter-name>helloFilter</filter-name>
<url-pattern>/error.jsp</url-pattern>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
附带: