内容简介: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:如果目标资源是通过声明式异常处理机制调用时,那么该过滤器会被调用,除此之外该过滤器不会调用。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Redis 哨兵节点之间相互自动发现机制(自动重写哨兵节点的配置文件)
- Redis 哨兵节点之间相互自动发现机制(自动重写哨兵节点的配置文件)
- 安装和配置Hadoop集群(3节点)
- Kafka读书笔记 -- 单节点安装与配置
- Substrate 一键发链极简教程(五)- 配置验证人节点
- Mybatis Mapper.xml 配置文件中 resultMap 节点的源码解析 原 荐
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Learning Vue.js 2
Olga Filipova / Packt Publishing / 2017-1-5 / USD 41.99
About This Book Learn how to propagate DOM changes across the website without writing extensive jQuery callbacks code.Learn how to achieve reactivity and easily compose views with Vue.js and unders......一起来看看 《Learning Vue.js 2》 这本书的介绍吧!
HTML 压缩/解压工具
在线压缩/解压 HTML 代码
RGB CMYK 转换工具
RGB CMYK 互转工具