Use XMLHTTP and Java Session to listen to improved station messages

xiaoxiao2021-03-06  79

Use XMLHTTP and Java Session to listen to improved station messages

Date: June 21 @ 10:44:48 CST Article theme: Programming

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. Using XMLHTTP and Java Session, Issue Www.linuxfans.org, please respect the copyright, the topic is included with the concept that needs to be explained, and it is the easiest to explain that "station messages", this is a lot of forums. You can send messages to other online users through the web, and many users are 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. 1. Sensions monitors a lot of interfaces in the session monitoring, the function is flexible, and most commonly used is listening to 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 monitor first write a session monitor class, actually httpsessionListener interface, its role is how many online users currently: code: / * * @ Author bromon * 2004-6-12 * / package org.bromon.test ..; import javax.servlet *; import javax.servlet.http *; public class SessionCount implements HttpSessionListener {private static int count = 0; public void sessionCreated (HttpSessionEvent se) {count ; System.out.println ( "session created: " New java.util.date ());} public void sessiondestroyed (httpsessionEvent se) {count -; system.out.println (" session destruction: " new java.util.date ());} public static INT getCount () {return (count);}} how is it? 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:

Code: Org.bromon.test.Sessioncount Write a test page Test.jsp, content is COUNT:

Code: <% int count = org.bromon.test.sessioncount.getCount (); out.println (count);%> Need to note that this is not involved in any session operation. Start App Server and try to connect Test.jsp, you can see that the listener has begun to work. 2, Attribute listens to a station message system, it is necessary to get all the landmen's ID, 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:

Code: //check.jsp <% string name = request.getParameter ("name"); name = new string (name.getbytes ("ISO8859-1"); session.setttribute ("user", name);% > 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 actually httpsensettributeListener interface:

Code: / * * @ Author bromon * 2004-6-12 * / package org.bromon.test; import javax.servlet. *; Import javax.servlet.http. *; Import java.util. *; Public class onlineLinLIST IMPLEMENTS HttpSessionAttributeListener {private static List list = new ArrayList (); public void attributeAdded (HttpSessionBindingEvent se) {if ( "user" .equals (se.getName ())) {list.add (se.getValue ());}} public void attributeRemoved (HttpSessionBindingEvent se) {if ( "user" .equals (se.getName ())) {list.remove (se.getValue ());}} public void attributeReplaced (HttpSessionBindingEvent se) {} public static List getList ( ) {return (list);}} Write a simple JSP to get a list of users: code: <% java.util.list list = org.bromon.test.onLineLinList.getlist (); out.println ("Again" List.size () "name users have been logged in:"); for (int i = 0; i maybe you Say, what is this magical? Monitor session, don't worry, look at XMLHTTP. Second, XMLHTTP XMLHTTP is a lot, here only say that we need, is no refreshing with Server communication, see this code:

Code: