效果图:
登陆前输入用户名:
登陆后:
点击返回:
控制台输出:
代码:
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="showUser.jsp" method="post">
<input type="text" name="user">
<input type="submit" value="登录">
</form>
</body>
</html>
showUser.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
UserInfoList userInfoList = UserInfoList.getInstance();
UserInfoTrace userInfoTrace = new UserInfoTrace();
String user = request.getParameter("user");
userInfoTrace.setUser(user);
session.setAttribute("list", userInfoTrace);
userInfoList.addUserInfo(userInfoTrace.getUser());
session.setMaxInactiveInterval(10);
%>
<textare>
<%
Vector<String> vector = userInfoList.getList();
if(vector != null && vector.size()>0){
for(int i=0; i<vector.size(); i++){
out.print(vector.elementAt(i)+"登陆成功");
}
}
%>
</textare>
<a href="index.jsp">返回</a>
</body>
</html>
src目录下:
cn.test.UserInfoList.java
package cn.test;
import java.util.Vector;
public class UserInfoList {
private static UserInfoList user = new UserInfoList();
private Vector<String> vector = null;
public UserInfoList(){
this.vector = new Vector<>();
}
//外界使用的instance对象
public static UserInfoList getInstance(){
return user;
}
//增加用户
public boolean addUserInfo(String user){
if(user != null){
this.vector.add(user);
return true;
}else{
return false;
}
}
//获取用户列表
public Vector<String> getList() {
return vector;
}
//移除用户
public void removeUserInfo(String user){
if(user != null){
vector.removeElement(user);
}
}
}
cn.test.UserInfoTrace.java
package cn.test;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class UserInfoTrace implements HttpSessionBindingListener {
private String user;
private UserInfoList container = UserInfoList.getInstance();
public UserInfoTrace(){
user = "";
}
public void setUser(String user) {
this.user = user;
}
public String getUser() {
return this.user;
}
@Override
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println(this.user+"上线");
}
@Override
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println(this.user+"下线");
container.removeUserInfo(user);
}
}
cn.test.MyContentListener.java
package cn.test;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyContentListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<listener>
<listener-class>cn.test.MyContentListener</listener-class>
</listener>
</web-app>