今天又重新学习了java web的基础 监听器技术,用sessionListener简单的实现了网页统计在线人数 (有一个bug就是当关闭网页的时候 session不会关闭依旧存在,除非关闭服务器),代码如下(前端网页代码就不显示了):
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 //重新定向到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 //从session中移除loginName参数 16 response.getWriter().write("2222"); 17 HttpSession session=request.getSession(); 18 session.removeAttribute("username"); 19 //重新定向到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("会话已创建"); 28 } 29 @Override 30 public void sessionDestroyed(HttpSessionEvent se) { 31 System.out.println("会话已销毁"); 32 } 33 34 @Override 35 public void attributeAdded(HttpSessionBindingEvent sbe) { 36 //向session范围中添加一个用户 37 User.addUser(String.valueOf(sbe.getValue())); 38 System.out.println("session增加属性为"+sbe.getName()+"值为:"+sbe.getValue()); 39 } 40 @Override 41 public void attributeRemoved(HttpSessionBindingEvent sbe) { 42 //将session会话中的用户移除 43 User.removeUser(String.valueOf(sbe.getValue())); 44 System.out.println(sbe.getValue()+"属性已被移除"); 45 } 46 @Override 47 public void attributeReplaced(HttpSessionBindingEvent sbe) { 48 //获取旧的属性值 49 String oldValue=String.valueOf(sbe.getValue()); 50 //获取新的属性值 51 String newValue=String.valueOf(sbe.getSession().getAttribute(sbe.getName())); 52 //移除旧的属性,添加新的属性 53 User.removeUser(oldValue); 54 User.addUser(newValue); 55 } 56 }
用两个不同的浏览器运行结果如下图所示:
项目工程目录如下图:
(坚持学习!努力加油!勉励自己)
原文地址:https://www.cnblogs.com/crabmany/p/8605826.html
时间: 2024-10-28 02:28:29