1.监听上下文的类

 package com.examp.ch9;

 import java.io.FileOutputStream;
 import java.io.PrintWriter;
 import java.util.Date;

 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextAttributeEvent;
 import javax.servlet.ServletContextAttributeListener;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;

 public final class MyServletContextListener
 implements ServletContextListener, ServletContextAttributeListener
 {
 private ServletContext context = null;

 public void contextDestroyed(ServletContextEvent sce)
 {
   logout("调用contextDestroyed()方法-->ServletContext被销毁");
   this.context = null;
 }

 public void contextInitialized(ServletContextEvent sce)
 {
   this.context = sce.getServletContext();
   logout("调用contextInitialized()方法-->ServletContext初始化");
 }

 public void attributeAdded(ServletContextAttributeEvent scae)
 {
   logout("调用attributeAdded('" + scae.getName() + "', '" + scae.getValue() +
     "')方法-->增加了一个属性");
 }

 public void attributeRemoved(ServletContextAttributeEvent scae)
 {
   logout("调用attributeRemoved('" + scae.getName() + "', '" + scae.getValue() +
     "')方法-->删除了该属性");
 }

 public void attributeReplaced(ServletContextAttributeEvent scae)
 {
   logout("调用attributeReplaced('" + scae.getName() + "', '" + scae.getValue() +
     "')方法-->更改了该属性");
 }

 private void logout(String message)
 {
   PrintWriter out = null;
   try
   {
     out = new PrintWriter(new FileOutputStream("E:\\contextLog.txt", true));
     out.println(new Date().toLocaleString() + message);
     out.close();
   }
   catch (Exception e)
   {
     out.close();
     e.printStackTrace();
   }
 }
 }

2.监听Http会话的类

 package com.examp.ch9;

 import java.io.FileOutputStream;
 import java.io.PrintWriter;
 import java.util.Date;

 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.http.HttpSessionAttributeListener;
 import javax.servlet.http.HttpSessionBindingEvent;
 import javax.servlet.http.HttpSessionEvent;
 import javax.servlet.http.HttpSessionListener;
 /*
  * HttpSessionListener Http会话的创建、销毁
  * HttpSessionAttributeListener 监听会话中属性的改变
  */
 public final class MySessionListener implements HttpSessionAttributeListener, HttpSessionListener
 {
 ServletContext context;//创建一个context对象
 int users = 1;//初始化用户数量为1
 /*
  * 在session中添加对象时触发此操作 笼统的说就是调用setAttribute这个方法时候会触发的
  *
  */
 public void attributeAdded(HttpSessionBindingEvent event)
 {
   logout("attributeAdded('" + event.getSession().getId() + "', '" +
     event.getName() + "', '" + event.getValue() + "')");
 }
 /*
  * 修改、删除session中添加对象时触发此操作  笼统的说就是调用 removeAttribute这个方法时候会触发的
  *
  */
 public void attributeRemoved(HttpSessionBindingEvent event)
 {
   logout("attributeRemoved('" + event.getSession().getId() + "', '" +
     event.getName() + "', '" + event.getValue() + "')");
 }
 /*
  * 在Session属性被重新设置时
  *
  */
 public void attributeReplaced(HttpSessionBindingEvent se)
 {
   logout("attributeReplaced('" + se.getSession().getId() + ",'" + se.getName() + "','" + se.getValue() + "')");
 }

 /*
  * 新建一个会话时候触发也可以说是客户端第一次和服务器交互时候触发
  */
 public void sessionCreated(HttpSessionEvent event)
 {
   System.out.println(users);
   this.users += 1;//获取ID 和用户个数
   logout("sessionCreated('" + event.getSession().getId() + "'),目前有" + this.users + "个用户");
   this.context.setAttribute("users", new Integer(this.users));//将用户数存入context
 }
 /*
  * 销毁会话的时候  一般来说只有某个按钮触发进行销毁 或者配置定时销毁
  *
  */
 public void sessionDestroyed(HttpSessionEvent event)
 {
   this.users --;
   logout("sessionDestroyed('" + event.getSession().getId() + "'),目前有" + this.users + "个用户");//获取ID 和用户个数
   this.context.setAttribute("users", new Integer(this.users));////将用户数存入context
 }

 public void contextDestroyed(ServletContextEvent sce)
 {
   logout("contextDestroyed()-->ServletContext被销毁");
   this.context = null;
 }

 public void contextInitialized(ServletContextEvent sce)
 {
   this.context = sce.getServletContext();
   logout("contextInitialized()-->ServletContext初始化了");
 }

 private void logout(String message)
 {
   PrintWriter out = null;
   try
   { //创建输入流 写入文件
     out = new PrintWriter(new FileOutputStream("E:\\sessionLog.txt", true));
     out.println(new Date().toLocaleString() + "-->" + message);
     out.close();
   }
   catch (Exception e)
   {
     out.close();
     e.printStackTrace();
   }
 }
 }

3.前端JSP文件

context_test.jsp

 <%@ page contentType="text/html;charset=UTF-8"%>
 <%
 out.println("add attribute");
 getServletContext().setAttribute("userName","Smith");
 out.println("replace attribute");
 getServletContext().setAttribute("userName","Kate");
 out.println("remove attribute");
 getServletContext().removeAttribute("userName");
 %>

session_test.jsp

 <%@ page contentType="text/html;charset=UTF-8"%>
 执行了以下的操作:
 session.setAttribute("userName","Smith")<br>
 <% session.setAttribute("userName","Smith");%><!--添加属性-->
 session.setAttribute("userName","Kate")<br>
 <% session.setAttribute("userName","Kate");%><!--更改属性-->
 session.removeAttribute("userName","Kate")<br>
 <% session.removeAttribute("userName");%><!--删除属性-->
 目前有<%=getServletContext().getAttribute("users")%>个用户。<br>
 after session.invalidate()<br>
 <% session.invalidate();%><!--销毁该session-->
 目前有<%=getServletContext().getAttribute("users")%>个用户。

JavaWeb监听器的使用(一)监听上下文和会话信息的更多相关文章

  1. SpringBoot2.0 监听器ApplicationListener的使用-监听ApplicationReadyEvent事件

    参考:http://www.shareniu.com/article/73.htm 一.需求是想将我的写一个方法能在项目启动后就运行,之前使用了redis的消息监听器,感觉可以照着监听器这个思路做,于 ...

  2. JavaWEB监听器

    1.基本概念 JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext, HttpSession和 ServletRequest等域对象 ...

  3. 监听域对象创建和销毁的Listener

    1.什么是Servlet监听器? 先来看看什么是监听器.监听器是专门用于对其它对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时立即采取相应的行动.Servlet监听器是S ...

  4. javaweb监听器实现与原理

    参考:https://www.cnblogs.com/lxp503238/p/6678688.html https://blog.csdn.net/CPOHUI/article/details/888 ...

  5. 【监听文件 多线程】使用java--WatchService监听文件 开启多线程copy文件

    有一个小需求: 在PC跟前没有人的时候,迅雷下载文件 至PC磁盘上,并且自动移动文件到U盘上,小主只要在走的时候取走U盘即可. 基于这个需求,有了下面这段代码:[JDK  1.8] package c ...

  6. javascript事件监听与事件委托

      事件监听与事件委托 在js中,常用到element.addEventListener()来进行事件的监听.但是当页面中存在大量需要绑定事件的元素时,这种方式可能会带来性能影响.此时,我们可以用事件 ...

  7. JAVAWEB监听器(二)

    监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事 ...

  8. ALERT日志中常见监听相关报错之中的一个:ORA-609错误的排查

    參考MOS文档有: Troubleshooting Guide ORA-609 : Opiodr aborting process unknown ospid (文档 ID 1121357.1) Al ...

  9. Java-Web监听器

    在WEB端实现监听实质: 实现一系列的监听接口(实现相应的接口,覆写各接口中相应的方法,在相应的事件触发的时候会执行自己的监听器中的覆写的方法,在各个方法中完成自己想要的操作,从而实现了监听) 监听- ...

随机推荐

  1. <转>MySQL性能调优的10个方法

    文章原地址:http://mp.weixin.qq.com/s/oRXJRz_Y5drmIrcbxSKOcw 1. 选择合适的存储引擎: InnoDB 除非你的数据表使用来做只读或者全文检索 (相信现 ...

  2. mysql-5.7.11-winx64.zip 安装配置

    1.下载 http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.11-winx64.zip 2.解压缩zip包: D:\Program Files\m ...

  3. jQuery Ajax传值给Servlet,在Servlet里Get接受参数乱码的解决方法

    最近在学jquery ui,在做一个小功能的时候需要将前台的值获取到,通过Ajax传递给Servlet,然后再在返回数据结果,但是在Servlet接受参数的时候,通过后台打印,发现接受乱码,代码示例如 ...

  4. Ubuntu 16.04 LAMP server 指南 - 配置 Apache2.4,PHP7,和MariaDB(而不是MySQL)

    翻译自:https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ 昨天在虚 ...

  5. 5G系统架构

    原文标题:迈向5G之路,颠覆性的5G系统架构?   本文部分图片,资料摘自<迈向5G C-RAN:需求.架构与挑战> 突如一夜春风来,随着Polar码与LDPC码作为5G编码候选方案,通信 ...

  6. BootStrap学习笔记,优缺点总结

    本篇约定Bootstrap简写为BT   BT的受欢迎程度是大家有目共睹的,用它可以快速的搭建出网站.很早就接触过这个框架,其中的栅格系统,css模块化以及js插件做的相当不错,由于工作中较少使用也一 ...

  7. C/C++ 标准输入输出重定向

    转载自:http://www.cnblogs.com/hjslovewcl/archive/2011/01/10/2314356.html 这个对经常在OJ上做题的童鞋们很有用.OJ基本都是用标准输入 ...

  8. python学习之路 第一天

    1.Python 3 安装. 2.Python 开发工具 PyCharm安装. 3.print("hello world!")  #打印hello world!  注:Python ...

  9. 二.持续集成之--WEB后台

    1.系统管理-系统设置:把linux服务器加进去 2.General配置 3.源码管理: 4.构建触发器 5.构建环境 6.构建 7.构建后操作

  10. Java防止SQL注入2(通过filter过滤器功能进行拦截)

    首先说明一点,这个过滤器拦截其实是不靠谱的,比如说我的一篇文章是介绍sql注入的,或者评论的内容是有关sql的,那会过滤掉:且如果每个页面都经过这个过滤器,那么效率也是非常低的. 如果是要SQL注入拦 ...