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

栏目: JSP · 发布时间: 8年前

内容简介:javaWEB总结(30):配置Filter的dispatcher节点

目录结构

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>

运行结果

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

点击链接后

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

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

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

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>

其他页面不变

运行结果

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

可以看到控制台为空,并没有通过拦截器的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,可看到如下运行效果

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

如上做的是转发方式的过滤器配置方式的一个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:如果目标资源是通过声明式异常处理机制调用时,那么该过滤器会被调用,除此之外该过滤器不会调用。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

随意搜寻

随意搜寻

Peter Morville / 沈浩翔 / 华中科技大学出版社 / 2013-10-1 / CNY 68.00

在这个信息爆炸的年代,我们如何找到出路?在纷繁交错的信息流中,我们如何筛选出想要的信息?既然Google已经魔法般地将正确答案呈现在我们面前,为什么信息架构的方式依然重要? 《Web信息架构》的作者Peter Morville,用了10年时间回答以上问题。《随意搜寻》是 一趟奇妙的旅程,让未来触手可及:无论何时何地,我们都能找到任何人、任何东西。这本书即是路线图,也是信息时代的“玛雅预言”,......一起来看看 《随意搜寻》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具