JAVA入门[15]-过滤器filter
一、过滤器
过滤器是可用于 Servlet 编程的 Java 类,可以实现以下目的:
- 在客户端的请求访问后端资源之前,拦截这些请求。
- 在服务器的响应发送回客户端之前,处理这些响应。
参考:http://www.journaldev.com/1933/java-servlet-filter-example-tutorial
二、如何实现和配置过滤器
1.定义过滤器
过滤器实现接口: javax.servlet.Filter
示例:定义过滤器实现计数器,counterFilter实现Filter接口
public class counterFilter implements Filter {
ServletContext context;
int count;
public void init(FilterConfig filterConfig) throws ServletException {
context=filterConfig.getServletContext();
String initCount=filterConfig.getInitParameter("count");
count= Integer.valueOf(initCount);
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
context.log("===counterFilter do Filter====");
count++;
context.setAttribute("count",count);
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {
}
}
jsp调用:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div>欢迎,您是第 <%=application.getAttribute("count")%> 位访客</div>
2.配置web.xml
过滤器是可插拔的,通过web.xml来声明,然后映射到您的应用程序的部署描述符中的 Servlet 名称或 URL 模式。
当 Web 容器启动 Web 应用程序时,它会为部署描述符中声明的每一个过滤器创建一个实例。
web.xml 中的 filter-mapping 元素的顺序决定了 Web 容器应用过滤器到 Servlet 的顺序。若要反转过滤器的顺序,只需要在 web.xml 文件中反转 filter-mapping 元素即可。
<filter>
<filter-name>counterFilter</filter-name>
<filter-class>filter.counterFilter</filter-class>
<init-param>
<param-name>count</param-name>
<param-value>100</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>counterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.如何给过滤器设置参数?
在web.xml中用init-param节点设置参数,然后在 init 方法使用 FilterConfig 对象获取参数。
context=filterConfig.getServletContext();
String initCount=filterConfig.getInitParameter("count");
三、使用注解@WebFilter定义过滤器
@WebFilter注解可以实现了javax.servlet.Filter接口的类定义为过滤器组件
@WebFilter(filterName = "filter1",initParams =@WebInitParam(name = "count",value = "100"),urlPatterns = "/*")
public class filter1 implements Filter {
ServletContext context;
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
context.log("filter1.doFilter()");
chain.doFilter(req, resp);
} public void init(FilterConfig config) throws ServletException {
context=config.getServletContext();
}
}
执行顺序:默认按照filter的名字排序,如果想调整顺序,还是要配置filter-mapping节点。
<filter-mapping>
<filter-name>filter2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
查看一下Tomcat Localhost Log:
02-Jun-2017 13:26:20.182 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
02-Jun-2017 13:26:21.302 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring FrameworkServlet 'springmvc'
02-Jun-2017 13:26:24.716 信息 [http-nio-8092-exec-1] org.apache.catalina.core.ApplicationContext.log ===counterFilter do Filter====
02-Jun-2017 13:26:24.716 信息 [http-nio-8092-exec-1] org.apache.catalina.core.ApplicationContext.log filter2.doFilter()
02-Jun-2017 13:26:24.716 信息 [http-nio-8092-exec-1] org.apache.catalina.core.ApplicationContext.log filter1.doFilter()
四、一个应用场景:过滤器防止中文编码
public class EncodeFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)servletRequest;
HttpServletResponse response=(HttpServletResponse)servletResponse;
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
filterChain.doFilter(request,response);;
}
public void destroy() {
}
}
JAVA入门[15]-过滤器filter的更多相关文章
- Java基础95 过滤器 Filter
1.filter 过滤器的概述 filter过滤器:是面向切面编程的一种实现策略,在不影响原来的程序流程的前提下,将一些业务逻辑切入流程中,在请求达到目标之前进行处理,一般用于编码过滤.权限过滤... ...
- java中的过滤器 --Filter
package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.Filter ...
- 【java web】过滤器filter
一.过滤器简介 过滤器filter依赖于servlet容器 所谓过滤器顾名思义是用来过滤的,Java的过滤器能够为我们提供系统级别的过滤,也就是说,能过滤所有的web请求, 这一点,是拦截器无法做到的 ...
- Java Web(四) 过滤器Filter
Filter概述 Filter意为滤镜或者过滤器,用于在Servlet之外对request或者response进行修改.Filter提出了过滤链的概念.一个FilterChain包括多个Filter. ...
- 使用java的自定义过滤器Filter 处理请求request 并响应response
package com.enation.eop; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...
- Java 中的过滤器Filter 和拦截器 Interceptor
1.先说拦截器 Interceptor 本项目以springboot为例: 新建 InterceptorConfig package com.opendev.mystudy.MyInterceptor ...
- JAVA入门--目录
在此记录自己的JAVA入门笔记,备忘 JAVA入门[1]--安装JDK JAVA入门[2]-安装Maven JAVA入门[3]—Spring依赖注入 JAVA入门[4]-IntelliJ IDEA配置 ...
- java之过滤器Filter
Java三大器之过滤器(Filter)的工作原理和代码演示 一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术之一,WEB开发人员通过Filter技术,对w ...
- java过滤器Filter笔记
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术之一,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp,Servlet, 静 ...
随机推荐
- queue的入门
#include "iostream"#include "queue" using namespace std; void main12(){ queue &l ...
- 【Arduino】使用LCD1602和DHT11制作温湿度显示器
材料: 1.DHT11 2.LCD1602 3.LCD1602 转接板 4.Arduino UNO 5.Arduino 传感器扩展版 那个Arduino UNO 我当初挑类个便宜的山寨货买,结果发来和 ...
- Python学习--字典
在Python中通过名字来引用值的数据结构称为映射(mapping).字典是Python中唯一内建(Python解释器本身支持,不需要import)的映射类型.字典中的值没有特殊的顺序,都存储在一个特 ...
- C#设计模式之十四模板方法模式(Template Method)【行为型】
一.引言 “结构型”的设计模式已经写完了,从今天我们开始讲“行为型”设计模式.现在我们开始讲[行为型]设计模式的第一个模式,该模式是[模板方法],英文名称是:Template Method Patte ...
- ARM开发板链接shell
1.用网线插入开发板(最好链接路由器) 2.启动开发板(可以用U盘启动) 执行 #run bootusb 3.联网 #ifconfig eth0 up #udhcpc或者#dhclient wan # ...
- MVC页面静态化
MVC 页面静态化 最近工作需要,实现页面静态化,以前在ASP时代,都是FSO自己手动生成的. 新时代,MVC了,当然也要新技术,网上一搜,找到一种解决方案,是基于MVC3的,实现原理是通过mvc ...
- vue打包之后生成一个配置文件修改接口
前言: 我们的vue代码打包上传到服务器之后, 要是数据接口 以后换了域名什么的,是不是需要重新去vue文件里修改接口. 能不能生成一个配置文件,里面可以配置域名或其它什么字段之类的,这样以后换了域名 ...
- python logging模块+ 个人总结
原文地址:http://www.cnblogs.com/sislcb/archive/2008/11/25/1340627.html 从Python2.3版本中开始引入的logging模块为应用提供了 ...
- #postman接口测试系列:基本操作总结
最近项目需要接口测试,所以选择了不少工具对比,最终决定使用postman进行接口测试,这个工具目前使用比较简单,但是有点还是比较多的,如下: 方便切换不同的环境进行接口测试工作,而不用修改变量或代码 ...
- 【smart-transform】取自 Atom 的 babeljs/coffeescript/typescript 智能转 es5 库
简介 有时间研究下开源库的源码,总是会有些收获的.注意到 Atom 插件编写时,可以直接使用 babel, coffeescript 或者 typescript.有些诧异,毕竟 Electron 中内 ...