Listener Listener __web in Javaweb

Source: Internet
Author: User
Tags sessions tomcat server

1, listening domain objects and properties

A, monitoring ServletContext creation and destruction implementation Interface Servletcontextlistener---> Listening domain object properties: Servletcontextattributelistener

Objects are created when the server is started and are destroyed when the server is shut down.

B, listening httpsession creation and destruction implementation Interface Httpsessionlistener---> Listening domain object properties: Httpsessionattributelistener

Object creation: Httpsessionsession=request.getsession ();

Session destroy 1. The default timeout is 30 minutes 2. Shutdown Server 3.invalidate () method 4.setMaxInactiveInterval (intinterval) can set timeout time

C, listening httpservletrequest creation and destruction Implement Interface Servletrequestlistener---> Listening domain object properties: Servletrequestattributelistener

The request object is created when the requesting server is sent, and when the response is generated, the request object is destroyed.

2, listening to the session binding JavaBean

Used to listen whether the JavaBean object is bound to the session domain Httpsessionbindinglistener

Httpsessionbindinglistener this interface is implemented by JavaBean and does not need to be registered in the Web.xml file.

The interesting thing is that when we implement the Serializable,httpsessionbindinglistener interface in the entity class, we session in the JSP

        person who = new person ();
    	Session.setattribute ("person", person);
Starts the Tomcat server, shuts down the Tomcat server, generates Sessions.ser in the Tomcat directory, and disappears when the server file is restarted (data recovery).

Activation and passivation Httpsessionactivationlistener for monitoring JavaBean objects

Httpsessionactivationlistener If JavaBean implements this interface, then when we shut down the server gracefully, the JavaBean object in session will be deactivated to the file we specified.

The next time the server is started, because we have written the object to the file, the JavaBean object is automatically activated to the session.
3, the use of the listener

A. Create a class that implements the specified listener interface

B. Overriding methods in an interface

C. Register the Listener in the Web.xml file.

3, the implementation of scheduled destruction session

Thought: Listens for session creation time, when session is created, puts a collection, periodically scans the collection, listens for sevletcontext creation time, and creates the collection when ServletContext is created.

Nonsense is less than the actual code to:

Listens for ServletContext and creates a collection of stored HttpSession objects when the ServletContext object is created

Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Timer;
Import Java.util.TimerTask;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;

Import javax.servlet.http.HttpSession; public class Testservletcontextlistener implements Servletcontextlistener {public void contextinitialized (
		Servletcontextevent SCE) {//Create List collection final listMonitor HttpSession

Import java.util.List;
Import Javax.servlet.ServletContext;
Import javax.servlet.http.HttpSession;
Import javax.servlet.http.HttpSessionEvent;
Import Javax.servlet.http.HttpSessionListener;
public class Testhttpsessionlistener implements Httpsessionlistener {

    //When HttpSession is created, put in the collection in ServletContext Public
    void sessioncreated (httpsessionevent se) {
        //HttpSession object into List
        HttpSession session = Se.getsession ();
        The ServletContext object---session can get the context object
        ServletContext application = Session.getservletcontext ();
        Gets the list set
        list

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.