过滤器&监听器

过滤器

1、什么是过滤器?

  • Filter 过滤器它是 JavaWeb 的三大组件之一:三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器
  • Filter 过滤器它是 JavaEE 的规范。也就是接口
  • Filter 过滤器它的作用是:拦截请求,过滤响应

过滤器实际上就是对web资源进行拦截,做一些处理后再交给下一个过滤器或servlet处理
通常都是用来拦截request进行处理的,也可以对返回的response进行拦截处理

大概流程图如下
请添加图片描述
应用场景

  1. List item
  2. 自动登录
  3. 统一设置编码格式
  4. 访问权限控制
  5. 敏感字符过滤等

2、filter生命周期及其与生命周期相关的方法

Filter接口有三个方法,并且这个三个都是与Filter的生命相关的方法

  1. init(Filterconfig):代表filter对象初始化方法 filter对象创建时执行

  2. doFilter(ServletRequest,ServletResponse,FilterChain):代表filter执行过滤的核心方法,如果某资源在已经被配置到这个filter进行过滤的话,那么每次访问这个资源都会执行doFilter方法(就是我们具体要对什么页面数据过滤的代码写在这里面)

  3. destory():代表是filter销毁方法 当filter对象销毁时执行该方法

  • Filter对象的生命周期:
  • Filter何时创建:服务器启动时就创建该filter对象
  • Filter何时销毁:服务器关闭时filter销毁

配置过滤器/代码演示

1配置:

@Component

@WebFilter("/*") //过滤所有的页面
public class MyFilter implements Filter {
 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
		// 这里是过滤器初始化
        System.out.println("Filter 前置");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		// 这里写我们要过滤的代码
        System.out.println("Filter 处理中");
        // 继续放行
        filterChain.doFilter(servletRequest, servletResponse); 
    }

    @Override
    public void destroy() {
   		 // 这里是过滤器被销毁
        System.out.println("Filter 后置");
    }
}

2配置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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
	
 <!-- 配置过滤器
	<filter>
		<filter-name>myfilter</filter-name>
		<filter-class>test.MyFilter</filter-class>
		配置过滤器的初始化参数
		<init-param>
		参数名
			<param-name>name</param-name>
		参数值
			<param-value>李四</param-value>
		</init-param>
		<init-param>
		参数名
			<param-name>sex</param-name>
		参数值
			<param-value></param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>myfilter</filter-name>
		设置过滤器要过滤的请求
		过滤访问index1.jsp
		<url-pattern>/index1.jsp</url-pattern>
		<url-pattern>/index2.jsp</url-pattern>
		过滤指定后缀名的请求(整个项目)
		<url-pattern>*.jsp</url-pattern>
		<url-pattern>*.do</url-pattern>
		过滤指定目录里的请求
		<url-pattern>/home/index.jsp</url-pattern>
		过滤整个项目所有请求(包括了所有的后缀)
		<url-pattern>/*</url-pattern>
	</filter-mapping> --> 
</web-app>

监听器

1、什么是监听器

是指专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生变化时,立即采取相应的行动。

分开来说:

  • 是一个实现了特定接口的Java类;
  • 用来监听另一个 Java类的方法调用或者属性的改变;
  • 当被监听对象发生了上述事件后,监听器某个方法将会立即被执行

2、监听器的分类

  1. 按监听的对象划分
  2. 用于监听应用程序环境对象(ServletContext)的事件监听器
  3. 用于监听用户会话对象(HttpSession)的事件监听器
  4. 用于监听请求消息对象(ServletRequest)的事件监听器

3、配置监听/代码演示

配置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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

	<!-- 配置监听器 -->
	<!-- <listener>
		<listener-class>com.zking.listener.AppLication</listener-class>
	</listener> -->
</web-app>
@WebListener
public class AppLication implements ServletContextListener,HttpSessionListener{
	// ServletContextListener 监听整个应用程序,只要你的应用程序启动也就是服务器启动,
	// 只要服务器启动,那么contextInitialized()这个方法就会被执行,不需要访问任何东西
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("应用程序被销毁!");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("应用程序被初始化!");
	}
	
	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		System.out.println("会话被创建!");

	}
	
	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
//		System.out.println("会话销毁!");	
		ServletContext application = arg0.getSession().getServletContext();
		Integer count = (Integer)application.getAttribute("count");
		
		count-=2; // 统计当前这个人的访问次数
		
		// 最后在把访问次数添加到application作用域里面
		application.setAttribute("count", count);
	}

}

代码演示对用户人数的累加如下:

@WebListener
public class AppLication implements ServletContextListener,HttpSessionListener{
	// ServletContextListener 监听整个应用程序,只要你的应用程序启动也就是服务器启动,
	// 只要服务器启动,那么contextInitialized()这个方法就会被执行,不需要访问任何东西
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("应用程序被销毁!");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("应用程序被初始化!");
	}
	
	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
//		System.out.println("会话被创建!");
		ServletContext application = arg0.getSession().getServletContext();
		Integer count = (Integer)application.getAttribute("count");
		if (null == count) { //在第一个人访问之前没有人在线
			count = 1; // 添加第一个人的访问
		}else { // 在你访问之前已经有人在线
			count++; // 统计当前这个人的访问次数
		}
		// 最后在把访问次数添加到application作用域里面
		application.setAttribute("count", count);
	}
	
	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
//		System.out.println("会话销毁!");	
		ServletContext application = arg0.getSession().getServletContext();
		Integer count = (Integer)application.getAttribute("count");
		
		count-=2; // 统计当前这个人的访问次数
		
		// 最后在把访问次数添加到application作用域里面
		application.setAttribute("count", count);
	}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值