监听器只要分为监听web对象创建与销毁,监听属性变化,感知监听器。
1.监听web对象的创建与销毁
servletContextListener 监听ServletContext对象的创建和销毁
创建监听器类
package listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; public class MyServletContextListener implements ServletContextListener{ @Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContext对象创建了...."); } @Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContext对象销毁了");
} }
配置监听器(web.xml)
<listener>
<!--对应的监听器类全路径-->
<listener-class>listener.MyServletContextListener</listener-class>
</listener>
测试:
加载: 把项目加载到tomcat(服务器)中,启动服务器,监听器就会加载到内存中,
销毁: 把服务器关闭监听器就会执行销毁方法;
HttpSession监听器 监听HttpSession对象的创建和销毁
创建监听器类
package listener; import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener; public class MyHttpSessionListener implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("sessin创建了");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("session销毁了");
}
}
配置文件(web.xml)
<listener>
<!--对应的监听器类的全路径-->
<listener-class>listener.MyHttpSessionListener</listener-class>
</listener>
测试:
创建: 访问jsp页面 或者访问已经创建了session对象的servlet文件(java类)
访问: 页面 是否创建
HTML 否
JSP 是
原因: 访问jsp文件时服务器会对jsp文件进行编译成servlet文件,查看编译的servlet文件,可以看到servlet文件在静态代码块中对session对象进行赋值了。
Servlet 否
销毁: 让session 对象销毁就会执行监听器的销毁方法
让session销毁的方式三种,
a.强行关闭控制台
b.session对象调方法(invalidate())
c.session对象超时(配置session对象存在的时间 1.在tomcat中的配置文件进行配置2.在项目的web.xml文件中进行配置 )
在tomcat中进行配置(apache-tomcat-7.0.52\conf)默认是时间为30分钟
在项目的中进行配置(web.xml)
<session-config>
<!-- 配置session的过期时间 (分钟)-->
<session-timeout>5</session-timeout>
</session-config>
HttpServletRequest监听器 监听请求的对象的创建和销毁
创建监听类
package listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener; public class MyServletRequestListener implements ServletRequestListener{ @Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("request销毁了");
} @Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("request创建了");
} }
配置监听器(web.xml)
<listener>
<listener-class>listener.MyServletRequestListener</listener-class>
</listener>
测试:
创建: 只要是一次请求(客户端-----服务器)request对象就是一次请求
访问页面 是否创建request对象
html 是
jsp 是
servlet 是
销毁:一次响应完成后就会销毁
2.监听web对象的属性变化(添加/移除/替换)
HttpServletContext的属性监听器
创建监听器 监听值得添加、移除、替换
package listener01; import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener; public class MyServletContextAttributeListener implements ServletContextAttributeListener{ @Override
public void attributeAdded(ServletContextAttributeEvent scab) {
// 设置值
System.out.println("ServletContextAttribute---add");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {
// 移除值
System.out.println("ServletContextAttribute---remove"); } @Override
public void attributeReplaced(ServletContextAttributeEvent scab) {
// 替代值
System.out.println("ServletContextAttribute---replaced"); } }
配置监听类
<listener>
<listenerclass>listener01.MyServletContextAttributeListener</listener-class>
</listener>
测试页面
<%@ 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>Insert title here</title>
</head>
<body>
<%
getServletContext().setAttribute("name", "张三");//设置ServletContext的值为nam值为“张三” 监听器中的attributeReplaced方法会执行 会在服务器加载时会给赋值
%> <% request.getSession.getAttribute("name") %>//获取ServletContext中名字为name的值
<%
getServletContext.setAttribute("name", "李四");//替换ServletContext的值为nam值为“李四” 监听器中的attributeReplaced方法会执行
%>
<%
getServletContext.removeAttribute("name");//移除session的值为nam值 监听器中的attributeRemoved方法会执行
%>
<h3>测试Session属性监听</h3> </body> </html>
HttpSession的属性监听器
创建监听类
package listener01; import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent; public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener{ @Override
public void attributeAdded(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub
System.out.println("sessionAttribute---add");
} @Override
public void attributeRemoved(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub
System.out.println( "sessionAttribute---remove");
} @Override
public void attributeReplaced(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub
System.out.println( "sessionAttribute---replace"); } }
配置监听类
<listener>
<listener-class>listener01.MyHttpSessionAttributeListener</listener-class>
</listener>
测试
<%@ 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>Insert title here</title>
</head>
<body>
<%
request.getSession().setAttribute("name", "张三");//设置session的值为nam值为“张三” 监听器中的attributeAdded方法会执行
%> <% request.getSession.getAttribute("name") %>//获取session中名字为name的值
<%
request.getSession().setAttribute("name", "李四");//替换session的值为nam值为“李四” 监听器中的attributeReplaced方法会执行
%> <%
request.getSession().removeAttribute("name");//移除session的值为nam值 监听器中的attributeRemoved方法会执行
%> <h3>测试Session属性监听</h3> </body> </html>
HttpServletRequest的属性监听器
创建监听类
package listener01; import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener; public class MyServletRequestAttributeListener implements ServletRequestAttributeListener{ @Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
// 设置值
System.out.println("requestAttribute---add"); } @Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
// 移除值
System.out.println("requestAttribute---remove"); } @Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
// 替换值
System.out.println("requestAttribute---replace"); } }
配置监听类
<listener>
<listener-class>listener01.MyServletRequestAttributeListener</listener-class>
</listener>
测试
<%@ 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>Insert title here</title>
</head>
<body>
<%
request.setAttribute("name", "张三");
%>
<% request.setAttribute("name", "李四"); %>
<h3>测试servletContextAttribute监听</h3>
</body>
</html>
3.感知类监听器
HttpSessionBindingListener 绑定监听器
创建方式: 只能绑定类 只要类创建了对象就会执行监听器中的绑定方法(valueBound)
解绑方式: 服务器关闭 或对象被回收了(valueUnbound)
session 绑定
session解绑
package listener02; import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener; public class MyHttpSessionBindingListener implements HttpSessionBindingListener { private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("绑定"); } @Override
public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("解绑");
} }
HttpSessionActivationListener
package listener02; import java.io.Serializable; import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent; public class User implements Serializable,HttpSessionBindingListener,HttpSessionActivationListener{ private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("向Session中绑定user对象");
} @Override
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("解绑session中的user对象");
}
@Override
public void sessionWillPassivate(HttpSessionEvent se) {
System.out.println("钝化了");
}
@Override
public void sessionDidActivate(HttpSessionEvent se) {
System.out.println("活化了");
} }
钝化:持久化到磁盘(序列化的对象)
活化:从磁盘中加载到项目中(反序列化对象)
操作过程: 钝化
1:创建user对象,并进行赋值,添加到session对象中。
2. 正常关闭服务器,这样session就钝化到磁盘中了
活化
1:创建user对象 可以直接获取值(其中的值是由钝化的user对象加载进来的)
<%@page import="listener02.User"%>
<%@page import="listener02.MyHttpSessionBindingListener"%>
<%@ 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>Insert title here</title>
</head>
<body>
<%
User user=new User();//创建User对象 user.setName("张三");//设置其中的name值
session.setAttribute("name", user); //将user对象添加到session对象中
%>
</body>
</html>