监听器的主要作用就是监听,当目标发生变化的时候就会触发一定的方法,这也就是所谓的事件触发机制。在这种机制中要有三个要素,也就是事件,事件源,处理事件的方法。这三要素之间是相互制约的。一旦事件处理方法被触发必定有事件发生,也就可以得到触发 的事件,通过事件也就可以得到事件源,也就谁触发了事件,这样据可以将这三个要素联系在一起了。一个事件源可以有多个触发事件,而一个触发事件的发生将有多个方法来调用,这也就是所谓的一对多关系,通过一对多关系我们很容易从多端得到一端,相反从一端无法得到多端,这个道理大家都是很清楚的,所以也就可以很好的理解事件处理方法可以得到事件对象,事件对象又可以得到事件源。
1. web监听器的开发步骤
首先建立一个类实现listenner接口
public class ContextTestListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { //具体实现类 } public void contextDestroyed(ServletContextEvent sce) { //具体实现类 } }
其次,在web.xml中配置监听器
<listener> <listener-class>com.jason.listener.ContextTestListener</listener-class> </listener>
2. web监听器的分类,如图所示,目前分如下几种监听器 ServletContext,Session,Request的
3. 各种监听器的详细介绍及用法示例
(1) 与ServletContext相关的监听器接口为ServletContextListener,ServletContextAttributeListener,该监听器针对的是整个web应用程序的生命周期,其范围最大的。一般用来在应用程序初始化时读取数据库连接对象,初始化应用程序程序的配置等。 其接口定义如下:
public interface ServletContextListener { public void contextInitialized(ServletContextEvent sce); public void contextDestroyed(ServletContextEvent sce); }
典型应用:需求统计一个网页请求的访问量,该web应用停止时,将访问量数值写到配置文件中,改应用程序重新启动,数值从上次访问量开始。
<1> 实现ServletContextListener接口,在初始化话方法中读取配置文件中访问量的信息,在销毁方法中存储访问量的信息。因为在应用程序启动时,会调用contextInitialized方法,在应用程序停止时,会调用contextDestroyed方法。该例子应用了properties的操作,不熟悉properties请google下用法,特别注意读取文件和存储文件的写法,代码实现如下:
public class ContextTestListener implements ServletContextListener { private static final String COUNT_NAME = "count"; private static final String PROPERTIES_NAME = "WEB-INF/count.properties"; /* * (non-Javadoc) * * @see * javax.servlet.ServletContextListener#contextInitialized(javax.servlet * .ServletContextEvent) */ public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); InputStream inputstream = context.getResourceAsStream(PROPERTIES_NAME); Properties prop = new Properties(); try { prop.load(inputstream); } catch (IOException e) { throw new RuntimeException(e); } int count = Integer.parseInt(prop.getProperty(COUNT_NAME)); context.setAttribute(COUNT_NAME, count); } /* * (non-Javadoc) * * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet. * ServletContextEvent) */ public void contextDestroyed(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); Object countAttr = context.getAttribute(COUNT_NAME); if(countAttr != null){ int count = (Integer) context.getAttribute(COUNT_NAME); InputStream inputstream = context.getResourceAsStream(PROPERTIES_NAME); Properties prop = new Properties(); try { prop.load(inputstream); prop.setProperty(COUNT_NAME, String.valueOf(count)); } catch (IOException e) { throw new RuntimeException(e); } String filepath = context.getRealPath("/"); FileOutputStream fos = null; try { fos = new FileOutputStream(filepath + PROPERTIES_NAME); } catch (FileNotFoundException e) { throw new RuntimeException(e); } try { prop.store(fos, "add the count"); } catch (IOException e) { throw new RuntimeException(e); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { System.err.println("Cant close stream :" + e.getMessage()); } } } } } }
<2> 定义TestCountServlet,作为请求访问网页的请求URL,其实现代码如下,记录方法量,同时做跳转到访问量页面,代码如下:
package com.jason.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestAccountServlet */ public class TestAccountServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub ServletContext context = request.getSession().getServletContext(); Integer count = (Integer) context.getAttribute("count"); if(count == null){ count = 1; }else{ count ++; } context.setAttribute("count",count); // do other things response.sendRedirect(request.getContextPath() + "/count.jsp"); } }
<3>webxml中的配置,webxml中配置了监听器,servlet及welcoen页面,内容如下:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WebListener</display-name> <listener> <listener-class>com.jason.listener.ContextTestListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>TestAccountServlet</display-name> <servlet-name>TestAccountServlet</servlet-name> <servlet-class>com.jason.servlet.TestAccountServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestAccountServlet</servlet-name> <url-pattern>/TestAccountServlet</url-pattern> </servlet-mapping> </web-app>
<4> 在WEB-INF 目录下,建立count.properties,内容如下:
count=0
<5>建立index和count页面,index页面如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!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 page</title> </head> <body> <a href="${pageContext.request.contextPath}/TestAccountServlet">visit it to test </a> </body> </html>
在index页面中注意${pageContext.request.contextPath},在jsp页面调用servlet的URL时,如果没设定basepath,前面一定要加上contextpath的路径,否则,servlet的地址是不对的。或者也可以直接用<c:out value=“/TestAccountServlet”/〉,因为jstl 中该标签直接在其前面添加contextpath路径。count页面的内容如下:
<%@ 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>count here </title> </head> <body> visit count:${applicationScope.count} <% System.out.println(application.getAttribute("count")); %> </body> </html>
利用${applicationScope.count}获取数值,该程序运行,每点击访问一次count页面,其访问量加1.
(2) 与Session相关的监听器有有个四个HttpSessionListener,HttpSessionAttributeListener ,HttpSessionBindListener和HttpSessionActivationListener
<1>HttpSessionListener 是生命周期监听器,如果想对Session对象创建或结束时,做相应的动作可实现HttpSessionListener,该接口的定义如下:
public interface HttpSessionListener { public void sessionCreated(HttpSessionEvent se) ; public void sessionDestroyed(HttpSessionEvent se) ; }
关于该监听器的用法,例如我们防止用户重新登录时,常常在数据库中定义字段状态,来表明该用户是否登录,在注销时,我们将其状态修改,但好多情况下,我们直接关闭浏览器,造成数据库状态没法更新,同时,用户登录session也有存活期限,此时我们希望在session销毁时,改变数据库状态,我们可以在HttpSessionListener监听器的sessionDestroyed(se)方法中在session中获取该用户,操作数据库,将该用户的登录状态设置为未登录。
/** * Application Lifecycle Listener implementation class OnlinePepoleListener * */ public class OnlinePepoleListener implements HttpSessionListener { /** * @see HttpSessionListener#sessionCreated(HttpSessionEvent) */ public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub } /** * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent) */ public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub HttpSession session = se.getSession(); String user = (String)session.getAttribute("login"); //修改数据库用户登录状态为未登录 } }
典型应用:统计当前用户在线数
(1)index页面
<%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="login.jsp" method="post"> userName:<input type="text" name="username" /> <br /> <input type="submit" value="login" /> </form> </body> </html>
2.login页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.util.*"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); // 取得登录的用户名 String username = request.getParameter("username"); // 把用户名保存进session session.setAttribute("username", username); // 把用户名放入在线列表 List onlineUserList = (List) application.getAttribute("onlineUserList"); // 第一次使用前,需要初始化 if (onlineUserList == null) { onlineUserList = new ArrayList(); application.setAttribute("onlineUserList", onlineUserList); } onlineUserList.add(username); // 成功 response.sendRedirect("result.jsp"); %> </body> </html>
3.result页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page isELIgnored="false"%> <%@page import="java.util.List"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h3>您好:${username} [<a href="logout.jsp">注销</a>]</h3> 当前在线用户: <table> <% List onlineUserList = (List) application.getAttribute("onlineUserList"); for (int i = 0; i < onlineUserList.size(); i++) { String onlineUsername = (String) onlineUserList.get(i); %> <tr> <td><%=onlineUsername%></td> </tr> <% } %> </table> </body> </html>
4、logout页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.util.*"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% // 取得登录的用户名 String username = (String) session.getAttribute("username"); // 销毁session session.invalidate(); // 从在线列表中删除用户名 List onlineUserList = (List) application.getAttribute("onlineUserList"); onlineUserList.remove(username); // 成功 response.sendRedirect("index.jsp"); %> </body> </html>
5、监听器代码
package com.jason.listener; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * Application Lifecycle Listener implementation class OnlineUserListener * */ public class OnlineUserListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { System.out.println("新建session:"+event.getSession().getId()); } public void sessionDestroyed(HttpSessionEvent event) { HttpSession session = event.getSession(); ServletContext application = session.getServletContext(); // 取得登录的用户名 String username = (String) session.getAttribute("username"); // 从在线列表中删除用户名 List onlineUserList = (List) application.getAttribute("onlineUserList"); onlineUserList.remove(username); System.out.println(username+"已经退出!"); } }
6 web的配置
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>OnlinePepoleListener</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>com.jason.listener.OnlineUserListener</listener-class> </listener> </web-app>
一旦监听器发现调用了sessionDestoryed方法就会把其用户从在线人数中delete,在下面两种情况下会发生sessionDestoryed事件
a.执行session.invalidate()方法时
logout.jsp中调用了 session.invalidate()方法
b.session会话超时
session的默认超时事件是30分钟,30分钟后自动销毁session
2、HttpSessionBindingListener
HttpSessionBindingListener虽然叫做监听器,但使用方法与HttpSessionListener完全不同。我们实际看一下它是如何使用的。新建类OnlineUserBindingListener,实现HttpSessionBindingListener接口,构造方法传入username参数,HttpSessionBindingListener内有两个方法valueBound(HttpSessionBindingEvent event)和valueUnbound(HttpSessionBindingEvent event),前者为数据绑定,后者为取消绑定所谓对session进行数据绑定,就是调用session.setAttribute()把HttpSessionBindingListener保存进session中。
在login.jsp中做这一步:
<%@page import="com.test.OnlineUserBindingListener"%> <%@ page contentType="text/html;charset=utf-8"%> <%@ page import="java.util.*"%> <% request.setCharacterEncoding("UTF-8"); // 取得登录的用户名 String username = request.getParameter("username"); // 把用户名放入在线列表 session.setAttribute("onlineUserBindingListener", new OnlineUserBindingListener(username)); // 成功 response.sendRedirect("result.jsp"); %>
这就是HttpSessionBindingListener和HttpSessionListener之间的最大区别:HttpSessionListener只需要设置到web.xml中就可以监听整个应用中的所有session。HttpSessionBindingListener必须实例化后放入某一个session中,才可以进行监听。从监听范围上比较,HttpSessionListener设置一次就可以监听所有session,HttpSessionBindingListener通常都是一对一的。正是这种区别成就了HttpSessionBindingListener的优势,我们可以让每个listener对应一个username,这样就不需要每次再去session中读取username,进一步可以将所有操作在线列表的代码都移入listener,更容易维护。
package com.test; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class OnlineUserBindingListener implements HttpSessionBindingListener { String username; public OnlineUserBindingListener(String username){ this.username=username; } public void valueBound(HttpSessionBindingEvent event) { HttpSession session = event.getSession(); ServletContext application = session.getServletContext(); // 把用户名放入在线列表 List onlineUserList = (List) application.getAttribute("onlineUserList"); // 第一次使用前,需要初始化 if (onlineUserList == null) { onlineUserList = new ArrayList(); application.setAttribute("onlineUserList", onlineUserList); } onlineUserList.add(this.username); } public void valueUnbound(HttpSessionBindingEvent event) { HttpSession session = event.getSession(); ServletContext application = session.getServletContext(); // 从在线列表中删除用户名 List onlineUserList = (List) application.getAttribute("onlineUserList"); onlineUserList.remove(this.username); System.out.println(this.username + "退出。"); } }
这里可以直接使用listener的username操作在线列表,不必再去担心session中是否存在username。
valueUnbound的触发条件是以下三种情况:
a.执行session.invalidate()时。
b.session超时,自动销毁时。
c.执行session.setAttribute("onlineUserListener", "其他对象");或session.removeAttribute("onlineUserListener");将listener从session中删除时。
因此,只要不将listener从session中删除,就可以监听到session的销毁。
典型事例:强行把用户踢出例子
1、监听器代码如下:
package com.jason.listener; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import com.jason.domain.User; /** * Application Lifecycle Listener implementation class SessionAttributeListener * */ public class SessionAttributeListener implements HttpSessionAttributeListener { /** * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent) */ public void attributeRemoved(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } /** * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent) */ public void attributeAdded(HttpSessionBindingEvent se) { // TODO Auto-generated method stub System.out.println("start"); Object obj = se.getValue(); if(obj instanceof User){ HttpSession session = se.getSession(); ServletContext application = session.getServletContext(); Map map = (Map) application.getAttribute("userMap"); if(map == null){ map = new HashMap(); application.setAttribute("userMap", map); } User user = (User) obj; map.put(user.getUserName(), session); } } /** * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent) */ public void attributeReplaced(HttpSessionBindingEvent arg0) { // TODO Auto-generated method stub } }
2.loginservlet
package com.jason.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.jason.domain.User; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String username = request.getParameter("username"); String password = request.getParameter("password"); User user = new User(); user.setUserName(username); user.setPassword(password); HttpSession session= request.getSession(); session.setAttribute("user", user); response.sendRedirect(request.getContextPath()+"/user.jsp"); } }
3、kickoutServelt
package com.jason.servlet; import java.io.IOException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class KickUserServlet */ public class KickUserServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String username = request.getParameter("username"); Map map = (Map) request.getServletContext().getAttribute("userMap"); HttpSession session = (HttpSession) map.get(username); if(session != null){ session.invalidate(); map.remove(username); } request.getRequestDispatcher("/listUser.jsp").forward(request, response); } }
6.index页面
<%@ 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> <form method="post" action="${pageContext.request.contextPath }/LoginServlet"> 用户名:<input type="text" name="username" /><br/> 密码: <input type="password" name="password" /><br/> <input type="submit" value="登陆" /><br/> </form> </body> </html>
2 user 页面
<%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> 欢迎您:${user.userName} <a href="listUser.jsp" >user list</a> </body> </html>
3 userlist页面
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@page import="java.util.*"%> <!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.userName} <br/> 当前登陆用户有:<br/> <% Map onlineUserMap = (Map) application.getAttribute("userMap"); System.out.println(onlineUserMap.size()); %> <c:forEach var="me" items="${userMap}"> ${me.key} <a href="${pageContext.request.contextPath}/KickUserServlet?username=${me.key}" >踢死你</a> </c:forEach> </body> </html>
(3)与HttpServletRequest的监听器有ServletRequestListener,ServletRequestAttributeListener和异步处理的相关监听器,该监听器是3.0新加的。
关于ServletRequestListener,更多应用于统计访问量等
这里就介绍这些。