监听器--显示登陆用户列表,并实现踢人功能

 

用户:login.jsp à loginServlet à  index.jsp

管理员: listUser.jsp à kickUserServlet

监听器:SessionAttributeListener

Bean: User

 

cn.class.domain.User

public class User {

 private String username;
 private String password;
 
 
 
 public User(String username, String password) {
  super();
  this.username = username;
  this.password = password;
 }
 public User() {
  super();
  // TODO Auto-generated constructor stub
 }
 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

<body>
   <form action="${pageContext.request.contextPath}/servlet/LoginServlet" method="post">
   用户名:<input type="text" name="username"/><br/>
   密码:<input type="password" name="password"><br/>
   <input type="submit" value="登录">
   </form>
  </body>

 

cn.class3g.web.servlet

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request,response);
  
 }

 
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  String username=request.getParameter("username");
  String password=request.getParameter("password");
  
  User user=new User(username,password);
  
  request.getSession().setAttribute("user",user);
  
  //用重定向,不用再转发
  response.sendRedirect(request.getContextPath()+"/index.jsp");
 }

index.jsp 

 <body>
    欢迎您:${user.username }
  </body>

cn.class3g.web.listener

public class SessionAttributeListener implements HttpSessionAttributeListener {

 public void attributeAdded(HttpSessionBindingEvent arg0) {
  Object obj=arg0.getValue();
  if(obj instanceof User){
   HttpSession session=arg0.getSession();
   
   ServletContext context=session.getServletContext();
   Map map=(Map)session.getServletContext().getAttribute("map");
   
   if(map==null){//第一个用户登录,此时还没有创建Map集合
    map=new HashMap();
    context.setAttribute("map",map);
    
   }
   User user=(User)obj;
   
   //登录用户的用户名作为key,登录用户的session对象作为value
   map.put(user.getUsername(), session);
  }

 }

 public void attributeRemoved(HttpSessionBindingEvent arg0) {
  // TODO Auto-generated method stub

 }

 public void attributeReplaced(HttpSessionBindingEvent arg0) {
  // TODO Auto-generated method stub

 }

}

listUser.jsp

 <body>
    当前的登录用户有:<br/>
    <c:forEach var="me" items="${map}">
    ${me.key }
    <a href="${pageContext.request.contextPath}/servlet/KickUserServlet?username=${me.key}">
     踢死你!
    </a><br/><br/>
    </c:forEach>
  </body>

cn.class3g.web.servlet

public class KickUserServlet extends HttpServlet {


 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  String username=request.getParameter("username");
  Map map=(Map) this.getServletContext().getAttribute("map");
  
  HttpSession session=(HttpSession) map.get(username);
  if(session!=null){//开踢
   session.invalidate();
   map.remove(username);
  }
  
  request.getRequestDispatcher("/listUser.jsp").forward(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doGet(request,response);
 }

}

测试流程:

1. 模仿用户登录(多个)

2. 模仿管理员访问listUser.jsp ,踢人

3. 用户重新刷新index.jsp ,发现session已被销毁

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值