【JavaEE】监听器、过滤器

1. 监听器

1.1 监听Request、Session、ServletContext的创建与销毁
  • 1.1.1 ServletRequestListener

    • 创建:客户端访问服务器端任何资源
    • 销毁:服务器对该次请求做出响应
  • 1.1.2 HttpSessionListener

    • 创建:调用session并且找不到相关session
    • 销毁:调用session.destroy(),session超时未被访问
  • 1.1.3 ServletContextListener

    • 创建:Web服务器启动
    • 销毁:Web服务器停止
  • 1.1.4 用法

实现监听器接口并重写方法

public class MyServletContextListener implements ServletContextListener {
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("ServletContext销毁了");
	}

	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("ServletContext创建了");
	}
}

在web.xml配置监听器路径

  <listener>
  	<listener-class>com.listener.MyRequestListener</listener-class>
  </listener>
  <listener>
  	<listener-class>com.listener.MySessionListener</listener-class>
  </listener>
    <listener>
  	<listener-class>com.listener.MyServletContextListener</listener-class>
  </listener>
1.2 监听作用域中的值的添加、移除、替换
  • 1.2.1 ServletRequestAttributeListener
  • 1.2.2 HttpSessionAttributeListener
  • 1.2.3 ServletContextAttributeListener
  • 用法同1.1.4
1.3 监听JavaBean被绑定进session的状态
  • 只需JavaBean实现HttpSessionBindingListener接口,不用配置web.xml
  • 当调用session.setAttribute()会触发valueBound(),removeAttribute()会触发valueUnbound()
public class Student implements HttpSessionBindingListener {
	private String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void valueBound(HttpSessionBindingEvent arg0) {
		System.out.println("Student被绑定进Session了");
	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		System.out.println("Student从Session中解除绑定了");
	}
}
1.4 监听Session数据的钝化与活化
  • 1.4.1 HttpSessionActivationListener

    • 监听session某些数据被钝化(序列化)或活化(反序列化)
  • 1.4.2 用法

    • 将被存储在session中的类实现 HttpSessionActivationListener接口和Serializable接口
//实现HttpSessionActivationListener接口和Serializable接口
public class User implements HttpSessionActivationListener,Serializable {
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void sessionDidActivate(HttpSessionEvent se) {
		System.out.println("session被活化了");
	}
	
	public void sessionWillPassivate(HttpSessionEvent se) {
		System.out.println("session被钝化了");
	}
}
  • 把数据存入到session中
<%
	User user=new User();
	user.setUsername("zhangsan");
	session.setAttribute("user", user);
%>
  • 不用配置web.xml
  • 当Web服务器关闭时session会被钝化,会触发监听器的sessionWillPassivate()方法
  • 当Web服务器启动时session会被活化,会触发监听器的sessionDidActivate()方法
1.4.3 配置定时钝化session
  • 在META-INF下新建context.xml
<Context>
	<Manager
		className="org.apache.catalina.session.PersistentManager"
		maxIdleSwap="1">
		<Store className="org.apache.catalina.session.FileStore"
			directory="session" />
	</Manager>
</Context>

2. 过滤器

过滤器:对请求进行拦截,做一些过滤操作

2.1 创建过滤器
public class MyFilter implements Filter {

	//在过滤器创建时(Web服务器启动)执行
	public void init(FilterConfig config) throws ServletException {
		//获取web.xml中的filter中配置的初始化变量
		String initValue=config.getInitParameter("initName");
		System.out.println(initValue);
		System.out.println("过滤器被创建了");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("before");
		//放行
		chain.doFilter(request, response);
		System.out.println("after");
	}

	//在过滤器被销毁时(Web服务器关闭)执行
	public void destroy() {
		System.out.println("过滤器被销毁了");
	}
}
2.2 web.xml配置
<filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>com.filter.MyFilter</filter-class>
    
    <!-- 初始化参数 -->
    <init-param>
    	<param-name>initName</param-name>
    	<param-value>initValue</param-value>
    </init-param>
  </filter>
  
  <filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <!-- 拦截的请求类型,可以添加多个dispatcher -->
    
    <!-- 默认值REQUEST拦截所有请求-->
    <dispatcher>REQUEST</dispatcher>
    <!-- 拦截request.getRequestDispatcher().forward()转发的请求 -->
    <dispatcher>FORWARD</dispatcher>
    <!-- 拦截request.getRequestDispatcher().include()转发的请求 -->
    <dispatcher>INCLUDE</dispatcher>
    <!-- 拦截<error-page>错误页的请求 -->
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>
  
  <!-- 错误页配置 -->
  <error-page>
  	<error-code>500</error-code>
  	<location>/errorPage.jsp</location>
  </error-page>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值