在写此篇前,看了一下园友写的,感觉其基础知识归纳的十分全面,我在此就不累赘的写了,链接地址(http://www.cnblogs.com/sherryueda/p/4273169.html),
我就写一下关于监听器的具体应用:
功能是负责监听WEB的各种操作,当相关的事件触发之后将产生事件,并对此事件进行处理,在WEB中可以对application、session、request三种操作进行监听。
package com.oumyye.监听器; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ServletContextListenerDemo implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { // 上下文初始化时触发 System.out.println("** 容器初始化 --> " + event.getServletContext().getContextPath()); } public void contextDestroyed(ServletContextEvent event) { // 上下文销毁时触发 System.out.println("** 容器销毁 --> " + event.getServletContext().getContextPath()); } }
web.xml配置
<listener> <listener-class> com.oumyye.监听器.ServletContextListenerDemo </listener-class> </listener>
对session监听
package com.oumyye.监听器; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class HttpSessionListenerDemo implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { // 创建session触发 System.out.println("** SESSION创建,SESSION ID = " + event.getSession().getId()); } public void sessionDestroyed(HttpSessionEvent event) { // 销毁session触发 System.out.println("** SESSION销毁,SESSION ID = " + event.getSession().getId()); } }
web.xml配置
<listener> <listener-class> <listener> <listener-class> com.oumyye.监听器.HttpSessionListenerDemo </listener-class> </listener>
session销毁的操作
package com.oumyye.监听器; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class HttpSessionAttributeListenerDemo implements HttpSessionAttributeListener { public void attributeAdded(HttpSessionBindingEvent event) {// 属性增加时调用 System.out.println(event.getSession().getId() + ",增加属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } public void attributeRemoved(HttpSessionBindingEvent event) {// 属性删除时调用 System.out.println(event.getSession().getId() + ",删除属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } public void attributeReplaced(HttpSessionBindingEvent event) {// 属性替换时调用 System.out.println(event.getSession().getId() + ",替换属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } }
web.xml配置
<listener> <listener-class> com.oumyye.监听器.HttpSessionAttributeListenerDemo </listener-class> </listener>
package com.oumyye.监听器; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class HttpSessionAttributeListenerDemo implements HttpSessionAttributeListener { public void attributeAdded(HttpSessionBindingEvent event) {// 属性增加时调用 System.out.println(event.getSession().getId() + ",增加属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } public void attributeRemoved(HttpSessionBindingEvent event) {// 属性删除时调用 System.out.println(event.getSession().getId() + ",删除属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } public void attributeReplaced(HttpSessionBindingEvent event) {// 属性替换时调用 System.out.println(event.getSession().getId() + ",替换属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } }
web.xml同上
package com.oumyye.监听器; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; public class ServletRequestListenerDemo implements ServletRequestListener { public void requestInitialized(ServletRequestEvent event) { System.out.println("** request初始化。http://" + event.getServletRequest().getRemoteAddr() + event.getServletContext().getContextPath()); } public void requestDestroyed(ServletRequestEvent event) { System.out.println("** request销毁。http://" + event.getServletRequest().getRemoteAddr() + event.getServletContext().getContextPath()); } }
web.xml配置
<listener> <listener-class> com.oumyye.监听器.ServletRequestListenerDemo </listener-class> </listener>
package com.oumyye.监听器; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; public class ServletRequestAttributeListenerDemo implements ServletRequestAttributeListener { public void attributeAdded(ServletRequestAttributeEvent event) { System.out.println("** 增加request属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } public void attributeRemoved(ServletRequestAttributeEvent event) { System.out.println("** 删除request属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } public void attributeReplaced(ServletRequestAttributeEvent event) { System.out.println("** 替换request属性 --> 属性名称:" + event.getName() + ",属性内容:" + event.getValue()); } }
web.xml配置
<listener> <listener-class> com.oumyye.监听器.ServletRequestAttributeListenerDemo </listener-class> </listener>
package com.oumyye.监听器; import java.util.*; public class LoginList { private static LoginList user = new LoginList(); private Vector vector = null; //private调用构造函数, //防止被外界类调用产生新的instance对象 public LoginList() { this.vector = new Vector(); } //外界使用的instance对象 public static LoginList getInstance() { return user; } //用户登录 public boolean addLoginList(String user) { if (user != null) { this.vector.add(user); return true; } else { return false; } } //获取用户列表 public Vector getList() { return vector; } //删除用户 public void removeLoginList(String user) { if (user != null) { vector.removeElement(user); } } }
创建LoginNote.java类,实现HttpSessionBindingListener类
package com.oumyye.监听器; import javax.servlet.http.HttpSessionBindingEvent; public class LoginNote implements javax.servlet.http.HttpSessionBindingListener { private String user; private LoginList container = LoginList.getInstance(); public LoginNote() { user = ""; } public void setUser(String user) { this.user = user; } public String getUser() { return this.user; } public void valueBound(HttpSessionBindingEvent arg0) { System.out.println(this.user+"该用户己经上线" ); } public void valueUnbound(HttpSessionBindingEvent arg0) { System.out.println(this.user+"该用户己经下线"); if (user != "") { container.removeLoginList(user); } } }
页面文件
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>监听查看在线用户</title> </head> <script language="javascript"> function checkEmpty(form){ for(i=0;i<form.length;i++){ if(form.elements[i].value==""){ alert("表单信息不能为空"); return false; } } } </script> <link href="css/style.css" rel="stylesheet" type="text/css"> <body> <div align="center"> <table width="400" height="150" border="0" cellpadding="0" cellspacing="0" bgcolor="lightblue"> <Tr><td> </td></Tr> <tr> <td align="center"> <form name="form" method="post" action="LoginList.jsp" onSubmit="return checkEmpty(form)"> <input type="text" name="user"><br><br> <input type="submit" name="Submit" value="登录上线"> </form> </td> </tr> </table> </div> </body> </html>
<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %> <%@ page import="java.util.*"%> <%@ page import="com.oumyye.监听器.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>监听查看在线用户</title> <link href="css/style.css" rel="stylesheet" type="text/css"> </head> <% LoginList list=LoginList.getInstance(); LoginNote ut=new LoginNote(); String name=request.getParameter("user"); ut.setUser(name); session.setAttribute("list",ut); list.addLoginList(ut.getUser()); session.setMaxInactiveInterval(10); %> <body> <div align="center"> <table width="400" height="150" border="0" cellpadding="0" cellspacing="0" bgcolor="lightblue"> <tr align="center"><td>用户在线列表</td></tr> <tr> <td align="center"><br> <textarea rows="5" cols="22"> <% Vector vector=list.getList(); if(vector!=null&&vector.size()>0){ for(int i=0;i<vector.size();i++){ out.println(vector.elementAt(i)+"己登录在线"); } } %> </textarea><br><br> <a href="loginOut.jsp">返回</a> </td> </tr> </table> </div> </body> </html>
<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %> <% session.invalidate(); out.println("<script>parent.location.href='index.jsp';</script>"); %>