版權聲明:本文爲博主原創文章,未經博主允許不得轉載。https://blog.csdn.net/qq_38720976/article/details/84474950
監聽器基本概念
JavaWeb中的監聽器是Servlet規範中定義的一種特殊類,它用於監聽web應用程序中的ServletContext, HttpSession和 ServletRequest等域對象的創建與銷燬事件,以及監聽這些域對象中的屬性發生修改的事件。
監聽器的分類
在Servlet規範中定義了多種類型的監聽器,它們用於監聽的事件源分別爲ServletContext,HttpSession和ServletRequest這三個域對象Servlet規範針對這三個對象上的操作,又把多種類型的監聽器劃分爲三種類型:
1)ServletContext監聽
ServletContextListener:用於對Servlet整個上下文進行監聽(創建、銷燬)。
ServletContextAttributeListener:對Servlet上下文屬性的監聽(增刪改屬性)。
2)Session監聽
HttpSessionListener接口:對Session的整體狀態的監聽。
HttpSessionAttributeListener接口:對Session的屬性監聽。
3)Request監聽
ServletRequestListener:用於對Request請求進行監聽(創建、銷燬)。
ServletRequestAttributeListener:對Request的屬性的監聽(增刪改屬性)。
示例:用監聽器統計網站在線人數
原理:每當有一個訪問連接到服務器時,服務器就會創建一個session來管理會話。那麼我們就可以通過統計session的數量來獲得當前在線人數。 所以這裏用到的是HttpSessionBindingListener。
特別提醒:由於瀏覽器會保存一段時間session數據,所以我們最好多使用幾個瀏覽器測試在線人數效果。
一.監聽器
1:創建監聽器類HttpSessionBindTest
package com.iflytek.xxsystem.listener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
@WebListener
public class HttpSessionBindTest implements HttpSessionBindingListener {
private int count;//記錄session的數量
public int getCount() {
return count;
}
public HttpSessionBindTest() {
}
//監聽session的創建
public void valueBound(HttpSessionBindingEvent sce) {
//登錄
System.out.println("登錄 value Bound");
count++;
}
//監聽session的撤銷
public void valueUnbound(HttpSessionBindingEvent sce) {
//退出
System.out.println("退出 value Unbound");
count--;
}
}
2.上下文屬性監聽類ServletContextListenerTest
package com.iflytek.xxsystem.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ServletContextListenerTest implements ServletContextListener {
public ServletContextListenerTest() {
// TODO Auto-generated constructor stub
}
@Override
public void contextInitialized(ServletContextEvent sce) {
//設置application的count屬性
HttpSessionBindTest sessionbinder = new HttpSessionBindTest();
sce.getServletContext().setAttribute("counter",sessionbinder);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
二.servlet處理數據
1.登陸及監聽數據處理
package com.aiit.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class doBusinessServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user=request.getParameter("user");
String pwd = request.getParameter("pwd");
//設置session過期時間1分鐘
//request.getSession().setMaxInactiveInterval(10);
if(user.equals(pwd)) {
request.getSession().setAttribute("user", user);
//找到唯一的bindlisten對象
Object counter = request.getServletContext().getAttribute("counter");
//把listener設置成session
request.getSession().setAttribute("usercount", counter);
request.getRequestDispatcher("main.jsp").forward(request, response);
}else {
response.sendRedirect("Login.jsp");
}
}
}
2.退出銷燬對象處理
package com.aiit.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/TuichuServlet")
public class TuichuServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TuichuServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//銷燬session
request.getSession().invalidate();
response.sendRedirect("Login.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
三、前臺JSP
1.登陸界面
<%@ 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>
<h3>登錄到HR系統</h3>
<h1 style="color: red;"></h1>
<form action="LoginServlet" method="post">
<table>
<tr>
<td>員工編號:</td>
<td><input type="text" name="user" value="" /></td>
</tr>
<tr>
<td>系統密碼:</td>
<td><input type="password" name="pwd" value="" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" /></td>
</tr>
</table>
</form>
</body>
</html>
2.主界面
<%@ 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>
歡迎${user}登錄 <br>
<font color="red">在線人數${applicationScope.counter.count}</font>
<br>
<a href="TuichuServlet">退出登錄</a>
</body>
</html>
效果展示: