当前位置: 澳门新濠3559 > 编程 > 正文

是一个实现特定接口的普通java类,Servlet中哪些对

时间:2019-10-07 13:18来源:编程
2.属性监听 监听session属性的变化 index.jsp %-- Created by IntelliJ IDEA. User: 马欢欢 Date: 2017/5/30 Time: 10:52 To change this template use File | Settings | File Templates.--%%@ page contentType="text/html;charset=UTF-8" la
2.属性监听
  • 监听session属性的变化

index.jsp

<%-- Created by IntelliJ IDEA. User: 马欢欢 Date: 2017/5/30 Time: 10:52 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>分页查询</title></head><body><h2>Hello World!</h2><h1>欢迎访问index页面</h1><% session.setAttribute("huan","mahuan"); session.removeAttribute;%></body></html>

web.xml

<listener> <listener-class>ed.huan.listener.Session</listener-class> </listener>

SessionAttributeListener.java

package ed.huan.listener;import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;/** * Created by 马欢欢 on 17-6-1. */public class RequestListener implements ServletRequestListener { //对象销毁 public void requestDestroyed(ServletRequestEvent servletRequestEvent) { Object obj = servletRequestEvent.getServletRequest().getAttribute; System.out.println("2.对象销毁" obj); } //对象创建 public void requestInitialized(ServletRequestEvent servletRequestEvent) { Object obj1 = servletRequestEvent.getServletRequest().getAttribute; System.out.println("1.对象创建"  obj1); }}

图片 1

上一篇:过滤器 - 学习笔记

SessionAttributeListener.java

属性监听器

监听:request/session/servletContext对象属性的变化!

(3) session相关监听器

三,程序实现

.xml配置

ServletContextAttributeListener

与前文的request对象监听一样的实现

一、监听器:主要是用来监听特定对象的创建或销毁、属性的变化;是一个实现特定接口的普通java类。

1.生命周期监听器

声明周期监听器: 监听对象的创建、销毁的过程!监听器开发步骤:1.写一个普通java类,实现相关接口;2.配置

  • 监听request对象的创建或者销毁

index.jsp

<%-- Created by IntelliJ IDEA. User: 马欢欢 Date: 2017/5/30 Time: 10:52 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>分页查询</title></head><body><h2>Hello World!</h2><h1>欢迎访问index页面</h1><% request.setAttribute("huan","mahuan");%></body></html>

web.xml

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>Archetype Created Web Application</display-name> <!--监听request对象创建,销毁--> <listener> <listener-class>ed.huan.listener.RequestListener</listener-class> </listener></web-app>

RequestListener.java

package ed.huan.listener;import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;/** * Created by 马欢欢 on 17-6-1. */public class RequestListener implements ServletRequestListener { //对象销毁 public void requestDestroyed(ServletRequestEvent servletRequestEvent) { Object obj = servletRequestEvent.getServletRequest().getAttribute; System.out.println("2.对象销毁" obj); } //对象创建 public void requestInitialized(ServletRequestEvent servletRequestEvent) { Object obj1 = servletRequestEvent.getServletRequest().getAttribute; System.out.println("1.对象创建"  obj1); }}

图片 2

图片 3

HttpSessionAttributeListener

(2)监听对象属性的变化

二.监听器概述

监听器接口:
  • 是一个实现特定接口的普通java类,Servlet中哪些对象需要监听。监听对象创建/销毁的监听器接口Interface ServletRequestListener 监听request对象的创建或销毁Interface HttpSessionListener 监听session对象的创建或销毁Interface ServletContextListener 监听servletContext对象的创建或销毁
  • 监听对象属性的变化Interface ServletRequestAttributeListener 监听request对象属性变化: 添加、移除、修改Interface HttpSessionAttributeListener 监听session对象属性变化: 添加、移除、修改Interface ServletContextAttributeListener 监听servletContext对象属性变化
  • session相关监听器Interface HttpSessionBindingListener 监听对象绑定到session上的事件Interface HttpSessionActivationListener 监听session序列化及反序列化的事件

测试结果

ServletRequestAttributeListener

五、统计在线人数。实战

一.监听器作用:

主要是用来监听特定对象的创建或销毁、属性的变化的! 是一个实现特定接口的普通java类!Servlet中哪些对象需要监听?request /session /servletContext分别对应的是request监听器、session相关监听器、servletContext监听器

package entity;
import javax.servlet.http.*;

/**
 * Created by pc on 2017/9/12.
 */
public class SessionAttributeListener implements HttpSessionAttributeListener {

    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("创建session");
    }

    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("移除session");
    }

    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("修改session");
    }
}

         Interface HttpSessionListener         监听session对象的创建或销毁

package entity;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

/**
 * Created by pc on 2017/9/12.
 */
public class RequestListener implements ServletRequestListener {
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
       Object object = servletRequestEvent.getServletRequest().getAttribute("name");
        System.out.println("2.对象销毁" object);
    }

    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        Object object1 = servletRequestEvent.getServletRequest().getAttribute("name");
        System.out.println("1.对象创建" object1);
    }
}
生命周期监听器

声明周期监听器: 监听对象的创建、销毁的过程!
监听器开发步骤:

  1. 写一个普通java类,实现相关接口;
  2. 配置(web.xml)

        Interface HttpSessionActivationListener(了解)  监听session序列化及反序列化的事件

一 监听器作用
主要是用来监听特定对象的创建对象或销毁,属性的变化的!是一个实现特定接口的普通java类。
Servlet中哪些对象需要监听?
request/session/servletContext
分别对应的request监听器,session相关监听器,servletContext监听器
二 监听器(listener)概述
监听器接口

Servlet中哪些对象需要监听?
  • request / session / servletContext
    • 分别对应的是request监听器、session相关监听器、servletContext监听器

         Interface HttpSessionAttributeListener      监听session对象属性变化: 添加、移除、修改

RequestListener.java

ServletRequestListener

监听request对象的创建或销毁

public class MyRequestListener implements ServletRequestListener {
    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent){
        System.out.println("MyRequestListener.requestDestroyed()");
    }

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent){
        System.out.println("MyRequestListener.requestInitialized()");
    }
}

    <listener>
        <listener-class>com.flynn.MyRequestListener</listener-class>
    </listener>

  (2)配置(web.xml)

运行结果

ServletContextListener

监听servletContext对象的创建或销毁

 

.xml配置

监听器(listener)

监听器接口:

  • 监听对象创建/销毁的监听器接口

    • Interface ServletRequestListener 监听request对象的创建或销毁
    • Interface HttpSessionListener 监听session对象的创建或销毁
    • Interface ServletContextListener 监听servletContext对象的创建或销毁
  • 监听对象属性的变化

    • Interface ServletRequestAttributeListener 监听request对象属性变化: 添加、移除、修改
    • Interface HttpSessionAttributeListener 监听session对象属性变化: 添加、移除、修改
    • Interface ServletContextAttributeListener 监听servletContext对象属性变化
  • session相关监听器

    • Interface HttpSessionBindingListener 监听对象绑定到session上的事件
    • Interface HttpSessionActivationListener(了解) 监听session序列化及反序列化的事件

  分别对应,request监听器、session相关的监听器、servletContext监听器。

HttpSessionListener

监听session对象的创建或销毁。

  request/session/servletContext

<%--
  Created by IntelliJ IDEA.
  User: pc
  Date: 2017/9/6
  Time: 17:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>路径问题</title>
</head>
<body>
<h2>欢迎访问index页面</h2>
<%
    session.setAttribute("name","YY");
    session.removeAttribute("name");
%>
</body>
</html>

用来监听特定对象的创建或销毁、属性的变化的!
是一个实现特定接口的普通java类!

四、监听器的开发步骤

2 属性监听(监听session变化)
index.jsp

   Interface ServletContextAttributeListener   监听servletContext对象属性变化

图片 4

  (1)编写普通java类,实现相关的接口

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <listener>
    <listener-class>entity.SessionAttributeListener</listener-class>
  </listener>
</web-app>

   Interface HttpSessionBindingListener             监听对象绑定到session上的事件    

  • 监听器对象创建/销毁的监听器接口
    Interface ServletRequestListener 监听request对象的创建或销毁
    Interface HttpSessionListener 监听session对象的创建或销毁
    Interface ServletContextListener 监听servletContext对象的创建或销毁
  • 监听对象属性的变化
    Interface ServletRequestAttributeListener 监听request对象属性变化: 添加、移除、修改
    Interface HttpSessionAttributeListener 监听session对象属性变化: 添加、移除、修改
    Interface ServletContextAttributeListener 监听servletContext对象属性变化
  • session相关监听器
    Interface HttpSessionBindingListener 监听对象绑定到session上的事件
    Interface HttpSessionActivationListener(了解) 监听session序列化及反序列化的事件
    三 程序实现
    1 生命周期监听
    index.jsp

         Interface ServletContextListener     监听servletContext对象的创建或销毁

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
<listener>
  <listener-class>entity.RequestListener</listener-class>
</listener>
</web-app>

三、监听器的接口

<%--
  Created by IntelliJ IDEA.
  User: pc
  Date: 2017/9/6
  Time: 17:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>路径问题</title>
</head>
<body>
<h2>欢迎访问index页面</h2>
<%
    request.setAttribute("name","Yang");
%>
</body>
</html>

二、servlet中哪些对象需要监听?

(1)监听对象的创建/销毁

     Interface ServletRequestAttributeListener  监听request对象属性变化: 添加、移除、修改

   Interface ServletRequestListener     监听request对象的创建或销毁

编辑:编程 本文来源:是一个实现特定接口的普通java类,Servlet中哪些对

关键词: 澳门新濠3559