首先,什么是监听器?
监听器主要有以下几种:
现在,我们来一一试过,把所有的事件监听器全部写好,按照输出,我们来分析:
首先是:ServletContextEvent事件:
package com.xust.jia.listener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class Listenerapplication implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"application结束");
System.out.println();
System.out.println();
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"application开始");
System.out.println();
System.out.println();
}
}
接下来是
ServletContextAttributeEvent:
package com.xust.jia.listener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class Listernerapplicationattr implements ServletContextAttributeListener{
@Override
public void attributeAdded(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"application的属性被新建"+arg0.getName());
System.out.println();
System.out.println();
}
@Override
public void attributeRemoved(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdFormat.format(new Date())+"application的"+arg0.getName()+"属性被删除");
System.out.println();
System.out.println();
}
@Override
public void attributeReplaced(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"application的"+arg0.getName()+"属性值被修改为:"+arg0.getValue());
System.out.println();
System.out.println();
}
}
HttpSessionEvent事件的HttpSessionListener接口:
package com.xust.jia.listener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class Listenersession implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent se) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date()) + "session对象被创建" + se.getSession());
System.out.println();
System.out.println();
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"session对象被销毁" + se.getSource()+se.getClass());
System.out.println();
System.out.println();
}
}
HttpSessionEvent事件的HttpSessionActivationListener接口:
这个接口并不是直接监听的,而是依附于具体的实体类对象中,即由具体需要的实体类进行实现这个接口,实现这个接口所定义的方法,这样,当在使用到这个实体类的对象时,就会触发这个监听器,从而执行对应的方法:
首先,我们现在实体类中实现这个接口:
package com.xust.jia.listener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
public class Man implements HttpSessionActivationListener{
@Override
public void sessionDidActivate(HttpSessionEvent se) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date()) +"这是session的didActive方法");
System.out.println();
System.out.println();
}
@Override
public void sessionWillPassivate(HttpSessionEvent se) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"这是session的Willpassivate方法");
System.out.println();
System.out.println();
}
public Man() {
super();
// TODO Auto-generated constructor stub
}
}
接下来,我们重写一个测试页面:
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.xust.jia.listener.Man"%>
<%@page import="java.util.Date" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>这是测试session的activationListener接口</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>测试session的activationListener接口</h1>
<%
if(session.getAttribute("act") == null)
{
Man man = new Man();
session.setAttribute("act", man);
}
System.out.println(session.getId()+session.getAttribute("act"));
Date d = new Date(session.getCreationTime());
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d));
%>
</body>
</html>
然后,在对服务器进行配置:
其中,htmlday1是项目名称,然后,在里面写入:
接下来,我们先打开配置的xml,然后通过xml来访问测试页面进行测试:
说明这个测试页面里的语句被执行,然后关闭服务器,重新打开服务器,发现在E:盘下多了一个文件夹,a:
发现多了一个SESSION文件,打开如下:
在控制台输出以下信息:
注意,关闭服务器是正常关闭,不是直接关闭。
HttpSessionBindingEvent事件的HttpSessionAttributeListener接口:
package com.xust.jia.listener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class Listenersessionattr implements HttpSessionAttributeListener{
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"session的"+event.getName()+"属性被增加,值为:"+event.getValue());
System.out.println();
System.out.println();
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"session的"+event.getName()+"属性被删除,值为:"+event.getValue());
System.out.println();
System.out.println();
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"session的"+event.getName()+"属性值被修改为:"+event.getValue());
System.out.println();
System.out.println();
}
}
HttpSessionBindingEvent事件的HttpSessionBindingListener接口:
因为这个接口比较特殊,是在实体类中进行实现方法,而且不需要在Web.xml文件中进行配置,所以,我们只是简单的检测验证一下这个接口:
package com.xust.jia.listener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class Person implements HttpSessionBindingListener{
@Override
public void valueBound(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"这是Binding的Bound"+event.getName()+event.getValue());
System.out.println();
System.out.println();
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"这是Binding的Unbound"+event.getName()+event.getValue());
System.out.println();
System.out.println();
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
}
接下来,我们在servlet类中进行调用这个类,然后把这个类对象加载到会话的属性中,最后使用session的方法,销毁这个session:
在servlet的post或者get方法中写入如下代码:
Person person = new Person();
HttpSession hsHttpSession = request.getSession();
hsHttpSession.setAttribute("person", person);
hsHttpSession.invalidate();
接下来,我们调入这个servlet方法中,进行测试:
显示结果如下:
ServletRequestEvent事件:
package com.xust.jia.listener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class Listenerrequest implements ServletRequestListener{
@Override
public void requestDestroyed(ServletRequestEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"request被创建");
System.out.println();
System.out.println();
}
@Override
public void requestInitialized(ServletRequestEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"request被销毁");
System.out.println();
System.out.println();
}
}
ServletRequestAttributeEvent事件:
package com.xust.jia.listener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
public class Listenerrequestattr implements ServletRequestAttributeListener{
@Override
public void attributeAdded(ServletRequestAttributeEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"request的"+arg0.getName()+"属性被创建,值为:"+arg0.getValue());
System.out.println();
System.out.println();
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"request的"+arg0.getName()+"属性被删除,值为:"+arg0.getValue());
System.out.println();
System.out.println();
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent arg0) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println();
System.out.println();
System.out.println(sdf.format(new Date())+"request的"+arg0.getName()+"属性被修改,值为:"+arg0.getValue());
System.out.println();
System.out.println();
}
}
接下来,我们写一个测试页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>secondjsp page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="../CSS/index.css">
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#div").hide();
$("#button").click(function() {
$("#div").show();
});
});
</script>
</head>
<body>
<%
String nameString = "小李子";
String pswdString = "1234567";
String sexString = "男";
String ageString = "21";
/* String nmString = request.getParameter("user");
String pdString = request.getParameter("pswd");
String sString = request.getParameter("sex");
String aString = request.getParameter("age"); */
/* nameString = new String(nmString.getBytes("ISO-8859-1"), "UTF-8");
pswdString = new String(pdString.getBytes("ISO-8859-1"), "UTF-8");
sexString = new String(sString.getBytes("ISO-8859-1"), "UTF-8");
ageString = new String(aString.getBytes("ISO-8859-1"), "UTF-8"); */
session.setAttribute("name", nameString);
session.setAttribute("pswd", pswdString);
session.setAttribute("sex", sexString);
session.setAttribute("age", ageString);
request.setAttribute("name", nameString);
request.setAttribute("pswd", pswdString);
request.setAttribute("sex", sexString);
request.setAttribute("age", ageString);
pageContext.setAttribute("name", nameString);
pageContext.setAttribute("pswd", pswdString);
pageContext.setAttribute("sex", sexString);
pageContext.setAttribute("age", ageString);
application.setAttribute("name", nameString);
application.setAttribute("pswd", pswdString);
application.setAttribute("sex", sexString);
application.setAttribute("age", ageString);
application.setAttribute("name", "123");
application.setAttribute("pswd", "456");
application.setAttribute("sex", "女");
application.setAttribute("age", "25");
request.setAttribute("name", "123");
request.setAttribute("pswd", "456");
request.setAttribute("sex", "女");
request.setAttribute("age", "25");
session.setAttribute("name", "123");
session.setAttribute("pswd", "456");
session.setAttribute("sex", "女");
session.setAttribute("age", "25");
String namepageContextString = (String) pageContext.getAttribute("name");
String namesessionString = (String) session.getAttribute("name");
String namerequestString = (String) request.getAttribute("name");
String nameapplicationString = (String) application.getAttribute("name");
String pswdpageContextString = (String) pageContext.getAttribute("pswd");
String pswdsessionString = (String) session.getAttribute("pswd");
String pswdrequestString = (String) request.getAttribute("pswd");
String pswdapplicationString = (String) application.getAttribute("pswd");
String sexpageContextString = (String) pageContext.getAttribute("sex");
String sexsessionString = (String) session.getAttribute("sex");
String sexrequestString = (String) request.getAttribute("sex");
String sexapplicationString = (String) application.getAttribute("sex");
String agepageContextString = (String) pageContext.getAttribute("age");
String agesessionString = (String) session.getAttribute("age");
String agerequestString = (String) request.getAttribute("age");
String ageapplicationString = (String) application.getAttribute("age");
%>
<div align="center">
<button id="button">点击进行pageContext测试</button>
<br> <a href="JSP/four.jsp">点击进行session测试</a><br> <b>进行application测试:重启浏览器,并转到此链接:http://localhost:8080/htmlday1/JSP/four.jsp</b>
</div>
<div align="center" id="div"
style="font-family: 楷体;font-size: 25px;color: green;">
客户信息:<br> pageContext:<br> 姓名:<%=namepageContextString%><br>
密码:<%=pswdpageContextString%><br> 性别:<%=sexpageContextString%><br>
年龄:<%=agepageContextString%><br> session:<br> 姓名:<%=namesessionString%><br>
密码:<%=pswdsessionString%><br> 性别:<%=sexsessionString%><br>
年龄:<%=agesessionString%><br> request:<br> 姓名:<%=namerequestString%><br>
密码:<%=pswdrequestString%><br> 性别:<%=sexrequestString%><br>
年龄:<%=agerequestString%><br> application:<br> 姓名:<%=nameapplicationString%><br>
密码:<%=pswdapplicationString%><br> 性别:<%=sexapplicationString%><br>
年龄:<%=ageapplicationString%><br> <a href="JSP/Login.jsp">回到信息输入界面</a>
</div>
</body>
</html>
在这个页面中,我们对request,pageContext,session,application的属性进行设置,修改。
其次还有一个页面,可以帮助我们完成测试:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>fourjsp page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String namepageContextString = (String)pageContext.getAttribute("name");
String namesessionString = (String)session.getAttribute("name");
String namerequestString = (String)request.getAttribute("name");
String nameapplicationString = (String)application.getAttribute("name");
String pswdpageContextString = (String)pageContext.getAttribute("pswd");
String pswdsessionString = (String)session.getAttribute("pswd");
String pswdrequestString = (String)request.getAttribute("pswd");
String pswdapplicationString = (String)application.getAttribute("pswd");
String sexpageContextString = (String)pageContext.getAttribute("sex");
String sexsessionString = (String)session.getAttribute("sex");
String sexrequestString = (String)request.getAttribute("sex");
String sexapplicationString = (String)application.getAttribute("sex");
String agepageContextString = (String)pageContext.getAttribute("age");
String agesessionString = (String)session.getAttribute("age");
String agerequestString = (String)request.getAttribute("age");
String ageapplicationString = (String)application.getAttribute("age");
%>
客户信息:<br>
pageContext:<br>
姓名:<%=namepageContextString %><br>
密码:<%=pswdpageContextString %><br>
性别:<%=sexpageContextString %><br>
年龄:<%=agepageContextString %><br>
session:<br>
姓名:<%=namesessionString %><br>
密码:<%=pswdsessionString %><br>
性别:<%=sexsessionString %><br>
年龄:<%=agesessionString %><br>
request:<br>
姓名:<%=namerequestString %><br>
密码:<%=pswdrequestString %><br>
性别:<%=sexrequestString %><br>
年龄:<%=agerequestString %><br>
application:<br>
姓名:<%=nameapplicationString %><br>
密码:<%=pswdapplicationString %><br>
性别:<%=sexapplicationString %><br>
年龄:<%=ageapplicationString %><br>
</body>
</html>
接下来:
因为session并不会在关闭浏览器的时候,及时的销毁session对象,所以,我们在web.xml中进行设置自动销毁时间:
<session-config>
<session-timeout>1</session-timeout>
</session-config>
然后启动项目,对这些事件进行监听:
当服务器开始启动时,就会创建一个application对象,然后服务器对application对象的属性进行操作:
为什么会有一个request对象被销毁呢?
因为浏览器打开有一个首页,但是我们需要浏览器跳转到指定的页面,所以手动的修改了url地址,只要url的地址发生改变,那么,就会产生一个request请求,那么就会有一个request请求被销毁。
因为我们新打开一个浏览器,所以就产生一个新的会话,就会创建一个新的session对象,当我们打开测试的页面时,因为在测试页面里对session,application等进行属性设置,修改,所以:
因为在jsp内部引入了jQuery文件,所以会发生request对象的销毁与新建:
这里是因为,我们设置了session的自动销毁时间是1分钟,所以当超过一分钟的时候,会自动的销毁session,此时如果刷新页面,则会:
到了这里,我们,对3大对象,6个事件八个接口进行了测试,可以检测到application创建销毁的接口,application属性改变的接口,request创建销毁的接口,request属性改变的接口,session创建销毁的接口,session属性改变的接口,以及session的active和Binding的接口事件。
所以,3大对象6种事件8个接口,到了这里就测试完毕了。