JavaEE在线人数管理系统

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

下面是详细代码

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>character</filter-name>
 <filter-class>cn.hncu.filter.CharacterFilter</filter-class>
 <init-param>
  <param-name>charset</param-name>
  <param-value>utf-8</param-value>
 </init-param>
 </filter>
 <filter>
 <filter-name>login</filter-name>
 <filter-class>cn.hncu.filter.LoginFilter</filter-class>
 </filter>

 <filter-mapping>
 <filter-name>character</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>
 <dispatcher>REQUEST</dispatcher>
 <dispatcher>FORWARD</dispatcher>
 </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>
 <servlet>
 <servlet-name>ShowServlet</servlet-name>
 <servlet-class>cn.hncu.servlet.ShowServlet</servlet-class>
 </servlet>
 <servlet>
 <description>This is the description of my J2EE component</description>
 <display-name>This is the display name of my J2EE component</display-name>
 <servlet-name>KickOutServlet</servlet-name>
 <servlet-class>cn.hncu.servlet.KickOutServlet</servlet-class>
 </servlet>
 <servlet>
 <description>This is the description of my J2EE component</description>
 <display-name>This is the display name of my J2EE component</display-name>
 <servlet-name>LoginOutServlet</servlet-name>
 <servlet-class>cn.hncu.servlet.LoginOutServlet</servlet-class>
 </servlet>

 <servlet-mapping>
 <servlet-name>LoginServlet</servlet-name>
 <url-pattern>/LoginServlet</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>ShowServlet</servlet-name>
 <url-pattern>/servlet/ShowServlet</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>KickOutServlet</servlet-name>
 <url-pattern>/servlet/KickOutServlet</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>LoginOutServlet</servlet-name>
 <url-pattern>/servlet/LoginOutServlet</url-pattern>
 </servlet-mapping>

 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>在线人信息管理</title>
 </head>

 <body>
 <h2>在线人信息管理</h2>
 <h3>会员登录</h3>
 <c:if test="${ empty sessionScope.user.name}" var="boo">
  <form action='<c:url value="/LoginServlet"></c:url>'>
   姓名:<input type="text" name="name"><br/>
    <input type="submit" value="登录"><br/>
  </form>
 </c:if>
 <c:if test="${!boo}" >
  欢迎回来,${ sessionScope.user.name}
  <c:if test="${ sessionScope.user.admin}" var="bo">
   管理员
  </c:if>
  <c:if test="${!bo}" >
   会员
  </c:if>
 </c:if>
 <br/>
  <c:if test="${!boo}" var="boo">
  <a href='<c:url value="/servlet/ShowServlet"/>'>查看在线人信息</a><br/>
  <a href='<c:url value="/servlet/LoginOutServlet"/>'>安全退出</a><br/>
  </c:if>

 </body>
</html>

/jsps/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 {
  color: green;
  border: 1px solid blue;
  border-collapse: collapse;
  width: 500px;
  margin: auto;
 }
 td {
  border: 1px solid blue;
 }
 th {
  border: 1px solid blue;
 }
 body {
  text-align: center;
 }
</style>
 <meta http-equiv="refresh" content="3">
</head>

<body>
 <table>
  <tr>
   <th>姓名</th>
   <th>上线时间</th>
   <th>最后访问时间</th>
   <th>ip</th>
   <th>操作</th>
  </tr>

  <c:forEach items="${requestScope.onlines}" var="online">
  <tr>
   <td>
    <c:if test="${!empty online.user }" var="boo">
      ${online.user.name }
    </c:if>
    <c:if test="${empty online.user }">
      游客
    </c:if>
   </td>
   <td>${online.createTime }</td>
   <td>${online.lastAccessedTime }</td>
   <td>${online.ip }</td>
   <td>
    <c:if test="${online.user!=sessionScope.user }">
     <c:if test="${!online.user.admin&&boo}">
       <a href='<c:url value="/servlet/KickOutServlet?id=${online.id }"/>' >踢出</a>
     </c:if>
    </c:if>
   </td>
  </tr>
  </c:forEach>
 </table>
</body>
</html>

User.java(值对象)

package cn.hncu.domain;

public class User {
 private String name;
 private boolean admin;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public boolean isAdmin() {
  return admin;
 }
 public void setAdmin(boolean admin) {
  this.admin = admin;
 }
 @Override
 public String toString() {
  return "User [name=" + name + ", admin=" + admin + "]";
 }
}

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());
  }
 }

}

CharacterFilter.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 CharacterFilter implements Filter {
 private String charset;
 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
  charset=filterConfig.getInitParameter("charset");
 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  request.setCharacterEncoding(charset);
  response.setCharacterEncoding(charset);
  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)response).sendRedirect(req.getContextPath()+"/index.jsp");
   return;
  }
  chain.doFilter(request, response);
 }

 @Override
 public void destroy() {
 }

}

LoginServlet.java

package cn.hncu.servlet;

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 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=(User) request.getSession().getAttribute("user");
  if (user==null&&name!=null&&name.trim().length()>0) {//封装User
   user = new User();
   user.setName(name);
   if(name.equals("admin")){
    user.setAdmin(true);
   }
  }
  request.getSession().setAttribute("user", user);
  response.sendRedirect(request.getContextPath()+"/index.jsp");
 }

}

ShowServlet.java

package cn.hncu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
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 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>) request.getServletContext().getAttribute("onlines");
  List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();//吧Online封装成list
  if (onlines!=null) {
   Iterator<HttpSession> it = onlines.values().iterator();
   while (it.hasNext()) {
    HttpSession s = it.next();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", s.getId());
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    map.put("createTime", sdf.format(new Date(s.getCreationTime())));
    map.put("lastAccessedTime",sdf.format(new Date(s.getLastAccessedTime())) );
    map.put("ip", s.getAttribute("ip"));//ip信息在characterFilter中补
    map.put("user", s.getAttribute("user"));
    list.add(map);
   }
  }
  request.setAttribute("onlines", list);
  request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);
 }

}

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 {
  String id=request.getParameter("id");
  Map<String, HttpSession> onlines=(Map<String, HttpSession>) request.getServletContext().getAttribute("onlines");
  if (onlines!=null) {
   if (onlines.containsKey(id)) {
    onlines.get(id).invalidate();
   }
  }
  request.getRequestDispatcher("/servlet/ShowServlet").forward(request, response);
 }

}

LoginOutServlet.java

package cn.hncu.servlet;

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;

public class LoginOutServlet 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");

 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 简单实现Android学生管理系统(附源码)

    本文实例讲述了Android实现学生管理系统,分享给大家供大家参考.具体如下: (1)管理系统实现的功能主要是:学生.教师的注册登录,和选课,以及修改学生的成绩等基本简单的功能,最主要的是实现一些Dialog的使用. 界面如下: (2)主要代码如下:(个人留作笔记,如需要完整代码,在最下边免费下载) 下边是一个适配器,适配器是为了一个listvie进行设置值,其中加载的是一个itemview,适配器中还是用了继承的方法,用于通知适配器进行更新. public class CourseAdapte

  • Java设计模块系列之书店管理系统单机版(二)

    Java-单机版的书店管理系统(练习设计模块和思想_系列 一 ): http://www.jb51.net/article/91004.htm 介绍 小提示:上面一点有一个目录,可以快速定位到自己需要看的类. 今天对前面的代码有了小小的修改,让代码更加完善了一点. 至于用户唯一标识码uuid,会在以后修改成程序内部生成的, 现在的uuid还是由用户自己设置. 今天对这个程序,添加了用户界面的表现层的一部分,增加了公共类 枚举, 下面贴出目前我写的这个程序的全部代码:我会逐渐的写完这个程序的,请大

  • C语言职工管理系统设计

    本文实例为大家分享了C语言职工管理系统设计代码,供大家参考,具体内容如下 代码如下: #include<stdio.h> #include<stdlib.h> #include <string.h> struct Worker { int id;//工号 char name[16];//姓名 char sex[8];//性别 int age;//年龄 char edu_exp[32];//学历 int wage;//工资 char adress[32];//地址 cha

  • JavaWeb Session 会话管理实例详解

    Session会话简介 会话是指在一段时间内,用户使用同一个浏览器进程与Web应用之间的交互过程. 会话(Session)通常用来跟踪用户的状态,缓存用户在此浏览器进程中的信息. 当用户关闭浏览器,上一个Session也就无法再次获得了(Cookie的maxAge为-1的情况).再次打开新的浏览器,将开始一个新的会话. 类javax.servlet.http.HttpSession.每一个HttpSession代表用户的一个会话. 每一个Session的过期时间默认为30分钟. 当浏览器第一次访

  • java学生信息管理系统设计

    本例的学生信息添加进入数据库的事务(可以提交事务,事务回滚,用本地线程完善) 主页面index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <htm

  • Android实现学生管理系统

    本文实例为大家分享了Android实现学生管理系统的关键性代码,供大家参考,具体内容如下 局部效果图: 实现代码: 1.布局 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima27.sutdentmanager&qu

  • java联系人管理系统简单设计

    本文实例为大家分享了java联系人管理系统毕业设计,供大家参考,具体内容如下 要求:  请使用XML保存数据,完成一个联系人管理系统.       用户必须经过认证登录后方可以使用系统.       注册.增加.删除.查看联系人功能.        分模块进行设计. 两层框架-用户交互层,Dao层.             其他支持层-数据封装层.             工具类-加密,工厂Bean. 开发步骤: 第一步:设计数据结构-XML. 第一步:设计数据结构-XML. 第三步:准备资源并

  • java实现学籍管理系统

    本教程为大家分享了学籍管理系统的具体java代码,供大家参考,具体内容如下 1.需求分析 1.1系统功能设计 (1)能够查询学生的基本信息,如:学号,姓名年龄,班级和性别 (2)能对表格中的学生信息实现增加,删除,查找,修改等功能 (3)能将数据保存到MySQL数据库中,记录下来 (4)能把数据通过mysql数据库进行数据更新 1.2系统模块设计 学籍管理系统包括增加信息模块,删除信息模块,修改信息模块,查询信息模块,更新数据模块.系统管理人员能够查询学生的学号,姓名,性别,年龄,所获奖项等信息

  • java用户管理注册功能 含前后台代码

    本文实例为大家分享了java用户注册功能,供大家参考,具体内容如下 主页面: 浏览器显示: 代码实现: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>用户信息管理</title> </head> <body> 用户注册:<br

  • java学生信息管理系统设计(2)

    本例的学生信息添加进入数据库的事务(可以提交事务,事务回滚,用本地线程完善) 主页面index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <htm

随机推荐