Java-web Listener

Source: Internet
Author: User

To implement the listening essence on the Web side:

Implementation of a series of listening interface (to implement the corresponding interface, overwrite the corresponding methods in each interface, the corresponding event triggered by the execution of their own listener in the overwrite method, in each method to complete their desired operation, thus realizing the monitoring)

Monitoring-is the setting, modification, and substitution of related values in a certain range (application,session,request), which trigger events, and the agent mechanism of events in Java, event handling is the use of the listener mechanism, So in order to be able to take the corresponding measures when the event is triggered, it is necessary to----> inherit such listener, overwrite the corresponding method in the listener, overwrite the corresponding event processing method, and deal with the corresponding event in the corresponding method, that is, listen ( On-line summary of the particularly good)

  Summary: Mainly used to listen to the creation or destruction of a particular object, property changes, the need to implement a specific interface of the ordinary class.

First-off interface

I. Listener interface for monitoring object creation/destruction

Interface Servletrequestlistener Monitoring the creation or destruction of request objects

Interface Httpsessionlistener Monitoring Session object creation or destruction

Interface Servletcontextlistener monitoring ServletContext object creation or destruction

Second, the change of listener object property

Interface Servletrequestattributelistener Listener Request object property changes: Add, remove, modify

Interface Httpsessionattributelistener Listener Session object property changes: Add, remove, modify

Interface Servletcontextattributelistener Listener ServletContext object property changes

Third, session related listener

Interface Httpsessionbindinglistener Listener objects are bound to events on the session

Interface Httpsessionactivationlistener Monitoring session serialization and deserialization events

Life cycle

Steps to use the listener

1, write a common Java class, the implementation of first-off interface

2. Do not live XML files

A, Servletrequestlistener (JSP method in Sevlet the parameter passed in is ServletRequest, in the httpservlet it is forced to transform, So there is no Httpservletrequestlistener related interface, use the most basic servletrequest)

Creating the corresponding implementation class

Package Comgqx.a_life;import Javax.servlet.servletrequestevent;import javax.servlet.servletrequestlistener;/** * Listen for creation and destruction of request objects * @author Administrator */public class Myrequestlistener implements servletrequestlistener{@ overridepublic void requestdestroyed (Servletrequestevent sre) {//TODO auto-generated method stub// Gets the data stored in the Request Object Obj=sre.getservletrequest (). getattribute ("cn"); System.out.println (obj); System.out.println ("myrequestlistener.requestdestoryed ()");} @Overridepublic void requestinitialized (Servletrequestevent sre) {//TODO auto-generated method stub// Gets the data stored in the Request Object Obj=sre.getservletrequest (). getattribute ("cn"); System.out.println (obj+ "hhhhh"); System.out.println ("myrequestlistener.requestinitialized ()");}}

When the session object is forced to close the browser, not a Session object is destroyed, can only be said to be useless, so it will not trigger the destruction event

Package Comgqx.a_life;import Javax.servlet.http.httpsessionevent;import Javax.servlet.http.HttpSessionListener; public class Mysessionlistener implements httpsessionlistener{//create destroy @overridepublic void sessioncreated ( Httpsessionevent se) {//TODO auto-generated method StubSystem.out.println ("mysessionlistener.sessioncreated ()");} @Overridepublic void sessiondestroyed (httpsessionevent se) {//TODO auto-generated method StubSystem.out.println (" Mysessionlistener.sessiondestroyed () ");}}

  

Package Comgqx.a_life;import Javax.servlet.servletcontextevent;import Javax.servlet.servletcontextlistener;public Class Myservletlistener implements servletcontextlistener{//is generally created when the server is started, the server is shut down and destroyed, with relatively many @overridepublic void Contextdestroyed (servletcontextevent arg0) {//TODO auto-generated method StubSystem.out.println (" Myservletlistener.contextdestroyed () ");} @Overridepublic void contextinitialized (Servletcontextevent arg0) {//TODO auto-generated method StubSystem.out.println ("1........myservletlistener.contextinitialized");}}

Write the configuration file.

<!--the creation of the listener Request Object-  <listener>  <listener-class>comgqx.a_life. myrequestlistener</listener-class>  </listener>  <!--monitoring ServletContext object Creation--  < Listener>  <listener-class>comgqx.a_life. myservletlistener</listener-class>  </listener>  <!--Listener Session object Creation--  < Listener>  <listener-class>comgqx.a_life. Mysessionlistener</listener-class>  </listener>

Write a JSP to test

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

You will see the creation of the Servlet object when you start the server

Then through the JSP to access, the console display as follows

Java-web Listener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.