⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 petclinicmansyslistener.java~130~

📁 一个基于JAVA的BS结构的宠物诊所管理系统.是当年在学校时写的,大家指点一下.
💻 JAVA~130~
字号:
package gxaccp.t07.guoneng_wei.support;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class PetClinicManSysListener
    extends HttpServlet implements ServletContextListener,
    ServletContextAttributeListener, HttpSessionListener,
    HttpSessionAttributeListener, ServletRequestListener,
    ServletRequestAttributeListener {
  //Notification that the web module is ready to process requests
  public void contextInitialized(ServletContextEvent sce) {
    /*上下文初始化*/
    this.initOnLine(sce);
//    throw new java.lang.UnsupportedOperationException(
//        "Method contextInitialized() not yet implemented.");
  }

  //Notification that the servlet context is about to be shut down
  public void contextDestroyed(ServletContextEvent sce) {
    /*上下文被销毁*/
//    throw new java.lang.UnsupportedOperationException(
//        "Method contextDestroyed() not yet implemented.");
  }

  //Notification that a new attribute has been added to the servlet context
  public void attributeAdded(ServletContextAttributeEvent scab) {
    /*添加上下文属性*/
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeAdded() not yet implemented.");
  }

  //Notification that an attribute has been removed from the servlet context
  public void attributeRemoved(ServletContextAttributeEvent scab) {
    /*去除上下文属性*/
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeRemoved() not yet implemented.");
  }

  //Notification that an attribute of the servlet context has been replaced
  public void attributeReplaced(ServletContextAttributeEvent scab) {
    /*替换上下文属性*/
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeReplaced() not yet implemented.");
  }

  //Notification that a session was created
  public void sessionCreated(HttpSessionEvent se) {
    /*创建会话*/
    this.addOnLineNumber(se);
//    throw new java.lang.UnsupportedOperationException(
//        "Method sessionCreated() not yet implemented.");
  }

  //Notification that a session was invalidated
  public void sessionDestroyed(HttpSessionEvent se) {
    /*销毁会话*/
    this.subtractOnLineNumber(se);
    this.subtractOnLineUser(se);
//    throw new java.lang.UnsupportedOperationException(
//        "Method sessionDestroyed() not yet implemented.");
  }

  //Notification that a new attribute has been added to a session
  public void attributeAdded(HttpSessionBindingEvent se) {
    //添加会话属性
    this.addOnLineUser(se);
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeAdded() not yet implemented.");
  }

  //Notification that an attribute has been removed from a session
  public void attributeRemoved(HttpSessionBindingEvent se) {
    //除去会话属性
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeRemoved() not yet implemented.");
  }

  //Notification that an attribute of a session has been replaced
  public void attributeReplaced(HttpSessionBindingEvent se) {
    //替换会话属性
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeReplaced() not yet implemented.");
  }

  //Notification that a request was created
  public void requestInitialized(ServletRequestEvent sre) {
    //请求对象ServletRequest初始化
//    throw new java.lang.UnsupportedOperationException(
//        "Method requestInitialized() not yet implemented.");
  }

  //Notification that a request was invalidated
  public void requestDestroyed(ServletRequestEvent sre) {
    //请求对象被销毁
//    throw new java.lang.UnsupportedOperationException(
//        "Method requestDestroyed() not yet implemented.");
  }

  //Notification that a new attribute has been added to a request
  public void attributeAdded(ServletRequestAttributeEvent srae) {
    //添加请求属性
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeAdded() not yet implemented.");
  }

  //Notification that an attribute has been removed from a request
  public void attributeRemoved(ServletRequestAttributeEvent srae) {
    //除去请求属性
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeRemoved() not yet implemented.");
  }

  //Notification that an attribute of a request has been replaced
  public void attributeReplaced(ServletRequestAttributeEvent srae) {
    //替换请求属性
//    throw new java.lang.UnsupportedOperationException(
//        "Method attributeReplaced() not yet implemented.");
  }

  private synchronized void addOnLineNumber(HttpSessionEvent se) { //增加在线人数
    ServletContext sc = se.getSession().getServletContext();
    if (sc != null) { //上下文不为空不为空
      if (sc.getAttribute("OnLineNumber") == null) { //上下文属性为空时
        sc.setAttribute("OnLineNumber", new Integer(0));
      }
      Integer intobj = (Integer) sc.getAttribute("OnLineNumber");
      int onLineNumber = intobj.intValue();
      onLineNumber++;
      sc.setAttribute("OnLineNumber", new Integer(onLineNumber));
    }
  }

  private synchronized void subtractOnLineNumber(HttpSessionEvent se) {
    //线人数减少一位
    ServletContext sc = se.getSession().getServletContext();
    if (sc != null) { //上下文不为空
      if (sc.getAttribute("OnLineNumber") != null) { //上下文属性不为空
        Integer intobj = (Integer) sc.getAttribute("OnLineNumber");
        int onLineNumber = intobj.intValue();
        if (onLineNumber > 0) {
          onLineNumber--;
        }
        sc.setAttribute("OnLineNumber", new Integer(onLineNumber));
      }
    }
  }

  private synchronized void addOnLineUser(HttpSessionBindingEvent se) {
    //新登录了一个用户
    if ("UserInfo".equals(se.getName())) {
      ServletContext sc = se.getSession().getServletContext();
      Integer intobj = (Integer) sc.getAttribute("OnLineUser");
      int onLineUser = intobj.intValue();
      onLineUser++;
      sc.setAttribute("OnLineUser", new Integer(onLineUser));
    }
  }

  private synchronized void subtractOnLineUser(HttpSessionEvent se) {
    //新下线了一个用户
    if (se.getSession().getAttribute("UserInfo") != null) {
      ServletContext sc = se.getSession().getServletContext();
      Integer intobj = (Integer) sc.getAttribute("OnLineUser");
      int onLineUser = intobj.intValue();
      if (onLineUser > 0) {
        onLineUser--;
      }
      sc.setAttribute("OnLineUser", new Integer(onLineUser));
    }
  }

  private synchronized void initOnLine(ServletContextEvent sce) {
    //初始化在线记录
    ServletContext sc = sce.getServletContext();
    sc.setAttribute("OnLineNumber", new Integer(0)); //在线浏览数
    sc.setAttribute("OnLineUser", new Integer(0)); //在线登录数
    System.out.println("---在上下文被初始化时,添加在线人数属性,并初始化这个属性---");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -