Listener的简单介绍及使用
Listener的简单介绍及使用
一、Listener概述
Listener可以监听容器中某一执行动作,并根据其要求做出相应的响应。
常用的Web事件的监听接口如下:
ServletContextListener:用于监听Web的启动及关闭
ServletContextAttributeListener:用于监听ServletContext范围内属性的改变
ServletRequestListener:用于监听用户请求
ServletRequestAttributeListener:用于监听ServletRequest范围属性的改变
HttpSessionListener:用于监听用户session的开始及结束
HttpSessionAttributeListener:用于监听HttpSession范围内的属性改变
二、ServletContextListener的使用
1、配置web.xml(下面例子都使用此配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 初始化参数 -->
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/escshop</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>pass</param-name>
<param-value></param-value>
</context-param>
<!-- 定义Filter -->
<filter>
<filter-name>logFilter</filter-name>
<filter-class>Filter.LogFilter</filter-class>
</filter>
<!-- 定义Filter拦截的URL地址 -->
<filter-mapping>
<filter-name>logFilter</filter-name>
<!-- 负责拦截的URL,/*表示拦截所有的请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 定义AuthorityFilter -->
<filter>
<filter-name>authority</filter-name>
<filter-class>Filter.AuthorityFilter</filter-class>
<init-param>
<param-name>encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>indexPage</param-name>
<param-value>/index.jsp</param-value>
</init-param>
<init-param>
<param-name>shopPage</param-name>
<param-value>/shop.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>authority</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 登录servlet -->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>Login.LoginServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<!-- 配置监听Web启动与关闭的Listener -->
<listener>
<listener-class>listener.GetConnListener</listener-class>
</listener>
<!-- 用于监听ServletContenxt(application)范围的变化 -->
<listener>
<listener-class>listener.MyServletContenxtAttributeListener</listener-class>
</listener>
<!-- 用于监听用户请求到达 -->
<listener>
<listener-class>listener.RequestListener</listener-class>
</listener>
<listener>
<listener-class>listener.OnlineRequestListener</listener-class>
</listener>
<!--用户监听用户session创建与销毁 -->
<listener>
<listener-class>listener.OnlineListener</listener-class>
</listener>
</web-app>
2、使用ServletContextListener
package listener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 监听Web应用启动及关闭
* @author xieyongxue
*
*/
public class GetConnListener implements ServletContextListener{
//应用关闭时,此方法被调用
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
ServletContext application=sce.getServletContext();
Connection conn=(Connection) application.getAttribute("conn");
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//应用启动时,此方法被调用
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
//获取到该应用的ServletContext的实例
ServletContext application=sce.getServletContext();
//获取数据库驱动
String driver=application.getInitParameter("driver");
//加载数据库连接URL
String url=application.getInitParameter("url");
//获取登录数据库的名称
String user=application.getInitParameter("user");
//获取连接数据库的密码
String pass=application.getInitParameter("pass");
try {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("加载数据库驱动失败!"+e.toString());
e.printStackTrace();
}
Connection conn= DriverManager.getConnection(url, user, pass);
//将数据库连接设置成application的属性
application.setAttribute("conn", conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("连接数据库失败!"+e.toString());
e.printStackTrace();
}
}
}
3、使用ServletContextAttributeListener
package listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContenxtAttributeListener implements ServletContextAttributeListener{
//当程序向application范围新增属性时触发此方法
public void attributeAdded(ServletContextAttributeEvent scab) {
// TODO Auto-generated method stub
ServletContext application=scab.getServletContext();
//获取新增的属性名与值
String name=scab.getName();
Object value=scab.getValue();
System.out.println(application+"范围内新增了name值为="+name+"value值="+value);
}
//当程序向application范围移除属性时触发此方法
public void attributeRemoved(ServletContextAttributeEvent scab) {
// TODO Auto-generated method stub
ServletContext application=scab.getServletContext();
//获取移除的属性名与值
String name=scab.getName();
Object value=scab.getValue();
System.out.println(application+"范围内移除了name值为="+name+"value值="+value);
}
//当程序向application范围替换属性时触发此方法
public void attributeReplaced(ServletContextAttributeEvent scab) {
// TODO Auto-generated method stub
ServletContext application=scab.getServletContext();
//获取替换的属性名与值
String name=scab.getName();
Object value=scab.getValue();
System.out.println(application+"范围内替换了name值为="+name+"value值="+value);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
4、测试例子
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
application.setAttribute("addAttrName","addAttrValue");
application.removeAttribute("addAttrName");
application.setAttribute("newAttrName","newAttrValue");
application.setAttribute("newAttrName","1234567");
request.setAttribute("requestName1","requestValue1");
request.removeAttribute("requestName1");
request.setAttribute("requestName2","requestValue2");
request.setAttribute("requestName2","requestValue23");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Login</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">
</head>
<body>
${tip}<br>
<form action="login?action=service" method="post">
名称:<input type="text" name="user"><br>
密码:<input type="text" name="pass"><br/>
<input type="submit" value="登录"><input type="reset" value="重置">
</form>
</body>
</html>
二、使用HttpSessionListener统计用户在线人数
1、实现HttpSessionListener监听器
package listener;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineListener implements HttpSessionListener{
//当用户与服务器开始session时触发该方法
public void sessionCreated(HttpSessionEvent se) {
// TODO Auto-generated method stub
HttpSession session=se.getSession();
ServletContext application=session.getServletContext();
//获取sessionId
String sessionId=session.getId();
session.setMaxInactiveInterval(15);
//如果是新连接
if(session.isNew()){
Map<String,String> online=(Map<String, String>) application.getAttribute("online");
String user=(String) session.getAttribute("user");
user=(user==null?"游客":user);
if(online==null){
online=new HashMap<String, String>();
}
online.put(sessionId, user);
application.setAttribute("online", online);
}
}
//当用户与服务器断开session时触发该方法
public void sessionDestroyed(HttpSessionEvent se) {
// TODO Auto-generated method stub
HttpSession session=se.getSession();
ServletContext application=session.getServletContext();
String sessionId=session.getId();
System.out.println("sessionId:"+sessionId);
HashMap<String,String> online=(HashMap<String, String>) application.getAttribute("online");
if(online!=null){
online.remove(sessionId);
}
application.setAttribute("online",online);
}
}
2、登录控制器
package Login;
import java.io.IOException;
import java.util.HashMap;
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 javax.servlet.http.HttpSessionEvent;
public class LoginServlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String user=request.getParameter("user");
String pass=request.getParameter("pass");
HttpSession session=request.getSession();
//session.setMaxInactiveInterval(3);
session.setAttribute("user", user);
session.setAttribute("pass", pass);
ServletContext application=session.getServletContext();
HashMap<String,String> dataMap=(HashMap<String, String>) application.getAttribute("online");
if(dataMap==null){
dataMap=new HashMap<String, String>();
}
dataMap.put(session.getId(),user);
application.setAttribute("online", dataMap);
try {
//转发
//request.getRequestDispatcher("/shop.jsp").forward(request, response);
//重定向
System.out.println("user1:"+session.getAttribute("user"));
response.sendRedirect("/webPrj/shop.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、登录界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
application.setAttribute("addAttrName","addAttrValue");
application.removeAttribute("addAttrName");
application.setAttribute("newAttrName","newAttrValue");
application.setAttribute("newAttrName","1234567");
request.setAttribute("requestName1","requestValue1");
request.removeAttribute("requestName1");
request.setAttribute("requestName2","requestValue2");
request.setAttribute("requestName2","requestValue23");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Login</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">
</head>
<body>
${tip}<br>
<form action="login?action=service" method="post">
名称:<input type="text" name="user"><br>
密码:<input type="text" name="pass"><br/>
<input type="submit" value="登录"><input type="reset" value="重置">
</form>
</body>
</html>
4、在线用户显示
<%@ 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>My JSP 'shop.jsp' starting 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>
<form action="buy.jsp">
书籍:<input type="checkbox" name="item" value="book">
电脑:<input type="checkbox" name="item" value="computer">
汽车:<input type="checkbox" name="item" value="car">
<input type="submit" value="提交">
</form>
<h1>在线用户:
