监听器(测试在线人数的)第一种 HttpSessionBindingListener

 

index.jsp:

复制代码
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/10/15
  Time: 11:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>HttpSessionBindingListener登录界面</title>
</head>
<body>

<form action="doLogin.jsp"method="post">
    <table align="center">
        <tr><td>
            账号姓名: <input type="text"name="userName"placeholder="请输入账号名">
            <input type="submit"value="登录">

        </td></tr>
    </table>


</form>
</body>
</html>
复制代码

 

doLogin.jsp:

复制代码
<%@ page import="pojo.User" %><%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/10/15
  Time: 11:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>HttpSessionBindingListener验证</title>
</head>
<body>

<%

    String userName = request.getParameter("userName");
    if(userName==null||userName.equals("")){
        response.sendRedirect("index.jsp");
    }else{
        //登录成功!
        User user = new User();
        user.setUserName(userName);
        session.setAttribute("user",user);
        response.sendRedirect("onLine.jsp");
    }
%>

</body>
</html>
复制代码

onLine.jsp:

复制代码
<%@ page import="pojo.User" %>
<%@ page import="constants.Constants" %><%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/10/15
  Time: 11:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>在线人数</title>
</head>
<body>


<%
    User user = null;
    if(session.getAttribute("user")==null){
        response.sendRedirect("index.jsp");
    }else{
        user = (User)session.getAttribute("user");


%>

欢迎您: <%=user.getUserName()%>
当前在线人数: <%=Constants.USER_COUNT%>
<a href="loginOut.jsp">注销</a>
<%
    }

%>
</body>
</html>
复制代码

 

loginOut.jsp:

复制代码
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/10/15
  Time: 12:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注销</title>
</head>
<body>

<%

session.invalidate();
response.sendRedirect("index.jsp");
%>

</body>
</html>
复制代码

javaBean:

Constants类:
package constants;

public class Constants {
    public static int USER_COUNT = 0;
}

User类:

复制代码
package pojo;

import constants.Constants;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class User implements HttpSessionBindingListener {
    private String userName;
    private String password;
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {

        Constants.USER_COUNT++;
    }

    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {

        Constants.USER_COUNT--;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
复制代码

 

本文作者:AxeBurner

本文链接:https://www.cnblogs.com/bichen-01/p/11676910.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   AxeBurner  阅读(186)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.