1. 程式人生 > >監聽器模擬用戶在線

監聽器模擬用戶在線

servlet rip exc protect ida pos param 結束 att

  由於會話管理機制的原因,用戶需在不同的瀏覽器上進行登錄。 期間可以訪問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>

監聽器模擬用戶在線