事件源、事件、監(jiān)聽(tīng)器
- 事件源:發(fā)生事件的對(duì)象
- 事件:事件封裝了事件源,方便監(jiān)聽(tīng)器的某個(gè)方法獲取到事件源對(duì)象
- 監(jiān)聽(tīng)器:監(jiān)聽(tīng)事件源的行為和動(dòng)作
注:監(jiān)聽(tīng)器一般都是接口樊破,誰(shuí)用誰(shuí)實(shí)現(xiàn)。
Servlet規(guī)范中提供的8個(gè)監(jiān)聽(tīng)器
監(jiān)聽(tīng)ServletContext唆铐、HttpSession哲戚、ServletRequest對(duì)象的初始化和銷(xiāo)毀的監(jiān)聽(tīng)器
- ServletContextListener
- void contextInitialized(ServletContextEvent sce) :ServletContext初始化
- void contextDestroyed(ServletContextEvent sce) :ServletContext銷(xiāo)毀
使用場(chǎng)景:
應(yīng)用啟動(dòng)時(shí)執(zhí)行,可將整個(gè)應(yīng)用的初始化代碼寫(xiě)到該監(jiān)聽(tīng)器里
- HttpSessionListener
- void sessionCreated(HttpSessionEvent sce):第一次執(zhí)行request.getSession()執(zhí)行
- void sessionDestroyed(HttpSessionEvent sce):HttpSession銷(xiāo)毀時(shí)調(diào)用
注:關(guān)閉瀏覽器不會(huì)導(dǎo)致HttpSession銷(xiāo)毀或链;HttpSession銷(xiāo)毀時(shí)機(jī)(超時(shí)或者手動(dòng)調(diào)用invalidate()方法)
使用場(chǎng)景:
統(tǒng)計(jì)網(wǎng)站在線(xiàn)用戶(hù)量
- ServletRequestListener
- void requestInitialized(ServletRequestEvent arg0):用戶(hù)請(qǐng)求時(shí)執(zhí)行
- void requestDestroyed(ServletRequestEvent arg0):請(qǐng)求結(jié)束后執(zhí)行
使用場(chǎng)景:根據(jù)用戶(hù)訪(fǎng)問(wèn)的url資源id進(jìn)行頁(yè)面訪(fǎng)問(wèn)量統(tǒng)計(jì)
監(jiān)聽(tīng)ServletContext惫恼、HttpSession、ServletRequest域?qū)ο髷?shù)據(jù)變化(增刪改)的監(jiān)聽(tīng)器
- ServletContextAttributeListener
- void attributeAdded(ServletContextAttributeEvent arg0):當(dāng)向應(yīng)用范圍內(nèi)添加屬性時(shí)執(zhí)行
- void attributeRemoved(ServletContextAttributeEvent arg0):當(dāng)從應(yīng)用范圍內(nèi)移除屬性時(shí)執(zhí)行
- void attributeReplaced(ServletContextAttributeEvent arg0):當(dāng)應(yīng)用范圍內(nèi)當(dāng)屬性值替換時(shí)執(zhí)行
- HttpSessionAttributeListener
- void attributeAdded(HttpSessionBindingEvent arg0):當(dāng)向會(huì)話(huà)范圍內(nèi)添加屬性時(shí)執(zhí)行
- void attributeRemoved(HttpSessionBindingEvent arg0):當(dāng)從會(huì)話(huà)范圍內(nèi)移除屬性時(shí)執(zhí)行
- void attributeReplaced(HttpSessionBindingEvent arg0):當(dāng)會(huì)話(huà)范圍內(nèi)當(dāng)屬性值替換時(shí)執(zhí)行
使用場(chǎng)景:統(tǒng)計(jì)在線(xiàn)用戶(hù)澳盐,踢人
- ServletRequestAttributeListener
- void attributeAdded(ServletRequestAttributeEvent arg0):當(dāng)向請(qǐng)求范圍內(nèi)添加屬性時(shí)執(zhí)行
- void attributeRemoved(ServletRequestAttributeEvent arg0):當(dāng)從請(qǐng)求范圍內(nèi)移除屬性時(shí)執(zhí)行
- void attributeReplaced(ServletRequestAttributeEvent arg0):當(dāng)請(qǐng)求范圍內(nèi)當(dāng)屬性值替換時(shí)執(zhí)行
感知型監(jiān)聽(tīng)器(無(wú)需注冊(cè)監(jiān)聽(tīng)器)
- HttpSessionBindingListener:監(jiān)聽(tīng)自身是否被添加到量session中去
- void valueBound(HttpSessionBindingEvent arg0):綁定時(shí)執(zhí)行
- void valueUnbound(HttpSessionBindingEvent arg0):解綁時(shí)執(zhí)行
- HttpSessionActivationListener:監(jiān)聽(tīng)自身合適被鈍化和激活
- void sessionWillPassivate(HttpSessionEvent arg0):鈍化時(shí)執(zhí)行
- void sessionDidActivate(HttpSessionEvent arg0):被激活時(shí)執(zhí)行
HttpSession的鈍化和激活
當(dāng)用戶(hù)第一次調(diào)用request.getSession()時(shí)祈纯,則會(huì)創(chuàng)建session對(duì)象,session對(duì)象會(huì)存儲(chǔ)在緩存當(dāng)中叼耙。
- session銷(xiāo)毀:只有在session超時(shí)和手動(dòng)調(diào)用invalidata()方法時(shí)腕窥,session對(duì)象才會(huì)銷(xiāo)毀。
- session鈍化:將session對(duì)象持久化到本地硬盤(pán)上(tomcat/work/catalina/localhost/應(yīng)用名/下的*.ser文件)
1筛婉、服務(wù)器內(nèi)存告急
2簇爆、HttpSession對(duì)象長(zhǎng)時(shí)間沒(méi)有使用
3、服務(wù)器正常停止或應(yīng)用被卸載
注:在編寫(xiě)javabean時(shí)爽撒,最好實(shí)現(xiàn)java.io.Serializable接口入蛆,若沒(méi)有實(shí)現(xiàn)該接口,則會(huì)導(dǎo)致session的激活會(huì)失敗
在線(xiàn)用戶(hù)統(tǒng)計(jì)及踢人案例
流程分析:
1. 創(chuàng)建HttpSessionAttributeListener
2. 當(dāng)用戶(hù)登錄成功后硕勿,則在session中設(shè)置當(dāng)前用戶(hù)信息
3. 在HttpSessionAttributeListener的attributeAdded方法中判斷并創(chuàng)建Map<String,HttpSession>是否有當(dāng)前用戶(hù)信息哨毁,沒(méi)有則添加,并將該map集合設(shè)置到全局屬性中
4. 展示頁(yè)面中通過(guò)獲取ServletContext中的attribute的map集合進(jìn)行遍歷展示在線(xiàn)用戶(hù)信息
5. 踢下線(xiàn)處理則將目標(biāo)用戶(hù)的username作為參數(shù)傳遞給Servlet源武,在踢下線(xiàn)Servlet中獲取全局map扼褪,刪除對(duì)應(yīng)的session中的目標(biāo)用戶(hù)信息想幻,再?gòu)膍ap中移除目標(biāo)元素
示例代碼:
index.jsp
<%@ 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>Insert title here</title>
</head>
<body>
<c:if test="${sessionScope.user == null }">
<c:redirect url="/login.jsp"></c:redirect>
</c:if>
歡迎您,${sessionScope.user.username}
<c:if test="${sessionScope.user.username eq 'admin' }">
<a href="${ pageContext.request.contextPath }/online.jsp">查看在線(xiàn)用戶(hù)</a>
</c:if>
</body>
</html>
login.jsp
<%@ 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>登錄</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/servlet/LoginServlet"
method="post">
<table align="center" border="1" cellpadding="4px">
<tr>
<td align="right">用戶(hù)名</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td align="right">密碼</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登錄" /></td>
</tr>
</table>
</form>
</body>
</html>
LoginServlet.java
package com.gaoshiyi.web.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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;
import org.apache.commons.beanutils.BeanUtils;
import com.gaoshiyi.web.domain.User;
import com.gaoshiyi.web.service.BusinessSevice;
import com.gaoshiyi.web.service.impl.BusinessServiceImpl;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;chartset=utf-8");
Map<String, String[]> map = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user, map);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BusinessSevice sevice = new BusinessServiceImpl();
User exitUser = sevice.login(user);
if (exitUser == null) {
response.getWriter().write("用戶(hù)或密碼錯(cuò)誤话浇,請(qǐng)重新登錄脏毯!");
response.setHeader("Refresh", "1;url=" + request.getContextPath() + "/login.jsp");
return;
}
//進(jìn)行session的存儲(chǔ)
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
LoginInfoListener.java
package com.gaoshiyi.web.listener;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import com.gaoshiyi.web.domain.User;
/**
* 用戶(hù)登錄信息監(jiān)聽(tīng)器
*
*/
@WebListener
public class LoginInfoListener implements HttpSessionAttributeListener {
public LoginInfoListener() {
}
public void attributeAdded(HttpSessionBindingEvent arg0) {
HttpSession session = arg0.getSession();
ServletContext servletContext = session.getServletContext();
Object obj = session.getAttribute("user");
if (obj != null && obj instanceof User) {
//建立Map集合存儲(chǔ)用戶(hù)信息
Map<String, HttpSession> usersMap = (Map<String, HttpSession>) servletContext.getAttribute("users");
if (usersMap == null) {
//同步處理否則會(huì)創(chuàng)建多個(gè)Map
usersMap = Collections.synchronizedMap(new HashMap<>());
servletContext.setAttribute("users", usersMap);
}
User user = (User) obj;
if (!usersMap.containsKey(user.getUsername())) {
//若集合中沒(méi)有該用戶(hù)則添加
usersMap.put(user.getUsername(), session);
System.out.println("新用戶(hù):" + user.getUsername() + "登錄...");
}
System.out.println(usersMap);
}
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
}
}
online.jsp
<%@ 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>在線(xiàn)用戶(hù)</title>
</head>
<body>
<c:if test="${sessionScope.user.username != 'admin' }">
<c:redirect url="/index.jsp"></c:redirect>
</c:if>
<h3 align="center">當(dāng)前在線(xiàn)用戶(hù)列表</h3>
<table align="center" border="1" width="50%" height="auto">
<tr>
<th>用戶(hù)名</th>
<th>操作</th>
</tr>
<c:forEach items="${applicationScope.users }" var="um">
<tr align="center">
<c:url var="url" value="servlet/KickServlet">
<c:param name="username" value="${ um.key}"></c:param>
</c:url>
<td>${ um.key }</td>
<td><a href="${ url }">踢下線(xiàn)</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
KickServlet.java
package com.gaoshiyi.web.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;
/**
* 踢下線(xiàn)處理
* @author gaopengfei
*
*/
public class KickServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public KickServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
// username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("被踢用戶(hù):" + username);
//獲取全局用戶(hù)登錄信息集合
Map<String, HttpSession> usersMap = (Map<String, HttpSession>) getServletContext().getAttribute("users");
if (usersMap.containsKey(username)) {
//先從session中移除
usersMap.get(username).removeAttribute("user");
//再?gòu)膍ap中移除元素
usersMap.remove(username);
System.out.println("用戶(hù):" + username + "已被踢下線(xiàn)...");
}
response.getWriter().write("操作成功^_^");
response.setHeader("Refresh", "1;url=" + request.getContextPath()+"/online.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
演示: