Java Web基础入门第八十三讲 Listener(监听器)——监听器在开发中的应用(二)

需求:显示登陆用户列表,并实现踢人功能。

为了实现这个需求,我们大可不必搞得那么复杂,设计成用户一旦登录了,就让其跳转到网站首页。下面开始写代码实现我们这个需求。
首先,在Eclipse中新建一个动态Web项目,比如day21_kick。接着,在cn.liayun.domain包下创建一个JavaBean——User.java。

package cn.liayun.domain;

public class User {
	private String username;
	private String password;
	
	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;
	}
}

紧接着,在根目录下新建一个用户登录页面——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 }/LoginServlet" method="post">
		用户名:<input type="text" name="username" /><br/>
		密码:<input type="password" name=""password"" /><br/>
		<input type="submit" value="登录" />
	</form>
</body>
</html>

接下来,在cn.liayun.web.servlet包下创建一个LoginServlet,用来处理用户的登录请求。

package cn.liayun.web.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;

import cn.liayun.domain.User;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		
		request.getSession().setAttribute("user", user);
		response.sendRedirect("/day21_kick/index.jsp");
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

接着,在根目录下新建一个网站首页页面——index.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>
	欢迎您,${user.username }
</body>
</html>

接下来在cn.liayun.web.listener包中创建一个监听器——UserListener.java,其实现了HttpSessionAttributeListener接口。

package cn.liayun.web.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import cn.liayun.domain.User;

public class UserListener implements HttpSessionAttributeListener {

	@Override
	public void attributeAdded(HttpSessionBindingEvent se) {
		//Map<String, HttpSession> map = new HashMap<String, HttpSession>();
		
		Map<String, HttpSession> map = (Map<String, HttpSession>) se.getSession().getServletContext().getAttribute("map");
		if (map == null) {
			map = new HashMap<String, HttpSession>();
			se.getSession().getServletContext().setAttribute("map", map);
		}
		
		
		Object object = se.getValue();
		if (object instanceof User) {
			User user = (User) object;
			//ServletContext域里面的Map集合的key就是用户的姓名,值就是用户的session
			map.put(user.getUsername(), se.getSession());
		}
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent se) {
		// TODO Auto-generated method stub

	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent se) {
		// TODO Auto-generated method stub

	}

}

编写完以上监听器之后,并在web.xml文件中注册该监听器。

<listener>
	<listener-class>cn.liayun.web.listener.UserListener</listener-class>
</listener>

再接着,在根目录下新建一个后台用户管理页面——listuser.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>用户展示列表</title>
</head>
<body>
	所有登录用户为:<br/>
	<c:forEach var="entry" items="${applicationScope.map }">
		<c:url var="url" value="/KickServlet">
			<c:param name="username" value="${entry.key }"></c:param>
		</c:url>
		
		${entry.key }&nbsp;&nbsp;&nbsp;&nbsp;<a href="${url }">踢死你</a><br/>
	</c:forEach>
</body>
</html>

温馨提示:由于listuser.jsp页面需要用到JSTL标签,所以需要导入jstl-1.2.jar包。
最后,在cn.liayun.web.servlet包中创建一个KickServlet,用来处理管理员踢人的请求。

package cn.liayun.web.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/KickServlet")
public class KickServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
//		username = new String(username.getBytes("UTF-8"), "UTF-8");
		
		Map<String, HttpSession> map = (Map<String, HttpSession>) this.getServletContext().getAttribute("map");
		HttpSession session = map.get(username);
		if (session != null) {
			session.invalidate();
			map.remove(username);
		}
		
		response.sendRedirect("/day21_kick/listuser.jsp");
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

至此,显示登陆用户列表,并实现踢人功能的这个需求我们就已经实现了,那我们快来测试一下吧!
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李阿昀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值