显示在线人员列表
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="loginConf.jsp" method="post">
<table>
<tbody>
<tr>
<td><font color="red">${requestScope.error}</font></td>
</tr>
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录判断页面</title>
</head>
<body>
<%
String username = request.getParameter("username");
// 登录判断省略
session.setAttribute("username", username);//将用户名保存到session中
%>
<jsp:forward page="UserList.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在线人员列表</title>
</head>
<body>
<h3>在线人员列表</h3>
<h5>欢迎${sessionScope.username}的登录,您可以选择<a href="loginOut.jsp">注销</a></h5>
<h5>当前总在线人数${fn:length(applicationScope.allUser)}</h5>
<c:forEach items="${applicationScope.allUser}" var="username" varStatus="stat">
<img src="images/username.gif"><c:out value="${username}"></c:out>
<c:if test="${(stat.index+1)%5==0}">
<br/>
</c:if>
</c:forEach>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注销</title>
</head>
<body>
<%
session.invalidate();
%>
<jsp:forward page="loginForm.jsp"></jsp:forward>
</body>
</html>
package com.mison.listener;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineUserListListener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {
private ServletContext application = null;
public void contextInitialized(ServletContextEvent sce) {
ArrayList<String> allUser = new ArrayList<String>();
application = sce.getServletContext();
application.setAttribute("allUser", allUser);
}
public void contextDestroyed(ServletContextEvent sce) {
}
public void sessionCreated(HttpSessionEvent se) {
}
public void sessionDestroyed(HttpSessionEvent se) {
ArrayList<String> allUser = (ArrayList<String>) application.getAttribute("allUser");
String user = (String) se.getSession().getAttribute("username");
allUser.remove(user);
application.setAttribute("allUser", allUser);
}
public void attributeAdded(HttpSessionBindingEvent se) {
ArrayList<String> allUser = (ArrayList<String>) application.getAttribute("allUser");
String user = (String) se.getValue();
allUser.add(user);
application.setAttribute("allUser", allUser);
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<listener>
<description>在线人数监听器</description>
<listener-class>com.mison.listener.OnlineUserListListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
本文介绍如何利用Java的监听器技术,在JavaEE环境中,结合Servlet和JSP,实现实时显示网站当前在线人数的功能。通过监听session的创建与销毁,更新HTML页面的在线计数。
1106

被折叠的 条评论
为什么被折叠?



