欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaEE在线人数管理系统

程序员文章站 2024-03-12 21:15:26
这篇博客是filter、listener和servlet技术的相关总结,实现了简单的显示在线人数、在线人详细信息、管理员踢人功能 下面是详细代码 web.xml...

这篇博客是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");

 }

}

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