java-web中的Filter&Listener
Filter过滤器
当访问服务器资源的时候,过滤器可以将i气你个球拦截下来,完成一些特殊的功能
过滤器的作用:
一般用于完成通用的操作,如验证登陆,统一的编码处理,敏感字符过滤。就是打游戏骂人,会出现****
快速入门
步骤:1定义一个类,实现接口Filter
2 复写方法
3 配置拦截资源,包括注解配置和,web.xml配置
1111

2222
package com.quan.web.filter; import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException; @WebFilter("/*")
public class FilterDemo implements Filter {
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("FilterDemo被执行了。。。。"); }
@Override
public void init(javax.servlet.FilterConfig config) throws ServletException { } }
333注解:
@WebFilter("/*") 拦截所有请求:
测试
自行加入sevlet容器管理 Tomcat ;
运行

拦截器进行放行操作
再上面的doFilter方法中加入
chain.doFilter(req,resp);
测试

filter的web.xml配置
和sevlet的配置差不多,只是这个是拦截的路径
<filter>
<filter-name>FilterDemo</filter-name>
<filter-class>com.quan.web.filter.FilterDemo</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDemo</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
过滤器的执行流程
我们可以新建一个filter
@WebFilter("/*")
public class FilterDemo2 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//对request对象请求消息增强
System.out.println("filterDemo2 doing.....");
chain.doFilter(req, resp);
//对response对象的响应消息增强
System.out.println("filterDemo2 ending....");
}
public void init(FilterConfig config) throws ServletException {
}
}
修改一下index.jsp
<html>
<head>
<title>$Title$</title>
</head>
<body>
index.jsp.....
<%
System.out.println("index.jsp....");
%>
</body>
</html>
备注:两个百分号之间,用于输出到控制台
运行结果;
filterDemo2 doing.....
index.jsp....
filterDemo2 ending....
由上面的测试可以知道
filter执行的过程是,到达filter,
11进行一些请求增强操作,
22然后通过放行到后端的servlet 或者资源,
33再回到filer中放行之后的操作
filter的生命周期
11再服务器启动后,创建Filter对象,然后调用init方法,用于加载资源
22每一次请求拦截资源时候,会执行
33服务器关闭后,Filter对象被销毁,,释放资源
过程理解
@WebFilter("/*")
public class FilterDemo3 implements Filter {
/**
* 服务器关闭后,Filter对象被销毁,,释放资源
* 如果服务器是正常关闭,则会执行destroy方法
* 只执行一次
*/
public void destroy() {
System.out.println("destroy.....");
}
/**
* 每一次请求拦截资源时候,会执行
* @param req
* @param resp
* @param chain
* @throws ServletException
* @throws IOException
*/
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("doFilter....");
chain.doFilter(req, resp);
}
/**
* 再服务器启动后,创建Filter对象,然后调用init方法,用于加载资源
* 只执行一次
* @param config
* @throws ServletException
*/
public void init(FilterConfig config) throws ServletException {
System.out.println("init....");
}
}
查看日志

过滤器配置----拦截路径配置
1具体路径配置
/index.jsp 只有访问index.jsp资源时,过滤器才会被执行
2目录拦截配置
/user/* 访问/user下的所有资源时,过滤器都会被执行
3后缀名拦截:
*.jsp 访问所有后缀名为jsp资源时,过滤器都会被执
4拦截所有资源:
/* 访问所有资源时,过滤器都会被执行
//@WebFilter("/index.jsp") 只有访问index.jsp资源时,过滤器才会被执行
//@WebFilter("/user/*") 访问/user下的所有资源时,过滤器都会被执行
@WebFilter("*.jsp") //访问所有后缀名为jsp资源时,过滤器都会被执行
public class FilterDemo4 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("filterDemo4");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
过滤器配置----拦截方式配置
资源被访问的方式

注解配置:
dispatcherTypes = DispatcherType.REQUEST
@WebServlet("/user/updateServlet")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("updateServlet");
//转发到index.jsp
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
//浏览器直接请求index.jsp资源时,该过滤器才会被执行
@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.REQUEST)
public class FilterDemo5 implements Filter {
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("filterDemo5....");
chain.doFilter(req, resp);
} public void init(FilterConfig config) throws ServletException { } }
直接访问时:
http://localhost:8080/quan/index.jsp
结果:

通过间接转发访问时:
http://localhost:8080/quan/user/updateServlet

dispatcherTypes = DispatcherType.FORWARD
只有转发访问index.jsp时,该过滤器才会被执行
@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.FORWARD)
dispatcherTypes可以配置多个值
@WebFilter(value = "/index.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})
web.xml配置
<filter>
<filter-name>FilterDemo</filter-name>
<filter-class>com.quan.web.filter.FilterDemo</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDemo</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<dispatcher> 标签也是有五个值得。
过滤器链(配置多个过滤器)
执行顺序

编写第一个过滤器:
package com.quan.web.filter; import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException; @WebFilter("/*")
public class FilterDemo6 implements Filter {
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("FilterDemo6执行!!!!!!!!!!!");
chain.doFilter(req, resp);
System.out.println("FilterDemo6回来执行!!!!!!!!!!!"); } public void init(FilterConfig config) throws ServletException { } }
编写第二个过滤器:
package com.quan.web.filter; import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException; @WebFilter("/*")
public class FilterDemo7 implements Filter {
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("FilterDemo7执行!!!!!!!!!!!");
chain.doFilter(req, resp);
System.out.println("FilterDemo7回来执行!!!!!!!!!!!"); } public void init(FilterConfig config) throws ServletException { } }
测试访问路径:
http://localhost:8080/quan/index.jsp
日志输出:
FilterDemo6执行!!!!!!!!!!!
FilterDemo7执行!!!!!!!!!!!
index.jsp....
FilterDemo7回来执行!!!!!!!!!!!
FilterDemo6回来执行!!!!!!!!!!!
过滤器先后顺序问题:

Filter的小案例------登陆验证

Listener

监听器的使用步骤

web.xml配置
<!-- 配置监听器-->
<listener>
<listener-class>com.quan.web.listener.ListenerDemo1</listener-class>
</listener> <!-- 资源文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>application.xml</param-value>
</context-param>
监听器内容:
public class ListenerDemo1 implements ServletContextListener {
// Public constructor is required by servlet spec
public ListenerDemo1() {
}
//
// -------------------------------------------------------
// 监听ServletContext对象创建的,ServletContext对象服务器启动后自动创建
// 服务器启动后自动调用
// -------------------------------------------------------
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContextEvent done.....");
//一般会进行资源的加载文件
//1获取ServletContext对象
ServletContext servletContext = sce.getServletContext();
//2加载资源文件
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
//3获取真实路径
InputStream fis = servletContext.getResourceAsStream(contextConfigLocation);
System.out.println(fis);
}
/**
* 再服务器关闭后,ServletContext对象被销毁,
* 当服务器正常关闭后,该方法被调用
* @param sce
*/
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContextEvent out....");
}
}

java-web中的Filter&Listener的更多相关文章
- Java Web学习总结(29)——Java Web中的Filter和Interceptor比较
1. 背景 在设计web应用的时候,用户登录/注册是必不可少的功能,对用户登录信息进行验证的方法也是多种多样,大致可以认为如下模式:前端验证+后台验证.根据笔者的经验,一般会在前端进行一些例如是否输入 ...
- Java web中常见编码乱码问题(一)
最近在看Java web中中文编码问题,特此记录下. 本文将会介绍常见编码方式和Java web中遇到中文乱码问题的常见解决方法: 一.常见编码方式: 1.ASCII 码 众所周知,这是最简单的编码. ...
- Java Web中解决乱码的方式
Java Web中解决乱码的方式 方式一:添加编码过滤器 package com.itmacy.dev.filter; import javax.servlet.*; import javax.ser ...
- 【java web】过滤器filter
一.过滤器简介 过滤器filter依赖于servlet容器 所谓过滤器顾名思义是用来过滤的,Java的过滤器能够为我们提供系统级别的过滤,也就是说,能过滤所有的web请求, 这一点,是拦截器无法做到的 ...
- Java Web 中 过滤器与拦截器的区别
过滤器,是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法u ...
- JAVA WEB 中的编码分析
JAVA WEB 中的编码分析 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src {background-co ...
- Java web中常见编码乱码问题(二)
根据上篇记录Java web中常见编码乱码问题(一), 接着记录乱码案例: 案例分析: 2.输出流写入内容或者输入流读取内容时乱码(内容中有中文) 原因分析: a. 如果是按字节写入或读取时乱码, ...
- 深入分析Java Web中的编码问题
编码问题一直困扰着我,每次遇到乱码或者编码问题,网上一查,问题解决了,但是实际的原理并没有搞懂,每次遇到,都是什么头疼. 决定彻彻底底的一次性解决编码问题. 1.为什么要编码 计算机的基本单元是字节, ...
- 解决java web中safari浏览器下载后文件中文乱码问题
解决java web中safari浏览器下载后文件中文乱码问题 String fileName = "测试文件.doc"; String userAgent = request.g ...
- Java Web 中使用ffmpeg实现视频转码、视频截图
Java Web 中使用ffmpeg实现视频转码.视频截图 转载自:[ http://www.cnblogs.com/dennisit/archive/2013/02/16/2913287.html ...
随机推荐
- 基于IPv6的RIPng路由协议测试——信而泰网络测试仪实操
关键词 IPv6; RIPng; 协议测试; 内部网关协议; 外部网关协议 前言:在国际性网络中,如因特网,拥有很多应用于整个网络的路由选择协议.形成网络的每一个自治系统(AS),都有属于自己的路由选 ...
- springMVC 调查问卷系统 record
Maven下的依赖包有两个 spring-web和springWebMVC springwebMVC包含spring-web依赖, 但是spring-web的等级大于Spring-webmvc 没有 ...
- ssh执行命令或者脚本里最好先加上 source /etc/profile
记: 写一次shell脚本,发现总提示 找不到JAVA_HOME .明明已经配了环境变量,并且java -version也已经能输出信息,但就是提示. 后来在 脚本中加入第一句代码 source /e ...
- 哈工大 计算机网络 实验一 HTTP 代理服务器的设计与实现
计算机网络实验代码与文件可见github:计算机网络实验整理 实验名称 HTTP 代理服务器的设计与实现 实验目的: 熟悉并掌握 Socket 网络编程的过程与技术:深入理解 HTTP 协议, 掌握 ...
- IntelliJ:自动生成JavaBean的读写方法
1.定义好一个class,其中写好private属性. 2.右键点击该class,在generate中选择自动根据模板生成的读写方法.
- [转载]MATLAB中神经网络的函数
1.设计函数 solvein 设计线性网络: solverb 设计径向基网络: solverbe 设计精确的径向 ...
- spring框架中Bean的生命周期
一.Bean 的完整生命周期 在传统的Java应用中,bean的生命周期很简单,使用Java关键字 new 进行Bean 的实例化,然后该Bean 就能够使用了.一旦bean不再被使用,则由Java自 ...
- 3、myql的逻辑架构和sql的执行流程
msyql逻辑架构 逻辑架构的解析 逻辑架构图如下(序号代表的是:服务器处理客户端请求的流程) 1.1connectors connectors是指使用不同语言的客户端与mysql server服务器 ...
- vue js计算精度问题处理,兼容XP系统
js计算精度问题(浮点数误差,大数计算出错) JavaScript 运算时经常遇到会 0.000000001 和 0.999999999 这样奇怪的结果. 网上教程一大篇,原理请百度,抄作业往下看!! ...
- Linux-timedatectl使用
Linux下使用timedatectl命令时间时区操作详解 timedatectl命令对于RHEL / CentOS 7和基于Fedora 21+的分布式系统来说,是一个新工具,它作为systemd系 ...