リスナー(オンライン人数をテストする)第1種HttpSessionBindingListener

26367 ワード


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;
    }
}