java-implement a listener HttpServletRequest creation destruction, number of people online (56)

Source: Internet
Author: User

Listener Classification in Javaweb

There are three monitored objects in the javaweb:

HttpServletRequest

Httpsessoin

ServletContext

Monitor Listener

By the Listener

Hear Event Object

Httpsessionactivationlistener

httpsession– monitoring of HttpSession activation and Dayton.

Httpsessionevent 

Httpsessionattributelistener [W1]

httpsession– Monitor the properties of the session change. S.setattributee ();

Httpsessionbindingevent 

Httpsessionbindinglistener

HttpSession-Listen to which object is bound to the session. S.setatrri (Name,user);

Httpsessionlistener [W2]

httpsesion– Monitor Sessioin Create destroy

Httpsessionevent

Servletcontextattributelistener

servletcontext– property changes.

Servletcontextlistener

ServletContext Create Destroy

Servletrequestlistener -Serlvetrequestattibutelistner

Rrequest-Create Destroy

Implement the creation of a listener httpservletrequest destroy

First step: Implement a class:

Package Cn.hx.listener;

import javax.servlet.ServletRequestEvent;

import Javax.servlet.ServletRequestListener;

Public class Requestlistener implements Servletrequestlistener {

Public void requestdestroyed (servletrequestevent SRE) {

System. err. println ("Request destroyed:");

Object o = Sre.getsource ();

System. err. println ("This O is:" +o);//[email protected]

}

Public void requestinitialized (servletrequestevent SRE) {

System. err. println ("Request created:");

Object o = Sre.getsource ();

System. err. println ("This O is:" +o);//[email protected]

}

}

Step two: Must be configured to Web. xml

<listener>

<listener-class>cn.itcast.listener.RequestListener</listener-class>

</listener>

Description

1: Configuring a listener only requires the class name to be available.

2: This listener class is automatically initialized when Tomcat is started.

The listner,serlvet,filter created by 3:tomcat are single-column.

Number of people online

As long as someone visits this site, the successful creation of a session is a conversation, a session is an online number.

Use Httpssessionlistener to monitor the creation of the session.

The first step:

Package Cn.hx.listener;

import java.util.ArrayList;

import java.util.List;

import Javax.servlet.ServletContext;

import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpSessionEvent;

import Javax.servlet.http.HttpSessionListener;

/**

* The following statistics are implemented for online people

*/

Public class Mysessionlistener implements Httpsessionlistener {

Private Integer online=1;

Request.getsession ();

Public void sessioncreated (httpsessionevent se) {

System. err. println ("Someone has visited this network");

HttpSession HS1 = (HttpSession) se.getsource ();

HttpSession HS2 = Se.getsession ();

System. err. println ("HS1:" +hs1.getid () + "," +hs2.getid () + ", IP:");

Get an entire domain object

ServletContext sc= se.getsession (). Getservletcontext ();

Sc.setattribute ("online", online++);

Place all sessions in ServletContext

Get all the Sesison that have been maintained from application first

list(list"Sessions");

if (list==null) {//First person to visit

List = new arraylist

Sc.setattribute ("Sessions", list);

}

List.add (HS2);

}

Expired (+), s.invalidate ();

Public void sessiondestroyed (httpsessionevent se) {

System. err. println ("Someone Quit ..." +se.getsession (). GetId ());

ServletContext sc= se.getsession (). Getservletcontext ();

Sc.setattribute ("online", online--);

}

}

java-implement a listener HttpServletRequest creation destruction, number of people online (56)

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.