Use XMLHTTP and Java Session to listen to improved station messaging systems

zhaozj2021-02-16  33

Bromon original, please respect the copyright

This topic contains many concepts that need to explain, the most primely manifest is "station messages", which is a function of many forums, which can send messages to other online users through the web, and many users have used. The first advantage of the station in the station is that you don't need to install the client. You don't have to know the MSN or QQ of the other party, you can contact him, praise his views or give him a stinky. The second benefit is that the customer management is convenient, using session to maintain online list, various scripts have been encapsulated by the session operation, without having to have other instant messaging tools (such as tools using UDP communication), I have to pay some brain cells to solve the problem of online listings. Disadvantages, it is not good in real time, usually in user jump or refresh the page to detect messages, update online lists.

SESSION monitors, there is nothing to explain, Java provides a very flexible event mechanism to monitor session, can monitor the creation, changes and destruction of session, monitor the creation, changes, and destruction of data from Session, can monitor the sharpening and passivation of session (Understanding the sequence of objects, you should know this), what happens to other platforms, I don't know much, it is estimated. If you can monitor all customers, you don't have to operate trouble and dangerous Application.

XMLHTTP is a technique pushed by MS. It can do a lot of things. For example, the client can open HTTP connections in simple HTML, actively request data to Server and get returns data, is a very important application of DOM technology, Using it to write a refreshed dynamic page is SO Easy, and the brothers who have done Web should understand how significant it is.

First, session monitoring

There are many interfaces to session in the servlet, and the features are flexible. The most commonly used listening session and Attribute. Here is the concept, the session monitoring and Attribute monitoring meaning in the servlet is different. The session listening refers to what we generally understand or destroy a session, which is the function of Attribute, because the syntax of the session in the servlet It is Session.setttribute ("Session Name", the object to be placed). And the session listening, listening is an HTTP connection. As long as there is a user with the Server connection, even if the connection is a blank JSP page, the session event is also triggered, so the session here is actually Connection, used to count the current online The number of users is most suitable. I don't know if I said it clearly. Below, these two listening methods are explained.

1, session monitoring

First, write a session listening class, which is the HTTPSessionListener interface. Its role is to calculate how many online users current:

/ *

* @Author bromon

* 2004-6-12

* /

Package org.bromon.test;

Import javax.servlet. *;

Import javax.servlet.http. *;

Public Class Sessioncount IMPLEments HTTPSESSIONLISTENER

{

Private statin = 0;

Public void sessioncreated (httpsessionever

{

COUNT ;

System.out.println ("SESSION Creation:" New Java.util.date ());}

Public void sessionDestroyed (httpsessioneverment se)

{

count -;

System.out.println ("Session destruction:" new java.util.date ());

}

Public static int getCount ()

{

Return (count);

}

}

How is it, is it a glimpse? Count is defined as static because it is necessary to ensure that only this COUNT is available throughout the system. If you are really unassay, you can write it into a single case.

Then declare this listener in Web.xml:

Org.bromon.test.SessionCount

Write a test page Test.jsp, the content is obtained in COUNT:

<%

INT count = org.bromon.test.sessioncount.getcount ();

Out.println (count);

%>

It should be noted that this is not involved in any session operation here. Start App Server and try to connect Test.jsp, you can see that the listener has begun to work.

2, Attribute listens

As a station message system, it is necessary to get all the login IDs, it is possible to mess up. This involves Attribute listens. Suppose we write a user login module, the user will generate a session after authentication, save its related information, such as:

//check.jsp.jsp

<%

String name = Request.getParameter ("name");

Name = new string (name.getbytes ("ISO8859-1"));

Session.setttribute ("User", Name);

%>

The brothers who have done JSP should be familiar with this code, and write a listener to listen to the user to log in, save all users to a list, this listener is on the HTTPSessionATtributeListener interface:

/ *

* @Author bromon

* 2004-6-12

* /

Package org.bromon.test;

Import javax.servlet. *;

Import javax.servlet.http. *;

Import java.util. *;

Public Class OnlineList Implements httpsessionattributeListener

{

Private static list list = new arraylist ();

Public void attributeadded (httpsessionBindingEvent SE)

{

IF ("User" .equals (se.getname ())))

{

List.add (se.getValue ());

}

}

Public void attributemoved (httpsessionBindingEvent SE)

{

IF ("User" .equals (se.getname ())))

{

List.remove (se.getValue ());

}

}

Public void attributeced (httpsessionBindingEvent SE) {}

Public static list getList () {

Return (List);

}

}

Write a simple JSP to get a list of users:

<%

Java.util.list List = Org.bromon.test.onLineList.getlist ();

Out.println ("Again" list.size () "name user has logged in:");

For (int i = 0; i

{

Out.println (List.get (i));

}

%>

Maybe you said, what is this magical, listening to sessions, don't worry, look at XMLHTTP.

Second, XMLHTTP

There are a lot of XMLHTTP. Here is just that we need, that is, no refreshing communication with Server, see this code: