Event monitoring: Number of logged-on users (including user names)

Source: Internet
Author: User

Event monitoring: Number of logged-on users (including user names)

(1) create a User class to encapsulate a User information. This class implements the valueBound () method in the HttpSessionBindingListener interface.
And valueUnbound () method

1 package entity; 2 3 import javax. servlet. annotation. webListener; 4 import javax. servlet. http. *; 5 6 @ WebListener () 7 public class User implements HttpSessionBindingListener {8 9 private String username; 10 private String password; 11 private String id; 12 13 public String getUsername () {14 return username; 15} 16 17 public void setUsername (String username) {18 this. username = username; 19} 20 21 public String getPassword () {22 return password; 23} 24 25 public void setPassword (String password) {26 this. password = password; 27} 28 29 public String getId () {30 return id; 31} 32 33 public void setId (String id) {34 this. id = id; 35} 36 37 @ Override38 public void valueBound (HttpSessionBindingEvent event) {39 // Save the user to the list of 40 onlineusers. getInstance (). addUser (this); 41} 42 43 @ Override44 public void valueUnbound (HttpSessionBindingEvent event) {45 OnlineUser. getInstance (). removeUser (this); 46} 47}

(2) create an OnlineUser class to store and obtain the list of users. This list should be the same for all pages, so it is a singleton mode.

1 package entity; 2 3 import java. util. hashMap; 4 import java. util. map; 5 6 public class OnlineUser {7 private OnlineUser () {} 8 private static OnlineUser instance = new OnlineUser (); 9 public static OnlineUser getInstance () {10 return instance; 11} 12 private Map userMap = new HashMap (); 13 // Add the User to the list 14 public void addUser (user User) {15 userMap. put (user. getId (), user. getUsername (); 16} 17 // Remove User List 18 public void removeUser (user User) {19 userMap. remove (user. getId (); 20} 21 // return to user list 22 public Map getOnlineUser () {23 return userMap; 24} 25}

(3) Compile a login. jsp page. Enter the user's logon name and password to complete the user logon function.

1 <% @ page contentType = "text/html; charset = UTF-8 "language =" java "%> 2 

(4) create a LoginServlet class to process User login requests. If the User logs on successfully, the User information is encapsulated into the User and saved to the Session object.

1 package servlet; 2 3 import entity. onlineUser; 4 import entity. user; 5 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 java. io. IOException; 12 import java. util. map; 13 import java. util. UUID; 14 15 @ WebServlet (name = "LoginServlet", urlPatterns = "/LoginServlet") 16 public class LoginServlet extends HttpServlet {17 protected void doPost (HttpServletRequest request, HttpServletResponse response) 18 throws ServletException, IOException {19 request. setCharacterEncoding ("UTF-8"); 20 response. setContentType ("text/html; charset = UTF-8"); 21 String username = request. getParameter ("username"); 22 String password = request. g EtParameter ("password"); 23 if (username! = Null &&! Username. trim (). equals ("") {24 // login successful 25 User user = new User (); 26 user. setId (UUID. randomUUID (). toString (); 27 user. setUsername (username); 28 user. setPassword (password); 29 request. getSession (). setAttribute ("user", user); 30 Map users = OnlineUser. getInstance (). getOnlineUser (); 31 request. setAttribute ("users", users); 32 request. getRequestDispatcher ("/showuser. jsp "). forward (request, response); 33} else {34 request. setAttribute ("errorMsg", "incorrect user name or password"); 35 request. getRequestDispatcher ("/login. jsp "). forward (request, response); 36} 37} 38 39 protected void doGet (HttpServletRequest request, HttpServletResponse response) 40 throws ServletException, IOException {41 this. doPost (request, response); 42} 43}

(5) Compile a showuser. jsp page to display the logon information of all users and the current logon users.

1 <% @ page contentType = "text/html; charset = UTF-8 "language =" java "%> 2 <% @ taglib prefix =" c "uri =" http://java.sun.com/jsp/jstl/core "%> 3 

(6) Compile a LogoutServlet class to log out of user logon information. After logging out, the user will jump to the showuser. jsp page.

 1 package servlet; 2  3 import entity.OnlineUser; 4  5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 import java.io.IOException;11 import java.util.Map;12 13 @WebServlet(name = "LogoutServlet",urlPatterns = "/LogoutServlet")14 public class LogoutServlet extends HttpServlet {15     protected void doPost(HttpServletRequest request, HttpServletResponse response)16             throws ServletException, IOException {17         request.setCharacterEncoding ("utf-8");18         response.setContentType ("text/html;charset=utf-8");19         request.getSession ().removeAttribute ("user");20         Map users = OnlineUser.getInstance ().getOnlineUser ();21         request.setAttribute ("users",users);22         request.getRequestDispatcher ("/showuser.jsp").forward (request,response);23     }24     protected void doGet(HttpServletRequest request, HttpServletResponse response)25             throws ServletException, IOException {26         this.doPost (request,response);27     }28 }

(7) use different browser accesses to display the following results:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.