码迷,mamicode.com
首页 > 其他好文 > 详细

监听器模拟用户在线

时间:2017-09-20 14:55:39      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:nts   exti   结束   ida   body   event   new   ted   UI   

  由于会话管理机制的原因,用户需在不同的浏览器上进行登录。 期间可以访问onlineservlet查询在线用户列表。

   设置监听器:

 1 package com.listener;
 2 
 3 import java.util.LinkedList;
 4 import java.util.List;
 5 
 6 import javax.faces.application.Application;
 7 import javax.servlet.ServletContext;
 8 import javax.servlet.ServletContextEvent;
 9 import javax.servlet.ServletContextListener;
10 import javax.servlet.annotation.WebListener;
11 import javax.servlet.http.HttpSessionAttributeListener;
12 import javax.servlet.http.HttpSessionBindingEvent;
13 import javax.servlet.http.HttpSessionEvent;
14 import javax.servlet.http.HttpSessionListener;
15 @WebListener
16 public class OnlineListener implements HttpSessionListener,
17         HttpSessionAttributeListener, ServletContextListener {
18      private  ServletContext application;
19     @Override
20     public void contextInitialized(ServletContextEvent arg0) {
21         //上下文启动时创建list放进application用来存放在线用户列表
22         List<String> list = new LinkedList<>();
23          application  = arg0.getServletContext();
24          application.setAttribute("online",list);
25     }
26 
27     @Override
28     public void attributeAdded(HttpSessionBindingEvent arg0) {
29         //session属性添加时调用   将登陆的用户名存进list
30         String name = (String) arg0.getSession().getAttribute("name");
31         List<String> list =(List<String>)application.getAttribute("online");
32         list.add(name);
33         application.setAttribute("online",list);
34     }
35     @Override
36     public void sessionDestroyed(HttpSessionEvent arg0) {
37         //session 结束时调用 将下线的用户从list中去除
38         System.out.println("session destroyed");
39         List<String> list = (List<String>)application.getAttribute("online");
40         String name =(String) arg0.getSession().getAttribute("name");
41         list.remove(name);
42         application.setAttribute("online",list);
43     }
44         @Override
45     public void sessionCreated(HttpSessionEvent arg0) 
46         {}
47         @Override
48     public void attributeRemoved(HttpSessionBindingEvent arg0) {}
49         @Override
50     public void attributeReplaced(HttpSessionBindingEvent arg0) {}
51         @Override
52     public void contextDestroyed(ServletContextEvent arg0) {}
53 }

登录serlet:

 1 package com.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.jms.Session;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 @WebServlet(name="loginservlet",urlPatterns="/loginservlet")
13 /**
14  * 用户的登陆
15  * @author Administrator
16  *
17  */
18 public class LoginServlet extends HttpServlet {
19 
20     @Override
21     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
22             throws ServletException, IOException {
23         doPost(req, resp);
24     }
25 
26     @Override
27     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
28             throws ServletException, IOException {
29             String name = req.getParameter("name");
30             HttpSession session =  req.getSession();
31             if(null!=name){
32             session.setAttribute("name",name);
33             resp.sendRedirect("main.jsp");
34             }
35     }
36     
37 
38 }

下线servlet:

 1 package com.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.List;
 6 
 7 import javax.faces.application.Application;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 @WebServlet(name="logoutservlet",urlPatterns="/logoutservlet")
14 /**
15  * 用户的下线
16  * @author Administrator
17  *
18  */
19 public class LogoutServlet extends HttpServlet {
20 
21     @Override
22     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
23             throws ServletException, IOException {
24         doPost(req, resp);
25     }
26 
27     @Override
28     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
29             throws ServletException, IOException {
30         req.setCharacterEncoding("utf8");
31         resp.setCharacterEncoding("utf8");
32         resp.setContentType("text/html;charset=UTF-8");
33         req.getSession().invalidate();//会话销毁
34         PrintWriter out = resp.getWriter();
35         
36         out.println("在线用户:");
37         List<String> list = (List)getServletContext().getAttribute("online");
38         for (String s : list) {
39             out.println(s);
40         }
41     }
42 
43 }

在线用户查询servlet:

 1 package com.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.List;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 @WebServlet(name="onlineservlet",urlPatterns="/onlineservlet")
13 /**
14  * 通过访问可以查询在线用户名单
15  * @author Administrator
16  *
17  */
18 public class OnlineServlet extends HttpServlet {
19 
20     @Override
21     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
22             throws ServletException, IOException {
23         doPost(req, resp);
24     }
25 
26     @Override
27     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
28             throws ServletException, IOException {
29         resp.setContentType("text/html;charset=UTF-8");
30         PrintWriter out = resp.getWriter();
31         out.println("在线用户:");
32         List<String> list = (List)getServletContext().getAttribute("online");
33         for (String s : list) {
34             out.println(s);
35         }
36     }
37 
38 }

登录界面jsp:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP ‘index.jsp‘ starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24   <form action="loginservlet" method="postggi">
25   <center>
26       name:<input type="text" name="name"><br>
27       <input type="submit" value="load..">
28       </center>
29   </form>
30   </body>
31 </html>

登录后主界面:

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>main</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24 
25   <body>
26           <center>
27           <form action="logoutservlet" method="post">
28                 <%
29                     String name = (String)session.getAttribute("name");
30                  %> 
31                  <%=name %>欢迎登录
32                  <br><input type="submit" value="注销">
33          </form>
34          </center>
35   </body>
36 </html>

 

监听器模拟用户在线

标签:nts   exti   结束   ida   body   event   new   ted   UI   

原文地址:http://www.cnblogs.com/the-wang/p/7560700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!