`
Reverie夜
  • 浏览: 20325 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

struts2监听器

    博客分类:
  • JAVA
阅读更多

与拦截器差不多~配置→写类√两步解决~_(:зゝ∠)_,~这是一个统计在线人数的小测试~

web.xml中配置listener标签,用自定义的监听类替换掉原先的~session-config标签中设置session的超时时间

web.xml:

 

        <listener>
    	<!-- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> -->
    	<listener-class>com.web.listener.CountLineListener</listener-class>
	</listener>
	<!-- 设置session超时时间 -->
	<session-config>
		<session-timeout>60</session-timeout>
	</session-config>

 

 

CountLineListener.java:

package com.web.listener;

import java.text.SimpleDateFormat;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.web.context.ContextLoaderListener;

/**
 * 监听器
 * @author Administrator
 *
 */
public class CountLineListener extends ContextLoaderListener implements HttpSessionListener{
	
	public CountLineListener() {
		super();
	}
	
	public void sessionCreated(HttpSessionEvent se) {
		System.out.println("创建session......"); 
        ServletContext context = se.getSession().getServletContext(); 
        Integer count = (Integer)context.getAttribute("count"); 
        if(count == null){ 
            count = new Integer(1); 
        }else{ 
            int co = count.intValue( ); 
            count= new Integer(co+1); 
        } 
        System.out.println(new SimpleDateFormat("HH:mm:ss").format(System.currentTimeMillis())+" 当前用户人数:"+count); 
        context.setAttribute("count", count);//保存人数
		
	}
	
	public void sessionDestroyed(HttpSessionEvent se) {
		System.out.println("销毁session......"); 
        ServletContext context = se.getSession().getServletContext(); 
        Integer count = (Integer)context.getAttribute("count"); 
        int co = 0;
        if(count != null)count.intValue();
        count = new Integer(co - 1); 
        context.setAttribute("count", count); 
        System.out.println(new SimpleDateFormat("HH:mm:ss").format(System.currentTimeMillis())+" 当前用户人数:"+count); 
		
	}
	
} 

session创建时会调用sessionCreated(HttpSessionEvent se)方法,销毁时调用sessionDestroyed(HttpSessionEvent se)方法,用不同浏览器测试√:


 

  • 大小: 1.9 KB
分享到:
评论

相关推荐

    Struts2的监听器的使用

    Struts2的监听器的使用实例 博文链接:https://zmx.iteye.com/blog/457435

    STRUTS:listener监听器

    STRUTS:listener监听器

    struts2拦截器及监听器示例demo

    默认登录页面 http://localhost:8080/Struts_ljq ...登录成功页面有add,delete,modify方法,配置有不同拦截,在后台看拦截信息。...有全局拦截器配置和方法拦截,及监听器简单运用, 仅新手学习demo

    整合struts2和spring源代码(可以直接在tomcat中运行)

    3.配置spring的监听器: 在web.xml中添加 org.springframework.web.context.ContextLoaderListener 4.配置spring的配置文件(可以查看WEB-INF里面的applicationContext.xml文件) 注意:如果是默认...

    Struts2属性文件详解

    该属性的默认值是org.apache.Struts2.config.DefaultConfiguration, 这是Struts 2默认的配置文件管理器.如果需要实现自己的配置管理器,开发者则可以实现一个实现Configuration接口的类,该类可以自己加载Struts 2配置...

    struts2+spring+hibernate整合示例

    web容器中(web.xml)中添加struts2 filter以及spring 监听器。 b 在struts.xml中添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ...

    struts2-spring-plugin-2.1.2.jar

    导入struts2-spring-plugin包,在web.xml中设置spring的监听器, spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。 struts2-spring-plugin包为我们将struts2的对象工厂设置为...

    Struts2\constant应用

    该属性的默认值是org.apache.struts2.views.freemarker.FreemarkerManager,这是Struts 2内建的FreeMarker管理器。 struts.freemarker.wrapper.altMap 该属性只支持true和false两个属性值,默认值是true。通常...

    Struts2学习文档,Struts入门学习资料

    Struts2学习文档,Struts入门学习资料,包括(struts-xml,action,上传下载,监听器,拦截器,验证,类型转换,国际化等等)

    基于struts2 的网络U盘

    软硬件环境: Windows Server 2003/PC 相关技术: Jsp+Struts2+MD5加密 ...tomcat启动时监听器实现扫描所有文件生成Map(存放加密码和对应文件绝对路径)。过滤器设置不允许上传文件的类型,过滤不安全文件

    Struts2 拦截结果监听器(三十)

    NULL 博文链接:https://takeme.iteye.com/blog/1651508

    struts2课件

    struts2课件 很好的struts2当输入login.jsp访问jsp页面填写完相关信息并提交给login.action时,它会首先被在web.xml中配置的过滤器监听到,过滤器会去查找strust.xml文件,并结合namespace查找名为login的action,...

    学习常用知识(java,sql,oracle,ejb,ssh,struts,xml,监听器,拦截器,过滤器)

    学习常用知识(java,sql,oracle,ejb,ssh,struts,xml,监听器,拦截器,过滤器等等)这些都是本人的学习心得非常适用,希望能该初学者带来更大的帮助!!!

    第17讲 Spring、hibernate和Struts2的整合

    ①在web.xml中配置ApplicationContext.xml,并使用ContextLoader监听器实例化spring容器 ②把action交给spring管理,即在spring配置文件中定义action Bean并使用依赖注入功能在action中注入业务Bean,同时修改作用域...

    Java Web整合开发王者归来(JSP+Servlet+Struts+Hibernate+Spring)

    第2篇为基础篇,内容包括Servlet技术、JSP技术、会话跟踪、过滤器Filter、监听器Listener等;第3篇为高级篇,内容包括JSTL标签库的使用、如何自定义标签、资源国际化、数据库概述、JDBC详解、XML概述等;第4篇为...

    Spring + struts +hibernate(SSHAnnotation) 全注解

    Struts2+Spring3+hibernate3全注解,操作oracle emp表的查询

    自定义PROXOOL监听器整合SSH框架

    1、proxool官网,下载最新的proxool-0.9.1.jar ...4、整合Struts2、Spring和proxool,配置Web服务启动配置文件[web.xml] 5、整合Spring和proxool,配置应用上下文配置文件[applicationContext.xml]

    Struts2框架整合Spring整合步骤

    1.新建工程,加入所需要的包; 2.配置sturts.xml; 3.配置Spring监听器; 4.Spring配置文件; .......

    Struts2.0 实现文件上传进度

    Struts2.0 实现文件上传进度 原理和实现 主要是实现Struts2.0的监听器

    线程+监听技术 简单的例子 和详细讲解

    监听器配置,java监听器,登陆监听,Listener,struts监听器配置,用户在线统计  所谓线程(或称线索,thread),指程序中的以单一的顺序控制流。线程按顺序执行,即在一个线程中,一个时刻只能由一个执行点。显然...

Global site tag (gtag.js) - Google Analytics