Servlet监听器和过滤器的配置及使用

版权声明:欢迎转载,转载请标明来源 https://blog.csdn.net/weixin_41973131/article/details/85224094

一.监听器

所有监听器都继承了ExentListener类
除了HttpSessionBindingListener外的监听器可以使用@WebListener标注或在web.xml中配置:

<listener>
    <listener-class>com.huachen.chatroom.listener</listener-class>
</listener>

1.ServletContext事件、监听器

Servlet中提供了两种监听器接口以实现ServletContext事件的监听:

设置初始参数:

<context-param>
    <param-name>AVATER</param-name>
    <param-value>/avater</param-value>
</context-param>

(1)ServletContextListenter

初始化:contextInitialized(ServletContextEvent servletContextEvent)
销毁:contextDestroyed(ServletContextEvent servletContextEvent)
触发条件:
1. 当服务器启动时触发初始化
2. 当服务器关闭时触发销毁
3. 使用getServletContext()得到ServletContext上下文信息

(2)ServletContextAttributeListener

属性添加:public void attributeAdd(ServletContextAttributeEvent scab);
属性移除:public void attributeRemoved(ServletContextAttributeEvent scab);
属性替换:public void attributeReplaced(ServletContextAttributeEvent scab);

注意事项:
1. 使用servletContext.setAttribute("userName",userName);触发添加
2. 已存在某属性时再次添加触发替换
3. 使用servletContext.removeAttribute("userName");触发移除
4. 使用getName()getValue()得到被操作对象的键值对

2.HttpSession事件、监听器

(1)HttpSessionListener生命周期

初始化:sessionCreated(HttpSessionEvent se);
销毁:sessionDestroy(HttpSessionEvent se);
使用getSession()取得HttpSession对象
触发条件:
1. 当使用session.setAttribute("userName",userName)时触发初始化
2. 当使用session.invalidate("userName")是触发销毁
3. 如果用户长时间没有访问服务器,超过了会话最大超时时间 ,服务器就会自动销毁超时的session。

发现问题:
1. 当Tomcat服务器启动时,监听器监听到了Session的创建
   当Tomcat请求JSP文件,由于JSP默认的隐式对象中是包含session的,其在生成Servlet文件时,内部相当于包含了HttpServletRequest.getSession(true)因此,请求时会直接创建session对象。
2. 取Attribute的值时得到的应该是一个对象,可以拿到Bean的值,拿到集合的值,但拿不到String的值。String是一个基本类型而不是引用类型,故得到空。
3. 当客户端连接到服务器时会监听到Session的创建,如果在Servlet中销毁Session后进行重定向或转发,Session会再次建立。

(2)HttpSessionAttributeListener属性改变

添加:attributeAdd(HttpSessionBindingEvent se);
移除:attributeRemoved(HttpSessionBindingEvent se);
替换:attributeReplaced(HttpSessionBindingEvent se);
getName()方法可以得到属性设置或移除时指定的名称,而getValue()可以取得属性设置或移除时的对象。

(3)HttpSessionBindingListener对象绑定

绑定:valueBound(HttpSessionBindingEvent event);
解绑: valueUnBound(HttpSessionBindingEvent event);
通过getSession()取得HttpSession对象。

注意事项:
1. 在Session.setAttribute("user",user),将User类放入Session时触发绑定
2. 在Session.removeAttribute("user",user),将User类移出Session触发解绑
3. 有方法getName()/getValue()/getSession()

(4)HttpSessionActivationListener对象迁移

session从内存到硬盘:SessionWillPassivate()
session从硬盘到内存:sessionDidActivate()
用于监控实现类本身,当实现类对象被添加到session中(session.setAttribute())后,session对象序列化(钝化)前和反序列化(活化)后都会被执行,相应的方法

3.HttpServletRequest

(1)ServletRequestListener

初始化:requestInitialized(ServletRequestEvent sre);
销毁:requestDestroyed(ServletRequestEvent sre)
可以取得ServletRequest,对request进行操作
可以去到ServletContext
注意事项:
1. 当request被初始化,即Servlet开始运行
2. Servlet结束,request被销毁。

(2)ServletRequestAttributeListener

添加:attributeAdd(HttpRequestAttributeEvent se);
移除:attributeRemoved(HttpRequestAttributeEvent se);
替换:attributeReplaced(HttpRequestAttributeEvent se);
getName()方法可以得到属性设置或移除时指定的名称,而getValue()可以取得属性设置或移除时的对象。

二.过滤器

过滤器基础配置

ServletContext初始化-》过滤器初始化-》过滤器销毁-》ServletContext销毁

// 实现过滤器后需要实现以下三个方法
init():通过FilterConfig的getInitParameter()方法得到初始参数。
doFilter():前置与后置处理
destroy():Filter销毁触发

web配置方法:

<filter>
	// 过滤器名字
    <filter-name></filter-name>
    // 过滤器类路径
    <filter-class>com.abc.model.UserFilter</filter-class>
    // 初始化参数
    <init-param>
        <param-name></param-name>
        <param-value></param-value>
    </init-param>
</filter>
// 匹配过滤器过滤路径
<filter-mapping>
	// 过滤器名称,需要与匹配的过滤器名称一样
    <filter-name></filter-name>
    // 过滤路径
    <url-pattern>/*</url-pattern>
    // 拦截请求,分别是请求,转发,包含,错误,异步。
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWORD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
    <dispatcher>ASYNC</dispatcher>
</filter-mapping>

标注配置方法:

@WebFilter(
	// 过滤器名
    filterName="",
    // 过滤路径
    urlPatterns={"/*"},
    // 过滤的Servlet名
    servletNames={""},
    // 初始化参数
    initParams={
        @WebInitParam(name="PARAM1",value="VALUE1");
    }
)

封装器

字符替换过滤器

// 替换用户输入信息中的非法字符
使用StringEscapeUtils.escapeHtml4(String message);
使用Jar包:commons-text/commons-lang

编码设置过滤器

// 实现过滤器后在doFilter()内设置编码格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);

猜你喜欢

转载自blog.csdn.net/weixin_41973131/article/details/85224094
今日推荐