Today, I relearned the basic listener technology of java web, and used sessionListener to simply count the number of people online (one bug is that when the web page is closed, the session will not be closed and still exists, unless the server is closed). The code is as follows (the front-end web page code will not be 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 @Override 12 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.jsp 17 response.sendRedirect("index.jsp"); 18 } 19 20 @Override 21 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 @Override 14 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 //from session Remove from loginName parameter 16 response.getWriter().write("2222"); 17 HttpSession session=request.getSession(); 18 session.removeAttribute("username"); 19 //Redirect to login.jsp 20 response.sendRedirect("login.jsp"); 21 } 22 23 @Override 24 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, HttpSessionAttributeListener { 15 public OnlineListener() { 16 } 17 @Override 18 public void contextInitialized(ServletContextEvent sce) { 19 20 } 21 @Override 22 public void contextDestroyed(ServletContextEvent sce) { 23 24 } 25 @Override 26 public void sessionCreated(HttpSessionEvent se) { 27 System.out.println("Session created"); 28 } 29 @Override 30 public void sessionDestroyed(HttpSessionEvent se) { 31 System.out.println("Session destroyed"); 32 } 33 34 @Override 35 public void attributeAdded(HttpSessionBindingEvent sbe) { 36 //towards session Add a user to the scope 37 User.addUser(String.valueOf(sbe.getValue())); 38 System.out.println("session Add attribute as"+sbe.getName()+"The value is:"+sbe.getValue()); 39 } 40 @Override 41 public void attributeRemoved(HttpSessionBindingEvent sbe) { 42 //take session User removal in session 43 User.removeUser(String.valueOf(sbe.getValue())); 44 System.out.println(sbe.getValue()+"Property removed"); 45 } 46 @Override 47 public void attributeReplaced(HttpSessionBindingEvent sbe) { 48 //Get old property values 49 String oldValue=String.valueOf(sbe.getValue()); 50 //Get new property value 51 String newValue=String.valueOf(sbe.getSession().getAttribute(sbe.getName())); 52 //Remove old properties and add new ones 53 User.removeUser(oldValue); 54 User.addUser(newValue); 55 } 56 }
The results of running with two different browsers are as follows:
The project catalog is as follows:
(keep learning! Come on! Encourage yourself)