1、过滤器的执行顺序: <url-pattern> 为第一梯队, <servlet-name> 为第二梯队,梯队内的执行顺序和 DD 里的声明顺序相同。

When the container recives a request, it first finds all the filter mappings with a <url-pattern> that matches the request URI. This becomes the first set of filters in the filter chain.Next it finds all the filter mappings with a <servlet-name> that matches the request URI. This becomes the second set of filters in the filter chain.In both the sets the filters are executed in the order in which they are declared in the D.D.

2、可以对服务器中传递的请求进行过滤。

    <filter>
<filter-name>BeerRequest</filter-name>
<filter-class>sample.BeerRequestFilter</filter-class>
<init-param>
<param-name>LogFileName</param-name>
<param-value>Userlog.txt</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>BeerRequest</filter-name>
<url-pattern>*.do</url-pattern>
<servlet-name>jack</servlet-name>
<dispatcher>REQUEST</dispatcher> <!-- 当一个也没有设置时,它是默认值 -->
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>

</filter-mapping>

3、过滤器应用实例

package sample;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter; public class Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter printWriter = resp.getWriter();
printWriter.println("calls doGet(req, resp)");
printWriter.println("测试能否输出中文");
printWriter.close();
}
}

filter :

package sample;

import javax.servlet.*;
import java.io.IOException; public class Filter implements javax.servlet.Filter{ private int visited; @Override
public void init(FilterConfig filterConfig) throws ServletException { } @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletResponse.setContentType("text/html");
servletResponse.setCharacterEncoding("utf-8");
visited ++;
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("visited = " + visited);
} @Override
public void destroy() { }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>sample.Servlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/do</url-pattern>
</servlet-mapping> <filter>
<filter-name>filter</filter-name>
<filter-class>sample.Filter</filter-class>
</filter> <filter-mapping>
<filter-name>filter</filter-name>
<servlet-name>servlet</servlet-name>
</filter-mapping> </web-app>

4、包装器略。

【Head First Servlets and JSP】笔记 28: 过滤器与包装器的更多相关文章

  1. 《Head First Servlets & JSP》-13-过滤器和包装器

    过滤器是什么 与servlet非常类似,过滤器就是java组件,请求发送到servlet之前,可以用过滤器截获和处理清求,另外 servlet结束工作之后,在响应发回给客户之前,可以用过滤器处理响应. ...

  2. [Java] JSP笔记 - Filter 过滤器

    一.什么是Web过滤器 Servlet API 很久以前就已成为企业应用开发的基石,而 Servlet 过滤器则是对 J2EE 家族的相对较新的补充. Servlet 过滤器是可插入的 Web 组件, ...

  3. 《第一行代码》学习笔记28-内容提供器Content Provider(1)

    1.内容提供器:用于在不同的应用程序之间实现数据共享的功能,提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问 数据的安全性.使用内容提供器是Android实现跨程序共享数据 ...

  4. 【Head First Servlets and JSP】笔记23:Expression Language(EL) 完全攻略

    基本上是<Head First Servlets and JSP>内容的整理.扩充.顺便推荐一个供参考的JSP教程:JSP Tutorial内容很全面,还有一些有趣的实例. 完整代码参考 ...

  5. 笔记28 mssql的update :from语法

    原文:笔记28 mssql的update :from语法 笔记28 mssql的update :from语法 --mssql的update :from语法 --a表 b表 结构分别 id ,name ...

  6. Servlet和JSP中的过滤器都是Java类

    JSP 过滤器 Servlet和JSP中的过滤器都是Java类,它们存在的目的如下: 在请求访问后端资源时拦截它 管理从服务器返回给客户端的响应 下面列出了多种常用的过滤器类型: 认证过滤器 数据压缩 ...

  7. Java精选笔记_Filter(过滤器)

    Filter(过滤器) Filter入门 什么是Filter Filter被称作过滤器或者拦截器,其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理前 ...

  8. [javaweb]Java过滤器与包装设计模式的实用案例.

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  9. Struts2中过滤器和拦截器的区别

    拦截器和过滤器的区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而 ...

随机推荐

  1. Angular2+学习第1篇 简介

    历史: Angular是Google推出的Web前端开发框架,从12年发布起就受到了强烈的关注,他首次提出了双向绑定的概念,让人耳目一新. Angular 2特性 就在2016年9月中旬,时隔4年,G ...

  2. Spring的AOP-----HelloWord

    这里就一个计算器开发为例1搭建环境-搭配好Spring的AOP开发环境导入以下这些包:2建立好核心处理模块的类ArithmeticCalculator: package com.jeremy.spri ...

  3. 升级PHP版本导致zabbix无法访问解决办法

    故障现象:无法打开zabbix首页,提示缺少zabbix.conf配置文件 原因分析:升级yum安装php版本了,升级前卸载了原PHP5.4版本导致 解决办法: 重新安装zabbix yum inst ...

  4. Spark源码分析 -- PairRDD

    和一般RDD最大的不同就是有两个泛型参数, [K, V]表示pair的概念 关键的function是, combineByKey, 所有pair相关操作的抽象 combine是这样的操作, Turns ...

  5. Xshell 连接虚拟机特别慢 解决方案

    由于各种原因,xshell连接虚拟机的rhel或者CentOS都几乎是龟速...... 今天专门查了一下解决方案: 原来是ssh的服务端在连接时会自动检测dns环境是否一致导致的,修改为不检测即可,操 ...

  6. python在处理CSV文件时,字符串和列表写入的区别

    概述 Python在处理CSV文件时,如果writerow的对象是<type 'unicode'>字符串时,写入到CSV文件时将会出现一个字符占一个单元格的情况: 但是将字符串转换为列表类 ...

  7. 手动编译安装Libvirt之后利用systemctl管理libvirtd服务

    因为要给特殊的虚拟机关联文件指定selinux标签,而默认的Libvirt没有这个功能,所以需要修改LIbvirt源代码,重新编译安装Libvirt,而手动编译安装的LIbvirt,没有办法使用sys ...

  8. generateScriptFile.py脚本使用过程中遇到的问题及解决

    generateScriptFile.py脚本 #!/usr/bin/env python # -*- coding: utf-8 -*- """ use case: p ...

  9. Centos配置nginx反向代理8090端口到80端口

    下面,我就来说说怎么反向代理自己的项目到默认80端口. 1)安装nginx:yum install nginx -y 2)启动nginx:service nginx start或者systemctl ...

  10. flatpickr功能强大的日期时间选择器插件

    flatpickr日期时间选择器支持移动手机,提供多种内置的主题效果,并且提供对中文的支持.它的特点还有: 使用SVG作为界面的图标. 兼容jQuery. 支持对各种日期格式的解析. 轻量级,高性能, ...