• HttpsessionListener 实现在线人数统计


    最近在学servlet jsp,用的林信良先生的 jsp&servlet 这本书,在第五章有道在线人数统计的课后题完成,做一次记录.

    实际效果:

    一:用户类:

     1 package cc.openhome.controler;
     2 
     3 public class User {
     4     private String name;
     5     private String sessionId;
     6     private String ip;
     7     private String firstTime;
     8     private String userAgent;
     9     public User(){
    10 
    11     }
    12 
    13     public String getName() {
    14         return name;
    15     }
    16 
    17     public String getIp() {
    18         return ip;
    19     }
    20 
    21     public String getSessionId() {
    22         return sessionId;
    23     }
    24 
    25     public String getFirstTime() {
    26         return firstTime;
    27     }
    28 
    29     public String getUserAgent() {
    30         return userAgent;
    31     }
    32 
    33     public void setUserAgent(String userAgent) {
    34         this.userAgent = userAgent;
    35     }
    36 
    37     public void setName(String name) {
    38         this.name = name;
    39     }
    40 
    41     public void setFirstTime(String firstTime) {
    42         this.firstTime = firstTime;
    43     }
    44 
    45     public void setIp(String ip) {
    46         this.ip = ip;
    47     }
    48 
    49     public void setSessionId(String sessionId) {
    50         this.sessionId = sessionId;
    51     }
    52 
    53 }
    public class User

    二:登录页面:

     1 package cc.openhome.controler;
     2 
     3 import javax.servlet.ServletException;
     4 import javax.servlet.annotation.WebServlet;
     5 import javax.servlet.http.Cookie;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import java.io.IOException;
    10 import java.text.SimpleDateFormat;
    11 import java.util.*;
    12 
    13 @WebServlet("/login.do")
    14 public class Login extends HttpServlet {
    15 
    16    private ArrayList<User> ulist;
    17    private Map<String,String> users;
    18    public Login(){
    19        users = new HashMap<String,String>();
    20        users.put("liming","123456");
    21        users.put("小猪","123456");
    22        users.put("小林","123456");
    23    }
    24 
    25     @Override
    26     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    27 
    28         req.setCharacterEncoding("UTF-8");
    29         String uname = req.getParameter("user");
    30         String pssd = req.getParameter("pwd");
    31 
    32         String page = "login.html";
    33 
    34         if (users.containsKey(uname) && users.get(uname).equals(pssd)) {
    35             req.getSession().setAttribute("uname", uname);
    36             String sessionId = req.getSession().getId();
    37             ArrayList<User> ulist = (ArrayList<User>) getServletContext().getAttribute("ulist");
    38             if(ulist == null){
    39                 ulist = new ArrayList<User>();
    40                 getServletContext().setAttribute("ulist",ulist);
    41             }
    42             User u = new User();
    43             u.setSessionId(sessionId);
    44             u.setName(uname);
    45             u.setIp(req.getRemoteAddr());
    46             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
    47             u.setFirstTime(df.format(new Date()));
    48             u.setUserAgent(req.getHeader("user-agent"));
    49             ulist.add(u);
    50             page = "welcome.view";
    51         }
    52         resp.sendRedirect(page);
    53     }
    54 }

    三:统计在线人数:

     1 package cc.openhome.controler;
     2 
     4 import javax.servlet.ServletContext;
     5 import javax.servlet.annotation.WebListener;
     6 import javax.servlet.http.HttpSession;
     7 import javax.servlet.http.HttpSessionEvent;
     8 import javax.servlet.http.HttpSessionListener;
     9 import java.util.List;
    10 
    11 @WebListener
    12 public class OnlineUserCounter implements HttpSessionListener {
    13     private static int counter;
    14 
    15     public static int getCounter(){
    16         return counter;
    17     }
    18 
    19     @Override
    20     public void sessionCreated(HttpSessionEvent se) {
    21         System.out.println("新建session: "+se.getSession().getId());
    22 
    23     }
    24 
    25     @Override
    26     public void sessionDestroyed(HttpSessionEvent se) {
    27         HttpSession session = se.getSession();
    28         ServletContext application = session.getServletContext();
    29         String username = (String) session.getAttribute("username");
    30 
    31         List ulist = (List)
    32                 application.getAttribute("ulist");
    33         ulist.remove(username);
    34         System.out.println(username+"已经退出!");
    35 
    36     }
    37 }

    四:登录成功页面显示

     1 package cc.openhome.view;
     2 
     3 import cc.openhome.controler.User;
     4 import cc.openhome.controler.OnlineUserCounter;
     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 javax.servlet.http.HttpSession;
    11 import java.io.IOException;
    12 import java.io.PrintWriter;
    13 import java.util.ArrayList;
    14 import java.util.Iterator;
    15 import java.util.List;
    16 
    17 @WebServlet("/welcome.view")
    18 public class Welcome extends HttpServlet {
    19     @Override
    20     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    21 
    22         resp.setContentType("text/html;charset=UTF-8");
    23         PrintWriter out = resp.getWriter();
    24         HttpSession session = req.getSession(false);
    25         out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01'" +
    26                 "Transitional//EN");
    27         out.println("<html>");
    28         out.println("<head>");
    29         out.println("<title>欢迎</title>");
    30         out.println("</head>");
    31         out.println("<body>");
    32 
    33         ArrayList<User> ul = (ArrayList <User>) getServletContext().getAttribute("ulist");
    34         out.println("<h1> 目前在线人数 "+ ul.size() +"人</h1>");
    35             Iterator<User> it = ul.iterator();
    36             while (it.hasNext()){
    37                 User u = it.next();
    38                 out.println("<h4>id: "+ u.getSessionId() +" </h4>");
    39                 out.println("<h4>用户: "+ u.getName()+" </h4>");
    40                 out.println("<h4>ip: "+ u.getIp() +" </h4>");
    41                 out.println("<h4>访问时间: "+ u.getFirstTime() +" </h4>");
    42                 out.println("<h4>浏览器信息: "+ u.getUserAgent() +" </h4>");
    43                 out.println("<br>");
    44             }
    45 
    46         out.println("</body>");
    47         out.println("</html>");
    48         out.close();
    49 
    50         }
    51 }

    五:登录的前端页面:

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>登录</title>
     6 </head>
     7 <body>
     8 <form action="login.do" method="post">
     9     用户名: <input type="text" name="user"><br>
    10     密码:  <input type="password" name="pwd"><br>
    11     提交: <input type= "submit">
    12 </form>
    13 </body>
    14 </html>
    longin.html

    六:实际效果:

    七:总结

    1.httpsessionListener 工作机制

    2,ArrayList 使用

    八:不足之处:

    1页面展示用的是:在servlet中用的html,还没有学习jsp.

    2.已知问题:同一个用户同时登录,重新显示为一新用户,后面再改

    参考资料:

    http://www.cnblogs.com/shencheng/archive/2011/01/07/1930227.html

    https://blog.csdn.net/su20145104009/article/details/53217201

    本人菜鸟一枚,如有问题请大神不吝赐教!

  • 相关阅读:
    洛谷 P2234 [HNOI2002]营业额统计
    洛谷p3146&p3147
    洛谷 p1439 最长公共子序列
    搜索
    一步一步分析Caliburn.Micro(二:绑定执行方法Message现学现卖之自定命令)
    一步一步分析Caliburn.Micro(一:绑定执行方法Message)
    整理的C# 字符串类
    不用ADOX.CatalogClass创建Access数据库文件
    取远程网页数据 WebClient,HttpWebRequest
    C# LinQ 与 ADO.NET
  • 原文地址:https://www.cnblogs.com/changlili/p/10018930.html
Copyright © 2020-2023  润新知