Servlet related Listener application

xiaoxiao2021-03-23  207

CONTEXT range:

ServletContextListener:

Global monitoring for an application. Start with the application, there is two main methods with disappearance with the application:

ContextDestroyed (servletContextevent Event)

Call when the application is closed

ContextInitialized (servletContextevent Event)

Call when the application is started

This listener is mainly used for some work to be completed as an application, that is, many people say I want to be in the container.

Dry when starting ..........

Generally, the "global variable" is generalized, such as

Public void contextinitialized (servletContextevent Event) {

ServletContex sc = event.getServletContext ();

Sc.setttribute (Name, Value);

}

You can get GetServletContext () in any servlet; getaTtribute (name);

I like to use it to use it to make guards, that is, in ContextInitialized (servletContextevent Event)

A Timer is implemented in the method, then let the application work at each time:

Public void contextinitialized (servletContextevent Event) {

Timer = new timer ();

Timer.schedule (new timertask () {

Public void run () {

// Do Any Things

}

}, 0, time interval;

}

Some people say that Timer can only specify how long it is every after the start now, when doing something or during?

One thing, then do I want to do a job at 12 o'clock every month or 12 o'clock?

You just have a daily interval, then every time I judge it, I will do it. For example, every month, then you

The time interval is space, which is 24 hours a loop, and then judges the date new date () in the run method. GetDate () == 1

Just, if it is 12 o'clock a day, then you are scheduled for hours, then judge New Date () in RUN. Gethour ()

== 12, do something to do.

ServletContextAttributeListener:

This listener mainly monitors the events of servletContex objects in SetAttribute () and RemoveAttribute (), pay attention to

That is, a "global variable" is in ADD (First SET), Replace (re-assaped existing variables) and transove.

Call the following three methods separately:

Public void attributeadded (servletContextttributeEvent SCAB) This method can not only know

Which global variables are added, and can get the container to automatically set which context variables when starting:

Public void attributeadded (servletContextAttributeEvent scab) {

System.out.println (Scab.getName ());

}

Public Void AttributeMoved (ServletContextAttributeEvent Scab)

Public void attributeced (servletContextAttributeEvent Scab)

Session range:

HttpSessionListener:

This listener mainly monitors an event that occurs when a session object is generated and destroyed. There are two ways:

Public void sessioncreated (httpsessionever

Public void sessionDestroyed (httpsessioneverment se)

In general, a session object is creathe, you can explain that there is a new customer entry. You can use it to roughly statistically online number, pay attention to this is not accurate, because this client may immediately turn off, but the sessiondestroyed method is pressed for sure

The strategy will happen for a long time.

HttpSessionAttributeListener:

Like ServletContextttributeListener, it monitors ATTRIBUT of a session object by add (a specific

Every time the name is set), the Replace (the value of the name of the ATTRIBUTE is reset) and the event when REMOVE.

There are three ways to do it.

Public void attributeadded (httpsessionBindingEvent SE)

Public void attributemoved (httpsessionBindingEvent SE)

Public Void AttributeReplaced (httpsessionBindingEvent SE)

The method of several listeners above is what happened in the servlet logic in the listener application logic, in general.

We only need to complete the logical function, such as session.setttribute ("AAA", "111"); I just want to put a variable called AAA

Put in session so that I can get it in the future, I don't care when session.settribute ("aaa", "111");

What else should I do. (Of course sometimes use it), but for the listener below, you should give it a good way:

HttpSessionBindingListener:

The listeners above are controlling events as a separate Listener in the container. HttpSessionBindingListener

The state of the object is listened to the state of the object in an object, and if the interface is used as Value is used as a session or from

Remove in Session, it will know that you have been deleted as a session object or have been deleted from Session, this for some non-

Pure Java objects, life cycle longer than session objects, and other objects that need to release resources or changing status.

such as:

Session.SetaTRibute ("abcd", "1111");

After session.removeattribute ("abcd"); because the ABCD is in a character, you will follow the transoint of Remove, it will

Automatically recovered with garbage collectors, and if it is a connection: (just example, you don't add Connection to session

Add in

Session.setttribute ("abcd", conn);

After session.removeattribute ("abcd"); then this CONN is removed from the session, you can't get it

The handle, so you can't close it at all. And before you don't know when you want to be remove, you can't

Close (), then this Connection object is dead. In addition, some objects can be locked when they are added to a session.

It is also necessary to unlock it when it is remove, you should be removed by remove (), add it, I can lock it first.

Add ADD, but then you can't find its handle, it can't be unlocked at all, so these operations can only be implemented in object itself.

That is, when the object is used by add or to notify the object you call the corresponding method:

MyConn Extends Connection Implements httpsessionBindingListener {

Public void valuebound (httpsessionbindingevent se) {

this.initxxx ();

}

Public void valueunbound (httpsessionBindingEvent se) {

THIS.CLOSE ();

}

}

Session.setttribute ("aaa", new myconn ());

At this time, if session.removeattribute ("aaa") is called, it will trigger the valueunbound method to automatically close yourself.

And other objects that need to change the state are the same.

There is also an HTTPSessionActivationListener listener to achieve session synchronization in distributed applications.

Introduce, if you have a friend who wants to achieve this function, you can contact me.

In servlet 2.4, the corresponding listener has been implemented for the Request range:

ServletRequestListener, ServletRequestattributeListener

But there is no support for a good container, so there is no more test. Although you can master 99% from the API, there is no real

Test I will not copy the API only. I will make up this aspect later.

转载请注明原文地址:https://www.9cbs.com/read-130432.html

New Post(0)