javaWEB总结(35):其他的servlet监听器

本文介绍如何使用ServletContextAttributeListener、HttpSessionAttributeListener和ServletRequestAttributeListener监听器来监听ServletContext、HttpSession和HttpServletRequest三个域对象中的属性变更事件。同时,还介绍了如何通过实现HttpSessionBindingListener和HttpSessionActivationListener接口使JavaBean对象感知自身在Session中的绑定状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.域对象中属性的变更的事件监听器

域对象中属性的变更的事件监听器就是来监听ServletContext,HttpSession,HttpServletRequest这三个对象中的属性变更信息事件的监听器。

这三个监听器接口分别是:

ServletContextAttributeListener,HttpSessionAttributeListener和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型是不同的。



目录结构


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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>javaWeb_35</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <listener>
      <listener-class>com.dao.chu.HelloListener</listener-class>
  </listener>
  
  <listener>
      <listener-class>com.dao.chu.HelloServletAttributeListenner</listener-class>
  </listener>
</web-app>


index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index.jsp</title>
</head>
<body>
<%
	request.setAttribute("name", "zhangsan");
	request.setAttribute("name", "lisi");
	request.removeAttribute("name");
%>
</body>
</html>


HelloListener.java

package com.dao.chu;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class HelloListener implements ServletContextListener,
		ServletRequestListener, HttpSessionListener {

	
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("ServletContext创建");
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("ServletContext销毁");
	}

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		System.out.println("HttpSession创建");
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		
		System.out.println("HttpSession销毁");
	}

	@Override
	public void requestInitialized(ServletRequestEvent arg0) {
		System.out.println("ServletRequest创建");
	}
	
	@Override
	public void requestDestroyed(ServletRequestEvent arg0) {
		System.out.println("ServletRequest销毁");
	}

}


HelloServletAttributeListenner.java

package com.dao.chu;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class HelloServletAttributeListenner implements
		ServletContextAttributeListener, HttpSessionAttributeListener,
		ServletRequestAttributeListener {

	@Override
	public void attributeAdded(ServletRequestAttributeEvent arg0) {
		System.out.println("ServletRequest添加属性"+arg0.getName()+":"+arg0.getValue());
	}

	@Override
	public void attributeRemoved(ServletRequestAttributeEvent arg0) {
		System.out.println("ServletRequest移除属性"+arg0.getName()+":"+arg0.getValue());

	}

	@Override
	public void attributeReplaced(ServletRequestAttributeEvent arg0) {
		System.out.println("ServletRequest替代属性"+arg0.getName()+":"+arg0.getValue());

	}

	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		System.out.println("HttpSession添加属性"+arg0.getName()+":"+arg0.getValue());
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		System.out.println("HttpSession移除属性"+arg0.getName()+":"+arg0.getValue());
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		System.out.println("HttpSession替代属性"+arg0.getName()+":"+arg0.getValue());
	}

	@Override
	public void attributeAdded(ServletContextAttributeEvent arg0) {
		System.out.println("ServletContext添加属性"+arg0.getName()+":"+arg0.getValue());
	}

	@Override
	public void attributeRemoved(ServletContextAttributeEvent arg0) {
		System.out.println("ServletContext移除属性"+arg0.getName()+":"+arg0.getValue());
	}

	@Override
	public void attributeReplaced(ServletContextAttributeEvent arg0) {
		System.out.println("ServletContext替代属性"+arg0.getName()+":"+arg0.getValue());
	}

}


运行效果

ServletRequestAttributeListener为例,ServletContextAttributeListener和HttpSessionAttributeListener与此类似。



2.感知Session绑定的事件监听器

保存在Session域中的对象可以有多种状态:绑定到Session中;从Session域中解除绑定;随Session对象持久化到一个存储设备中;随Session对象从一个存储设备中恢复。

Servlet规范中定义了两个特殊的监听器接口来帮助javaBean对象了解自己在Session域中的这些状态:

HttpSessionBindingListener和HttpSessionActivationListener接口,实现这两个接口的类不需要web.xml文件中进行注册。


(1)HttpSessionBindingListener

实现了HttpSessionBindingListener接口的JavaBean对象可以感知自己被绑定到Session中和从Session中删除的事件。

当对象被绑定到HttpSession对象中时,web服务器调用该对象的 void valueBound(HttpSessionBindingEvent event)方法。

当对象从HttpSession对象中解除绑定时,web服务器调用该对象的void valueUnbound(HttpSessionBindingEvent event)。



项目结构



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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>javaWeb_35</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


index.jsp

<%@page import="com.dao.chu.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index.jsp</title>
</head>
<body>
	<%
	
		session.setAttribute("student", new Student());
	
		session.removeAttribute("student");
	%>
</body>
</html>


Student.java

package com.dao.chu;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class Student implements HttpSessionBindingListener {

	@Override
	public void valueBound(HttpSessionBindingEvent arg0) {
		System.out.println("绑定到session."+arg0.getName()+":"+arg0.getValue());
	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		System.out.println("从session中解除绑定."+arg0.getName()+":"+arg0.getValue());
	}

}



运行效果


(2)HttpSessionActivationListener接口

*活化:从硬盘中读取。*钝化:写到硬盘上。

实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化和钝化的事件。

当绑定到HttpSession对象中的对象将要随HttpSession对象钝化之前,web服务器调用该对象的void sessionWillPassivate(HttpSessionBindingEvent event)方法。

当绑定到HttpSession对象中的对象将要随HttpSession对象活化之后,web服务器调用该对象的void

sessionDidActive(HttpSessionBindingEvent event)方法。


项目结构


运行效果


第一次访问index.jsp



第二次访问index.jsp




随意改动Student.java中的代码,然后ctrl+s保存,相当于重启tomcat


从其服务器后,再次访问index.jsp。还能够读取Session中的对象,说明Session被持久化。


其实在tomcat/work/Catalina/localhost/项目名/SESSION.ser中存储



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值