【Head First Servlets and JSP】笔记 28: 过滤器与包装器
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: 过滤器与包装器的更多相关文章
- 《Head First Servlets & JSP》-13-过滤器和包装器
过滤器是什么 与servlet非常类似,过滤器就是java组件,请求发送到servlet之前,可以用过滤器截获和处理清求,另外 servlet结束工作之后,在响应发回给客户之前,可以用过滤器处理响应. ...
- [Java] JSP笔记 - Filter 过滤器
一.什么是Web过滤器 Servlet API 很久以前就已成为企业应用开发的基石,而 Servlet 过滤器则是对 J2EE 家族的相对较新的补充. Servlet 过滤器是可插入的 Web 组件, ...
- 《第一行代码》学习笔记28-内容提供器Content Provider(1)
1.内容提供器:用于在不同的应用程序之间实现数据共享的功能,提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问 数据的安全性.使用内容提供器是Android实现跨程序共享数据 ...
- 【Head First Servlets and JSP】笔记23:Expression Language(EL) 完全攻略
基本上是<Head First Servlets and JSP>内容的整理.扩充.顺便推荐一个供参考的JSP教程:JSP Tutorial内容很全面,还有一些有趣的实例. 完整代码参考 ...
- 笔记28 mssql的update :from语法
原文:笔记28 mssql的update :from语法 笔记28 mssql的update :from语法 --mssql的update :from语法 --a表 b表 结构分别 id ,name ...
- Servlet和JSP中的过滤器都是Java类
JSP 过滤器 Servlet和JSP中的过滤器都是Java类,它们存在的目的如下: 在请求访问后端资源时拦截它 管理从服务器返回给客户端的响应 下面列出了多种常用的过滤器类型: 认证过滤器 数据压缩 ...
- Java精选笔记_Filter(过滤器)
Filter(过滤器) Filter入门 什么是Filter Filter被称作过滤器或者拦截器,其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理前 ...
- [javaweb]Java过滤器与包装设计模式的实用案例.
在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...
- Struts2中过滤器和拦截器的区别
拦截器和过滤器的区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而 ...
随机推荐
- [libwww-perl]——POST方法的使用
libwww-perl是我在学习varnish的时候遇到的一个工具. 具体libwww-perl是干什么的,可以参考官网https://github.com/libwww-perl/libwww-pe ...
- MD5加密算法全解析
转自:http://blog.csdn.net/nzfxx/article/details/51804193 大家好,我们现在来讲解关于加密方面的知识,说到加密我认为不得不提MD5,因为这是一种特殊的 ...
- VUE学习总结
VUE学习总结 文档:https://cn.vuejs.org/v2/guide/ Webstorm的一些常用快捷键:1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任 ...
- shell命令发送网站请求
GET请求:curl "http://192.168.87.195:8888/refresh" POST请求:curl -d "name=value" &quo ...
- 160503、onunload、onbeforeunload事件详解
最近项目中做到一个功能:在上传页面用户开始上传文件之后用户点击任意跳转都需要弹出提示层进行二次确定才允许他进行跳转,这样做的目的是为了防止用户的错误操作导致这珍贵的UGC 流失(通常用户在一次上传不成 ...
- 160415、sql语句sort排序,sort为空的在后面
按sort排序,sort为空的在后面 select * from 表名 order by (case when sort is null or sort='' then 1 else 0 end),s ...
- HDU_5527_Too Rich
Too Rich Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- Android中可自由移动悬浮窗口的实现
http://www.xsmile.net/?p=538 调用WindowManager,并设置WindowManager.LayoutParams的相关属性,通过WindowManager的ad ...
- Storm-源码分析- Component ,Executor ,Task之间关系
Component包含Executor(threads)的个数 在StormBase中的num-executors, 这对应于你写topology代码时, 为每个component指定的并发数(通过s ...
- Buy the souvenirs---hdu2126(01背包输出方案数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 有n个物品每个物品的价格是v[i],现在有m元钱问最多买多少种物品,并求出有多少种选择方法: 如 ...