Listener监视器
- Listener用于监听JavaWeb程序中的事件。
- 例如:ServletContext、HttpSession、ServletRequest的创建、修改和删除。
- 监听器的类型分为
① 生命周期
② 数据绑定
使用实例:
显示页面:
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>欢迎 <span style="color:blue">${sessionScope.user.username }</span> 登录,当前在线人数<span style="color:red">${applicationScope.count }</span>人 <a href="${pageContext.request.contextPath }/LogoutServlet">注销</a></h1>
<c:if test="${empty requestScope.users }">
<h1>没有任何用户!</h1>
</c:if>
<c:if test="${not empty requestScope.users }">
<h1>用户列表</h1>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th colspan="2">Operate</th>
</tr>
<c:forEach items="${requestScope.users }" var="user">
<tr>
<td>${user.id }</td>
<td>${user.username }</td>
<td>${user.password }</td>
<td>${user.email }</td>
<td><a href="#">Edit</a></td>
<td><a href="#">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</center>
</body>
</html>
LoginServlet登录Servlet:
/**
* 处理用户登录的Servlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//创建UserDao对象
UserDao userDao = new UserDaoImpl();
//调用UserDao中UserDao中验证用户名和密码方法
User user = userDao.checkUsernameAndPassword(username, password);
if(user != null) {
//用户名和密码正确,重定向登录成功页面
//response.sendRedirect(request.getContextPath()+"/page/login_success.jsp");
HttpSession session = request.getSession();
//将用户信息保存到session域中
session.setAttribute("user", user);
//重定向到查询所有用户的请求
response.sendRedirect(request.getContextPath()+"/GetUsersServlet");
}else {
//用户名或密码不正确,设置一个错误信息并放到request域中
request.setAttribute("msg", "用户名或密码不正确!");
//转发到等录页面
//获取转发器
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/page/login.jsp");
//进行请求的转发
requestDispatcher.forward(request, response);
}
}
LogoutServlet注销的Servlet:
/**
* 注销的Servlet
*/
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取Session对象
HttpSession session = request.getSession();
//使Session对象失效
session.invalidate();
//重定向到首页
response.sendRedirect(request.getContextPath() +"/index.jsp");
}
用户实体:
public class User implements Serializable, HttpSessionBindingListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private String password;
private String email;
public User(Integer id, String username, String password, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]";
}
//向Session域中添加User对象是调用
@Override
public void valueBound(HttpSessionBindingEvent event) {
//获取Session对象
HttpSession session = event.getSession();
//获取ServletContext对象
ServletContext application = session.getServletContext();
//从application域中获取当前在线人数
Integer count = (Integer) application.getAttribute("count");
if(count == null ) {
//证明之前还没有人上线,此时需要向application域中设置在线人数为1
application.setAttribute("count", 1);
}else {
//证明之前已经有人在线,此时需要将之前的在线人数加1并再次放到application域中
application.setAttribute("count", count+1);
}
}
//User对象从Session域中移除时调用
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
HttpSessionBindingListener.super.valueUnbound(event);
//获取Session对象
HttpSession session = event.getSession();
//获取ServletContext对象
ServletContext application = session.getServletContext();
//从application域中获取当前在线人数
Integer count = (Integer) application.getAttribute("count");
application.setAttribute("count", count-1);
}
}
说明:
- 当用户登录时,User对象被创建,而将User对象加入到Session域中时valueBound方法被调用,使在线人数增加,从而可以统计当前的在线人数。
- 当用户退出时,User对象从Session中被移除,valueUnbound方法被调用,使在线人数减少,从而可以统计当前准确的在线人数。