javaWEB总结(30):配置Filter的dispatcher节点



目录结构



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_30</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
      <filter-name>IndexFilter</filter-name>
      <filter-class>com.dao.chu.IndexFilter</filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>IndexFilter</filter-name>
      <url-pattern>/index.jsp</url-pattern>
  </filter-mapping>
  
</web-app>

login.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>login.jsp</title>
</head>
<body>
	<a href="<%=request.getContextPath() %>/index.jsp">To Index.jsp</a>
</body>
</html>

IndexFilter.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;

public class IndexFilter implements Filter {

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		System.out.println("do Filter");
		
		chain.doFilter(request, response);
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

}

index.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>index.jsp</title>
</head>
<body>
	<h1>hello</h1>
</body>
</html>

运行结果



点击链接后






下面再login.jsp与index.jsp中加入dispatcher.jsp,并且通过转发的方式连到index.jsp页面。修改的页面如下:




login.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>login.jsp</title>
</head>
<body>
	<a href="<%=request.getContextPath() %>/dispatcher.jsp">To Index.jsp</a>
</body>
</html>

dispatcher.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>dispatcher.jsp</title>
</head>
<body>

	<jsp:forward page="/index.jsp"></jsp:forward>
</body>
</html>

其他页面不变


运行结果




可以看到控制台为空,并没有通过拦截器的doFilter方法,这是由于拦截器默认拦截的是请求的方式,而此处是转发的方式所以拦截不到,此时我们就要配置Filter的dispatcher节点。



我们修改web.xml文件,将dispachter节点配置为转发的方式:


<?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_30</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
      <filter-name>IndexFilter</filter-name>
      <filter-class>com.dao.chu.IndexFilter</filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>IndexFilter</filter-name>
      <url-pattern>/index.jsp</url-pattern>
      <dispatcher>FORWARD</dispatcher>
  </filter-mapping>
  
</web-app>

修改后重启tomcat,可看到如下运行效果





如上做的是转发方式的过滤器配置方式的一个DEMO,还有另外三种和此种配置方式类似,这里不再叙述。


下面是总结:


<dispather>元素:指定过滤器所拦截资源被Servlet容器调用的方式。可以使REQUEST,INCLUDE,FORWARD和ERROR之一,默认是REQUEST。

可以设置多个<dispatcher>子元素用来指定Filter对资源的多种调用方式进行拦截。


1.REQUEST:当用户直接访问页面时,Web容器将会调用过滤器。如果目标资源是通过RequestDispather的include()或forward()方法访问时,那么该过滤器会被调用,除此之外该过滤器不会调用。
2.INCLUEDE:如果目标资源时通过RequestDispather的incluede()方法访问时,那么该过滤器会被调用,除此之外该过滤器不会调用。

3.FORWARD:如果目标资源是通过RequestDispather的forward()方法访问时,那么该过滤器会被调用,除此之外该过滤器不会调用。

4.ERROR:如果目标资源是通过声明式异常处理机制调用时,那么该过滤器会被调用,除此之外该过滤器不会调用。

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值