统计在线用户 踢人

原创 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);
}
 
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

apache shiro踢出用户和获取所有在线用户

apache shiro踢出用户: 产品要求用户只能在同一个地方登录,如果之前在其他机器或者浏览器上登录,讲之前登录帐号踢出。applicationContext-shiro.xml配置: 在默...

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

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

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

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

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

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

用户在线踢人

index.jsp:   用户登录           欢迎${requestScopse.user.username}  注销     loggin.jsp 用户...

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

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

统计用户登陆在线数量

【原创作者】:丛兴滋(cncxz)[E-mail:cncxz@126.com]【关 键 词】:xmlhttp  ASP.NET在线用户列表  关闭浏览器  精确统计 单人登陆【代码下载】:http:/...

统计在线用户数以及同账号登录踢出另一用户

参考博客:http://blog.csdn.net/licheng989/article/details/51579979 思路:写一个session属性监听器(SessionListener),实现...

ASP_NET中在线用户统计

统计在线用户的作用不言而喻,就是为了网站管理者可以知道当前用户的多少,然后根据用户数量来观察服务器或者程序的性能,从而可以直观的了解到网站的吸引力或者网站程序的效率。现在,我们就介绍一个简单明了的方法...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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