JavaWeb監聽器簡單介紹及一個登陸監聽案例
版權宣告:本文為博主原創文章,未經博主允許不得轉載。https://blog.csdn.net/qq_38720976/article/details/84474950
監聽器基本概念
JavaWeb中的監聽器是Servlet規範中定義的一種特殊類,它用於監聽web應用程式中的ServletContext, HttpSession和 ServletRequest等域物件的建立與銷燬事件,以及監聽這些域物件中的屬性發生修改的事件。
監聽器的分類
在Servlet規範中定義了多種型別的監聽器,它們用於監聽的事件源分別為ServletContext,HttpSession和ServletRequest
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>
效果展示: