ListenerFilter

Listener(监听器)

是什么,作用?

监听某一个事件的发生,状态的改变
监听器的内部机制:其实就是接口回调


8个监听器,分为三种

  • 监听三个作用域创建和销毁

request —httpServletRequest
session —httpSession
application —ServletContext

  1. ServletContextListener
    ServletContext创建:启动服务器的时候
    ServletContext销毁:关闭服务器,从服务器移除项目
  2. ServletRequestListener
    request创建:访问服务器上的任意资源都会有请求出现
    request销毁:服务器已经对这次请求做出了响应
  3. HttpSessionListener
    session的创建:只要调用getSession
    session的销毁:超时(30分钟)或者非正常关闭

作用:
ServletContextListener:

  1. 完成自己想要的初始化工作
  2. 执行自定义任务调度。执行某一个任务

HttpSessionListener:统计在线人数

  • 监听三个作用域属性状态变更
    可以监听在作用域中值 添加 | 替换 | 移除的动作
    属性的添加,替换用的都是session.setAttribute(“name”,“zhangsan”);
    移除使用的是session.removeAttibute(“name”);

request — ServletRequestAttributeListener
session — HttpSessionAttributeListener
servletContext — ServletContextAttributeListener

  • 监听httpSession里面存值的状态变更
    这一类监听器不用注册
    a). HttpSessionBindingListener:对象与session绑定和接触绑定的动作
让JavaBean实现该接口
@override
public void valueBound(HttpSessionBindingEvent event) {
	System.out.println("对象被绑定进去了");
}

@override
public void valueUnbound(HttpSessionBindingEvent event) {
	System.out.println("对象被解除绑定了");
}

b). HttpSessionActivationListener:用于监听现在session的值是钝化(序列化)还是活化(反序列化)的动作

钝化(序列化):把内存中的数据存储到磁盘上
活化(反序列化):把硬盘上的数据读取到内存中

session的钝化活化的用意:session中的值可能会很多,并且我们有很长一段时间不适用这个内存中的值,那么可以考虑把session的值可以存储到硬盘上【钝化】,等下一次在使用的时候,它从硬盘上提取出来【活化】
如何让session在一定事件内钝化:做配置即可

  1. 在tomcat里面conf/context.xml里面配置:对所有的运行在这个服务器的项目生效
  2. 在conf/Catalina/localhost/context.xml配置:对localhost生效
  3. 在自己的web工程项目中的META-INF/context.xml:对当前的工程生效
maxIdleSwp:1分钟不用就钝化
directory:钝化后的那个文件存放的位置
<Context>
	<Manager className="rog.apache.catalina.session.PersistentManager" maxIdleSwap="1">
		<Store className = "org.apache.catalina.session.FileStore" direcotry="it123" />
</Manager>
</Context>

Filter

Filter介绍

过滤器:其实就是对客户端发出来的请求进行过滤。浏览器发出,然后服务器派servlet处理。在中间就可以过滤,其实过滤器起到的是拦截的作用
作用:

  1. 对一些敏感词汇进行过滤
  2. 设置统一编码
  3. 自动登陆

如何使用Filter

  1. 定义一个类,实现Filter
public class FilterDemo implements Filter {

	public void destroy() {
	
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("doFilter");
	}
	
	public void init(FilterConfig fConfig) throws ServletException {

	}

}
  1. 注册过滤器:在web.xml里面注册,注册的方式和servlet基本一样
 <filter>
    <display-name>FilterDemo</display-name>
    <filter-name>FilterDemo</filter-name>
    <filter-class>com.java.filter.FilterDemo</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>FilterDemo</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Filter生命周期

创建:服务器启动的时候
销毁:服务器停止的时候

Filter执行顺序

  1. 客户端发出请求,先经过过滤器,如果过滤器放行,那么才能到servlet
  2. 如果有多个多个过滤器,那么他们会按照注册的映射顺序来排队。只要有一个过滤器不放行,那么后面排队的过滤器以及servlet都不会收到请求

Filter细节处理

  1. init方法的参数filterConfig,可以用于获取filter在注册的名字以及初始化参数。其实这里的设计的初衷与ServletConfig是一样的
  2. 如果想要放行,那么在doFilter方法里面操作,使用参数chain
//放行,让请求到达下一个目标
chain.doFilter(request,response)
  1. <url-pattern></url-pattern>写法格式与servlet一样
  • 全路径匹配,以/开始
	/LoginServlet
  • 以目录匹配,以/开始,以*结束
   /demo01/*
  • 以后缀名匹配,以*开始,以后缀名结束
	*.jsp
  1. 针对dispatcher设置
  • REQUEST:只要是请求过来,都拦截,默认就是REQUEST
  • FORWARD:只要是转发都拦截
  • ERROR:页面出错发生跳转
  • INCLUDE:包含页面的时候拦截

BeanUtils的使用

//注册自己的日期转换器
ConvertUtils.register(new MyDateConverter(),Date.class);

//转化数据
Map map = request.getParameterMap();
UserBean bean = new UserBean();

//转化map中的数据,放置到bean对象身上
BeanUtils.populate(bean,map);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值