JavaWeb---在线用户显示与管理---(监听器/过滤器)
Posted 夏小弥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb---在线用户显示与管理---(监听器/过滤器)相关的知识,希望对你有一定的参考价值。
管理登录(在线用户):
第一步:书写一个HttpSession的监听,并维护一个LinkedHashMap,其中保存着所有用户的Session.
第二步:让上面的类,同时实现ServletContextListener接口,并将LinkedHashMap添加到ServletContext对像中。
第三步:实现一个页面,显示所有SessionID。
第四步:在页面上添加踢出按扭,通过SessionID在Map中查询指定的Session,然后执行它的invalidate方法,清除它。从而实现踢出用户的功能。
第五步:改进,应该通过HttpSessionAttributeListener监听器,只踢出登录成功的用户。对于游客不用关心,并且只有管理员有权限踢人。
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>在线人信息管理</title>
</head>
<body>
<h2>在线人信息管理</h2>
<c:if test="$empty sessionScope.user" var="boo">
<h3>会员登录</h3>
<form action="<c:url value='/LoginServlet'/>" method="post">
姓名:<input type="text" name="name" /><br/>
<input type="submit" value="输入任意名即可登录"/>
</form>
</c:if>
<c:if test="$!boo">
欢迎你:$user.name
<c:if test="$user.admin" var="bo">
管理员
</c:if>
<c:if test="$!bo">
会员
</c:if>
<br/>
<a href="<c:url value='/servlet/ShowServlet'/>">显示所有在线用户</a>
<br/>
<a href="<c:url value='/servlet/LogOutServlet'/>">安全退出</a>
</c:if>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<filter>
<filter-name>charset</filter-name>
<filter-class>cn.hncu.filter.CharsetFilter</filter-class>
</filter>
<filter>
<filter-name>login</filter-name>
<filter-class>cn.hncu.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>charset</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/servlet/*</url-pattern>
<url-pattern>/jsps/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>cn.hncu.listener.MySessionListener</listener-class>
</listener>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>cn.hncu.servlet.LoginServlet</servlet-class>
</servlet>
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>在线人信息管理</title>
<style type="text/css">
table
border: 1px solid red;
border-collapse: collapse;
width: 80%;
td
border: 1px solid red;
padding:3px;
.header
background: gray;
</style>
</head>
<body>
<h2>以下是所有在线用户信息</h2>
<table>
<tr class="header" >
<td>姓名</td> <td>上线时间</td> <td>最后访问时间</td> <td>IP</td> <td>操作</td>
</tr>
<c:forEach items="$requestScope.onLines" var="m" >
<tr>
<td>
<c:if test="$empty m.user" var="boo">
游客
</c:if>
<c:if test="$ !boo ">
$ m.user.name
</c:if>
</td>
<td> $m.creationTime </td>
<td> $m.lastAccessTime </td>
<td> $m.ip </td>
<td>
<c:if test="$!boo && m.user.admin ">
<a href="<c:url value='/servlet/KickOutServlet?id=$m.id'/> " >踢出</a>
</c:if>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
过滤器:
CharsetFilter.java
package cn.hncu.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class CharsetFilter implements Filter
@Override
public void init(FilterConfig filterConfig) throws ServletException
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取该用户的ip,存储到它的session对象中
HttpServletRequest req = (HttpServletRequest) request;
if(req.getSession().getAttribute("ip")==null)
req.getSession().setAttribute("ip", req.getRemoteAddr() );
chain.doFilter(request, response);//放行
@Override
public void destroy()
LoginFilter.java
package cn.hncu.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginFilter implements Filter
@Override
public void init(FilterConfig filterConfig) throws ServletException
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
HttpServletRequest req = (HttpServletRequest) request;
if(req.getSession().getAttribute("user")==null)
HttpServletResponse resp = (HttpServletResponse) response;
resp.sendRedirect(req.getContextPath()+"/index.jsp");
else
chain.doFilter(request, response);
@Override
public void destroy()
监听器:
MySessionListener.java
package cn.hncu.listener;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener
@Override
public void sessionCreated(HttpSessionEvent se)
Map<String, HttpSession> onLines =(Map<String, HttpSession>) se.getSession().getServletContext().getAttribute("onLines");
if(onLines==null)//沙发
onLines = Collections.synchronizedMap( new HashMap<String, HttpSession>() ); //使用同步技术的Map
se.getSession().getServletContext().setAttribute("onLines",onLines);
onLines.put(se.getSession().getId(), se.getSession());
@Override
public void sessionDestroyed(HttpSessionEvent se)
Map<String, HttpSession> onLines =(Map<String, HttpSession>) se.getSession().getServletContext().getAttribute("onLines");
if(onLines.containsKey(se.getSession().getId()))
onLines.remove( se.getSession().getId() );
值对象:
User.java
package cn.hncu.domain;
public class User
private String name;
private String pwd;
private boolean admin;
public User()
super();
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getPwd()
return pwd;
public void setPwd(String pwd)
this.pwd = pwd;
public boolean isAdmin()
return admin;
public void setAdmin(boolean admin)
this.admin = admin;
Servlet:
LoginServlet.java
package cn.hncu.servlet;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hncu.domain.User;
public class LoginServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doPost(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
String name = request.getParameter("name");
User user = new User();
user.setName(name);
//用user作为参数到后台登录,这里我们简化了,直接假设登录成功
Random r = new Random();
int a = r.nextInt(10);
if(a%2==0)
user.setAdmin(true);
else
user.setAdmin(false);
request.getSession().setAttribute("user", user);
response.sendRedirect(request.getContextPath()+"/index.jsp");
ShowServlet.java
package cn.hncu.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ShowServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doPost(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
Map<String,HttpSession> onLines= (Map<String,HttpSession>) getServletContext().getAttribute("onLines");
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//遍历onlines,针对每个session对象封装一个map(信息包含:name,createTime,lastAccessTime,ip),把该map添加到list当中
Iterator<Entry<String, HttpSession>> it = onLines.entrySet().iterator();
while(it.hasNext())
Entry<String, HttpSession> en = it.next();
Map<String,Object> m = new HashMap<String, Object>();
m.put("id", en.getValue().getId());
m.put("user", en.getValue().getAttribute("user"));
String creationTime = sdf.format( new Date(en.getValue().getCreationTime()) );
m.put("creationTime", creationTime);
String lastAccessTime = sdf.format( new Date(en.getValue().getLastAccessedTime()) );
m.put("lastAccessTime", lastAccessTime);
m.put("ip", en.getValue().getAttribute("ip") ); //注意,该ip数据是在CharsetFilter中封装的
list.add(m);
request.setAttribute("onLines",list);
request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);
LogOutServlet.java
package cn.hncu.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LogOutServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doPost(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
request.getSession().invalidate();
response.sendRedirect(request.getContextPath()+"/index.jsp");
KickOutServlet.java
package cn.hncu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class KickOutServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doPost(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
Map<String, HttpSession> onLines = (Map<String, HttpSession>) getServletContext().getAttribute("onLines");
if(onLines.containsKey(id))
HttpSession session = onLines.get(id);
session.invalidate();
out.println("该用户已被踢出!");
else
out.println("该用户已经不存在!");
用户登录界面如下:
登陆成功界面:
显示联系人界面:
以上是关于JavaWeb---在线用户显示与管理---(监听器/过滤器)的主要内容,如果未能解决你的问题,请参考以下文章
JavaWeb学习 (二十七)————监听器(Listener)在开发中的应用
javaweb学习总结(四十七)——监听器(Listener)在开发中的应用
JavaWeb SpringMVC+MyBatis学生在线考试系统(源码+sql《精品毕设》)主要实现:用户管理角色管理学科管理考生管理试题管理考试管理试卷管理成绩统计学生端考试和成绩