文章目录
Listener(监听器)
是什么,作用?
监听某一个事件的发生,状态的改变
监听器的内部机制:其实就是接口回调
8个监听器,分为三种
- 监听三个作用域创建和销毁
request —httpServletRequest
session —httpSession
application —ServletContext
- ServletContextListener
ServletContext创建:启动服务器的时候
ServletContext销毁:关闭服务器,从服务器移除项目 - ServletRequestListener
request创建:访问服务器上的任意资源都会有请求出现
request销毁:服务器已经对这次请求做出了响应 - HttpSessionListener
session的创建:只要调用getSession
session的销毁:超时(30分钟)或者非正常关闭
作用:
ServletContextListener:
- 完成自己想要的初始化工作
- 执行自定义任务调度。执行某一个任务
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在一定事件内钝化:做配置即可
- 在tomcat里面conf/context.xml里面配置:对所有的运行在这个服务器的项目生效
- 在conf/Catalina/localhost/context.xml配置:对localhost生效
- 在自己的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处理。在中间就可以过滤,其实过滤器起到的是拦截的作用
作用:
- 对一些敏感词汇进行过滤
- 设置统一编码
- 自动登陆
- …
如何使用Filter
- 定义一个类,实现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 {
}
}
- 注册过滤器:在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执行顺序
- 客户端发出请求,先经过过滤器,如果过滤器放行,那么才能到servlet
- 如果有多个多个过滤器,那么他们会按照注册的映射顺序来排队。只要有一个过滤器不放行,那么后面排队的过滤器以及servlet都不会收到请求
Filter细节处理
- init方法的参数filterConfig,可以用于获取filter在注册的名字以及初始化参数。其实这里的设计的初衷与ServletConfig是一样的
- 如果想要放行,那么在doFilter方法里面操作,使用参数chain
//放行,让请求到达下一个目标
chain.doFilter(request,response)
- <url-pattern></url-pattern>写法格式与servlet一样
- 全路径匹配,以/开始
/LoginServlet
- 以目录匹配,以/开始,以*结束
/demo01/*
- 以后缀名匹配,以*开始,以后缀名结束
*.jsp
- 针对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);