统计在线用户 踢人

原创 2015年07月07日 23:36:19


2014年9月8日 16:55:13





<%@ 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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
 
</head>
<body>
<c:if test="${sessionScope.user==null}">
<form action="${pageContext.request.contextPath}/servlet/LoginServlet" method="post">
username:<input type="text" name="username"/>(用户名不能重复,且不能为空)<br/>
<input type="submit" value="登录"/>
</form>
</c:if>
<c:if test="${sessionScope.user!=null}">
欢迎您:${sessionScope.user.username}<a href="#">注销</a>
</c:if>
<a href="onlineuser.jsp">在线用户</a>
</body>
</html>



package cn.itcast.domain;
 
import java.io.Serializable;
 
public class User implements Serializable {
private String username;
 
public String getUsername() {
return username;
}
 
public void setUsername(String username) {
this.username = username;
}
}



package cn.itcast.web.controller;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import cn.itcast.domain.User;
 
public class LoginServlet extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//取到用户名:
String username = request.getParameter("username");
//验证用户是否合法(略)
User user = new User();
user.setUsername(username);
//正确:放到HttpSession中
HttpSession session = request.getSession();
session.setAttribute("user", user);
out.write("登录成功");
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}





package cn.itcast.domain;
 
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
//此处核心代码
public class OnlineUserListener implements HttpSessionAttributeListener {
//只要向HttpSession的域中放东东都会执行该方法
//我们要的是放的是User对象,标记还是user的东东
//找个地方存起来:容器要求唯一;页面上还能访问到该容器
public synchronized void attributeAdded(HttpSessionBindingEvent se) {
//找到存储的是user标记,且绑定的对象时User类
HttpSession session = se.getSession();
ServletContext sc = session.getServletContext();
Object obj = session.getAttribute("user");
if(obj!=null&&obj instanceof User){
//向容器中存起来
Map<String,HttpSession> map = (Map<String, HttpSession>) sc.getAttribute("users");//存放东东的容器(Map<String,HttpSession>.key:用户名;value:HttpSession对象)
if(map==null){
map = new HashMap<String, HttpSession>();//Collections.synchronizedMap(map)
sc.setAttribute("users", map);
}
map.put(((User)obj).getUsername(), session);
}
}
 
public synchronized void attributeRemoved(HttpSessionBindingEvent se) {
}
 
public void attributeReplaced(HttpSessionBindingEvent se) {
 
}
 
}



<%@ 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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
 
</head>
<body>
<h1>在线用户</h1>
<c:if test="${empty applicationScope.users}">
对不起!没有在线用户
</c:if>
<c:if test="${!empty applicationScope.users}">
<c:forEach items="${applicationScope.users}" var="me">
<c:url value="/servlet/KickServlet" var="url">
<c:param name="username" value="${me.key}"></c:param>
</c:url>
${me.key}  <a href="${url}">踢出</a><br/>
</c:forEach>
</c:if>
</body>
</html>




package cn.itcast.web.controller;
 
import java.io.IOException;
import java.util.Map;
 
import javax.servlet.ServletContext;
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 KickServlet extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = getServletContext();
Map<String, HttpSession> users = (Map<String, HttpSession>) sc.getAttribute("users");
if(users!=null){
String username = request.getParameter("username");
username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
HttpSession s = users.get(username);
if(s!=null){
s.invalidate();
users.remove(username);
}
}
response.sendRedirect(request.getContextPath()+"/onlineuser.jsp");
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}
2014年9月8日 16:55:13






<%@ 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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
 
</head>
<body>
<c:if test="${sessionScope.user==null}">
<form action="${pageContext.request.contextPath}/servlet/LoginServlet" method="post">
username:<input type="text" name="username"/>(用户名不能重复,且不能为空)<br/>
<input type="submit" value="登录"/>
</form>
</c:if>
<c:if test="${sessionScope.user!=null}">
欢迎您:${sessionScope.user.username}<a href="#">注销</a>
</c:if>
<a href="onlineuser.jsp">在线用户</a>
</body>
</html>



package cn.itcast.domain;
 
import java.io.Serializable;
 
public class User implements Serializable {
private String username;
 
public String getUsername() {
return username;
}
 
public void setUsername(String username) {
this.username = username;
}
}



package cn.itcast.web.controller;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import cn.itcast.domain.User;
 
public class LoginServlet extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//取到用户名:
String username = request.getParameter("username");
//验证用户是否合法(略)
User user = new User();
user.setUsername(username);
//正确:放到HttpSession中
HttpSession session = request.getSession();
session.setAttribute("user", user);
out.write("登录成功");
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}





package cn.itcast.domain;
 
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
//此处核心代码
public class OnlineUserListener implements HttpSessionAttributeListener {
//只要向HttpSession的域中放东东都会执行该方法
//我们要的是放的是User对象,标记还是user的东东
//找个地方存起来:容器要求唯一;页面上还能访问到该容器
public synchronized void attributeAdded(HttpSessionBindingEvent se) {
//找到存储的是user标记,且绑定的对象时User类
HttpSession session = se.getSession();
ServletContext sc = session.getServletContext();
Object obj = session.getAttribute("user");
if(obj!=null&&obj instanceof User){
//向容器中存起来
Map<String,HttpSession> map = (Map<String, HttpSession>) sc.getAttribute("users");//存放东东的容器(Map<String,HttpSession>.key:用户名;value:HttpSession对象)
if(map==null){
map = new HashMap<String, HttpSession>();//Collections.synchronizedMap(map)
sc.setAttribute("users", map);
}
map.put(((User)obj).getUsername(), session);
}
}
 
public synchronized void attributeRemoved(HttpSessionBindingEvent se) {
}
 
public void attributeReplaced(HttpSessionBindingEvent se) {
 
}
 
}



<%@ 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>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
 
</head>
<body>
<h1>在线用户</h1>
<c:if test="${empty applicationScope.users}">
对不起!没有在线用户
</c:if>
<c:if test="${!empty applicationScope.users}">
<c:forEach items="${applicationScope.users}" var="me">
<c:url value="/servlet/KickServlet" var="url">
<c:param name="username" value="${me.key}"></c:param>
</c:url>
${me.key}  <a href="${url}">踢出</a><br/>
</c:forEach>
</c:if>
</body>
</html>




package cn.itcast.web.controller;
 
import java.io.IOException;
import java.util.Map;
 
import javax.servlet.ServletContext;
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 KickServlet extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = getServletContext();
Map<String, HttpSession> users = (Map<String, HttpSession>) sc.getAttribute("users");
if(users!=null){
String username = request.getParameter("username");
username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
HttpSession s = users.get(username);
if(s!=null){
s.invalidate();
users.remove(username);
}
}
response.sendRedirect(request.getContextPath()+"/onlineuser.jsp");
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

监听器(实现在线统计人数,踢人)

Servlet监听器 一、监听三个域对象创建和销毁的事件监听器 1.  ServletContextListener接口 MyServletContextListener pu...
  • mm2223
  • mm2223
  • 2011年11月24日 20:36
  • 4346

JavaWeb实例:记录在线人员信息,管理员可以踢人

总的需求:   1、使用HttpSessionListener记录在线人数。 2、使用HttpSessionAttributeListener记录登录人数。 3、 将用户踢出系统。     成果展示:...

用户在线踢人

index.jsp:   用户登录           欢迎${requestScopse.user.username}  注销     loggin.jsp 用户...
  • hcq__yy
  • hcq__yy
  • 2013年10月05日 22:38
  • 418

监听器--显示登陆用户列表,并实现踢人功能

用户:login.jsp à loginServlet à  index.jsp 管理员: listUser.jsp à kickUserServlet 监听器:SessionAttributeL...

单一用户登录,即当前用户登录后要踢出前一个登录,即做出踢人效果,如何实现?

单一用户登录,即当前用户登录后要踢出前一个登录,即做出踢人效果,如何实现? 一般情况下,用户登录,有两种方式:cookie方式,session方式。一般情况下,session方式是使用最多的...

JavaEE——在线人管理系统(显示在线人数、在线人详细信息、管理员踢人)

这篇博客是filter、listener和servlet技术的相关总结,实现了简单的显示在线人数、在线人详细信息、管理员踢人功能下面是详细代码web.xml ...

让你的 Nginx 的 RTMP 直播具有统计某频道在线观看用户数量的功能

你的 Nginx 已经有了 RTMP 直播功能的话,如果你还想统计某直播频道当前观看用户量的话,可以加入 with-http_xslt_module 模块。具体步骤如下:         1.查看原来...
  • defonds
  • defonds
  • 2013年06月09日 16:10
  • 11431

java session HttpSessionListener、HttpSessionBindingListener使用区别,实现在线人数统计以及踢出用户

java web项目中管理session的时候,除了在web.xml文件中设置过时时间 2 xxx分钟,还可以通过HttpSessionListener接口或HttpSessionBindi...

统计用户登陆在线数量

【原创作者】:丛兴滋(cncxz)[E-mail:cncxz@126.com]【关 键 词】:xmlhttp  ASP.NET在线用户列表  关闭浏览器  精确统计 单人登陆【代码下载】:http:/...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:统计在线用户 踢人
举报原因:
原因补充:

(最多只允许输入30个字)