Listener(2)—案例

  • ServletContext的事件监听器,创建:当前web应用被加载(或重新加载)到服务器中,销毁:当前web应用被卸载
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
 * ServletContext的事件监听器,创建:当前web应用被加载(或重新加载)到服务器中,销毁:当前web应用被卸载
 */
public class ApplicationListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("当前web应用被加载到服务器中,创建ServletContextListener对象...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("当前web应用被卸载,销毁ServletContextListener对象...");
    }

}
  • HttpSession对象的监听器,创建:会话的开始,销毁:session调用invalidate()方法,或者会话时间到期
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
 * HttpSession对象的监听器,创建:会话的开始,销毁:session调用invalidate()方法,或者会话时间到期
 */
public class SessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("监听HttpSession对象的创建...");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("监听HttpSession对象的销毁...");
    }

}
  • HttpSession对象的监听器,创建:一次请求的开始,销毁:一次请求的结束
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/**
 * HttpSession对象的监听器,创建:一次请求的开始,销毁:一次请求的结束
 *
 */
public class RequestListener implements ServletRequestListener {

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("监听ServletRequest对象的销毁...");
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("监听ServletRequest对象的创建...");
    }

}

页面:

demo1.jsp

<body>

    <h3>Demo1 Page</h3>
<!-- 1.请求页面 --> 
    <!-- 使用该方法无法跨页面传递参数 -->
    <a href="demo2.jsp">To test Page</a>

    <%  
        //为request对象设置一个属性
        request.setAttribute("name", "test1");
    %>

    <!-- 使用forward方法可以跨页面传递参数 -->
    <jsp:forward page="demo2.jsp"></jsp:forward>

</body>

demo2.jsp

<body>

    <h3>销毁session</h3>
    <%
        session.invalidate();
    %>

    name:<%=request.getAttribute("name") %>
    <br><br>
<!-- 2.请求Servlet -->        

    <a href="testServlet">TestServlet </a>      

</body>

demo3.jsp

<body>

    <h3>Demo3 Page</h3>

    name:<%=request.getAttribute("name") %>

</body>

session.jsp

<body>

    <h3>Session Page</h3>

    <%
        //在page标签中禁用掉session,然后显式的创建一个session
        HttpSession session = request.getSession();

        //销毁session,1.设置过期时间,2.调用invalidate方法
        session.setMaxInactiveInterval(10);

        //session.invalidate();


    %>

</body>

web.xml配置文件

<!-- 1.监听域对象自身的创建和销毁的事件监听器 -->
  <listener>
    <listener-class>com.test.demo1.ServletContextListener.ApplicationListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.test.demo1.HttpSessionListener.SessionListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.test.demo1.ServletRequestListener.RequestListener</listener-class>
  </listener>

<!-- 2.监听域对象中属性的增加和删除的事件监听器 -->  
  <listener>
    <listener-class>com.test.demo2.HttpSessionAttributeListener.SessionAttributeListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.test.demo2.ServletContextAttributeListener.ApplicationAttributeListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.test.demo2.ServletRequestAttributeListener.RequestAttributeListener</listener-class>
  </listener>

结果:

信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [232] milliseconds.
当前web应用被加载到服务器中,创建ServletContextListener对象...
Application中添加属性:org.apache.jasper.compiler.TldLocationsCache :org.apache.jasper.compiler.TldLocationsCache@1bdb367
十一月 30, 2017 2:54:53 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8084"]
十一月 30, 2017 2:54:53 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
十一月 30, 2017 2:54:53 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 1386 ms
监听ServletRequest对象的创建...
Request中替代属性:org.apache.catalina.ASYNC_SUPPORTED :true
Application中添加属性:org.apache.jasper.runtime.JspApplicationContextImpl :org.apache.jasper.runtime.JspApplicationContextImpl@52ccef60
监听HttpSession对象的创建...
Request中添加属性:name :test1
监听HttpSession对象的销毁...
监听ServletRequest对象的销毁...
监听ServletRequest对象的创建...
Request中替代属性:org.apache.catalina.ASYNC_SUPPORTED :true
Request中添加属性:name :test2
监听ServletRequest对象的销毁...
监听ServletRequest对象的创建...
Request中替代属性:org.apache.catalina.ASYNC_SUPPORTED :true
监听HttpSession对象的创建...
监听ServletRequest对象的销毁...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ApplicationListener 是 Spring Framework 中的一个接口,用于监听 Spring 应用程序中发生的事件。它定义了一组回调方法,当应用程序中发生特定事件时,这些方法将被调用。通过实现 ApplicationListener 接口并注册为 Spring Bean,我们可以在应用程序中监听和响应这些事件。 以下是一个 ApplicationListener 的使用案例: ```java @Component public class MyApplicationListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent) { // 应用程序上下文已刷新事件 // 在此处添加自定义逻辑 } else if (event instanceof ContextStartedEvent) { // 应用程序上下文已启动事件 // 在此处添加自定义逻辑 } else if (event instanceof ContextStoppedEvent) { // 应用程序上下文已停止事件 // 在此处添加自定义逻辑 } else if (event instanceof ContextClosedEvent) { // 应用程序上下文已关闭事件 // 在此处添加自定义逻辑 } else { // 其他事件 } } } ``` 在这个例子中,我们创建了一个名为 MyApplicationListener 的 Spring Bean,并实现了 ApplicationListener 接口。我们定义了一个回调方法 onApplicationEvent,它将在任何应用程序事件发生时调用。通过检查事件的类型,我们可以执行不同的自定义逻辑。在这个例子中,我们处理了四个不同的应用程序事件:ContextRefreshedEvent、ContextStartedEvent、ContextStoppedEvent 和 ContextClosedEvent。这些事件分别表示应用程序上下文已刷新、已启动、已停止和已关闭。 要在应用程序中使用 ApplicationListener,我们需要将实现了 ApplicationListener 接口的类注册为 Spring Bean。这可以通过在类上添加 @Component 注解来实现。 总的来说,ApplicationListener 为我们提供了一种在 Spring 应用程序中监听和响应事件的方式,是一个非常有用的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值