首先利用Context的监听器去
创建一个Map,key为用户名,value为与当前用户绑定的Session对象
然后去建立一个login.jsp,里面写入用户名
利用servlet将创建的用户与session值绑定起来
里面就会触发HttpSessionBindingListener的监听器 监听器会将用户名和其session存入map然后存入context域中
最后建立一个admin.jsp,里面显示map中的用户和其session,添加一个按钮剔除(
servlet 将通用户的session值失效并从map中删除)
返回显示admin.jsp页面中
package com._13_kikcUser.listeners;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
// 创建一个Map,key为用户名,value为与当前用户绑定的Session对象
Map<String, HttpSession> map = new HashMap<>();
// 获取到全局域
ServletContext sc = sce.getServletContext();
// 将map放入到全局域
sc.setAttribute("map", map);
System.out.println(map);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
package com._13_kikcUser.listeners;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.Map;
/**
* ClassName:User
* Package:com._13_kikcUser.po
* Description:
*
* @Date:2019/4/22 17:24
* @Author:
*/
public class User implements HttpSessionBindingListener{
private String name;
private Integer age;
public User(){
super();
}
public User(String name, Integer age){
super();
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public void valueBound(HttpSessionBindingEvent event) {
// 获取session
HttpSession session = event.getSession();
// 获取全局域
ServletContext sc = session.getServletContext();
// 从全局域中获取map
Map<String, HttpSession> map = (Map<String, HttpSession>) sc.getAttribute("map");
// 将当前用户名与Session放到map中
map.put(name, session);
// 将map写回全局域
sc.setAttribute("map", map);
System.out.println(map);
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
}
}
package com._13_kikcUser.listeners;
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;
import java.io.IOException;
@WebServlet(value = "/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
Integer age = Integer.valueOf(ageStr);
// 创建User对象
User user = new User(name, age);
// 获取当前请求对应的session
HttpSession session = request.getSession();
// 将User与session绑定起来
session.setAttribute("user", user);
// 跳转到欢迎页面
response.sendRedirect(request.getContextPath() + "/welcome.jsp");
}
}
package com._13_kikcUser.listeners;
import javax.servlet.ServletContext;
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;
import java.io.IOException;
import java.util.Map;
@WebServlet(value = "/kikcServlet")
public class KikcServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取全局域
ServletContext sc = request.getSession().getServletContext();
// 从全局域中获取map
Map<String, HttpSession> map = (Map<String, HttpSession>) sc.getAttribute("map");
// 获取要剔除的用户名
String name = request.getParameter("name");
System.out.println(name);
// 利用用户名在map找到对应的session
HttpSession session = map.get(name);
System.out.println(session);
// 使session失效
session.invalidate();
// 将该用户对应的Entry从map中删除
map.remove(name);
// 返回admin页面
request.getRequestDispatcher("/admin.jsp").forward(request, response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java"
import="com._13_kikcUser.listeners.User"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/loginServlet" method="post">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1">
<caption>已登陆用户</caption>
<tr>
<th>用户名</th>
<th>session</th>
<th>剔除</th>
</tr>
<c:forEach items="${map }" var="entry">
<tr>
<td>${entry.key }</td>
<td>${entry.value }</td>
<td><a href="${pageContext.request.contextPath }/kikcServlet?name=${entry.key }">剔除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>