JavaEE在线人数管理系统
发布时间:2020-12-05 03:28:43  所属栏目:Java  来源:互联网 
            导读:这篇博客是filter、listener和servlet技术的相关总结,实现了简单的显示在线人数、在线人详细信息、管理员踢人功能
                
                
                
            | 
                         这篇博客是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>) 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() {
 }
}
                        (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
