Java web uses the listener to easily calculate the number of people online, and the listener

Source: Internet
Author: User

Java web uses the listener to easily calculate the number of people online, and the listener

Today, I learned the basic listener Technology of java web again, and used sessionListener to calculate the number of online users on the web page. (One bug is that when the web page is closed, the session will not be closed and will still exist, unless the server is disabled), the Code is as follows (the front-end webpage code is not displayed ):

LoginServlet. java:

1 import javax. servlet. servletException; 2 import javax. servlet. annotation. webServlet; 3 import javax. servlet. http. httpServlet; 4 import javax. servlet. http. httpServletRequest; 5 import javax. servlet. http. httpServletResponse; 6 import javax. servlet. http. httpSession; 7 import java. io. IOException; 8 9 @ WebServlet (name = "LoginServlet", urlPatterns = "/login") 10 public class LoginServlet extends HttpServlet {11 @ Override12 protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {13 String username = request. getParameter ("username"); 14 HttpSession session = request. getSession (); 15 session. setAttribute ("username", username); 16 // redirect to index. jsp17 response. sendRedirect ("index. jsp "); 18} 19 20 @ Override21 protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {22 23} 24}

ExitServlet. java:

1 package servlet; 2 3 import javax. servlet. servletException; 4 import javax. servlet. annotation. webServlet; 5 import javax. servlet. http. httpServlet; 6 import javax. servlet. http. httpServletRequest; 7 import javax. servlet. http. httpServletResponse; 8 import javax. servlet. http. httpSession; 9 import java. io. IOException; 10 11 @ WebServlet (name = "ExitServlet", urlPatterns = "/exit") 12 public class ExitServlet extends HttpServlet {13 @ Override14 protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {15 // remove the loginName parameter 16 response from the session. getWriter (). write ("2222"); 17 HttpSession session = request. getSession (); 18 session. removeAttribute ("username"); 19 // redirect to login. jsp20 response. sendRedirect ("login. jsp "); 21} 22 23 @ Override24 protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {25 doPost (request, response); 26} 27}

OnlineListener. java:

1 package listener; 2 3 import bean. user; 4 5 import javax. servlet. servletContextEvent; 6 import javax. servlet. servletContextListener; 7 import javax. servlet. http. httpSessionAttributeListener; 8 import javax. servlet. http. httpSessionEvent; 9 import javax. servlet. http. httpSessionListener; 10 import javax. servlet. http. httpSessionBindingEvent; 11 12 @ javax. servlet. annotation. webListener () 13 public class OnlineListener implements ServletContextListener, 14 HttpSessionListener, listener {15 public OnlineListener () {16} 17 @ Override18 public void contextInitialized (ServletContextEvent sce) {19 20} 21 @ Override22 public void contextDestroyed (ServletContextEvent sce) {23 24} 25 @ Override26 public void sessionCreated (HttpSessionEvent se) {27 System. out. println ("session created"); 28} 29 @ Override30 public void sessionDestroyed (HttpSessionEvent se) {31 System. out. println ("session destroyed"); 32} 33 34 @ Override35 public void attributeAdded (HttpSessionBindingEvent sbe) {36 // Add a User 37 User to the session range. addUser (String. valueOf (sbe. getValue (); 38 System. out. println ("added the property of the session" + sbe. getName () + "value:" + sbe. getValue (); 39} 40 @ Override41 public void attributeRemoved (HttpSessionBindingEvent sbe) {42 // remove 43 users from the session. removeUser (String. valueOf (sbe. getValue (); 44 System. out. println (sbe. getValue () + "attribute removed"); 45} 46 @ Override47 public void attributeReplaced (HttpSessionBindingEvent sbe) {48 // get the old attribute value 49 String oldValue = String. valueOf (sbe. getValue (); 50 // get the new property value 51 String newValue = String. valueOf (sbe. getSession (). getAttribute (sbe. getName (); 52 // remove the old property and add the new property 53 User. removeUser (oldValue); 54 User. addUser (newValue); 55} 56}

Shows the running result in two different browsers:

The project directory is as follows:

(Keep learning! Come on! Encourage yourself)

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.