inicio mail me! sindicaci;ón

With system of the information inside station of listening improvement of Xmlhttp and Java Session

This title contains a lot of concepts that need an explanation, what explain the most easily is ” the message inside the station ” , this is a lot of forum some functions, can send a message to other online users through Web, a lot of users had been used. The first advantage of the message inside the station is everybody does not need to install client end, the MSN that you need not know the other side or QQ, can contact with him, the viewpoint that praises him perhaps gives him curse. The 2nd advantage is client management goes to the lavatory, use Session to maintain online list, all sorts of script had enclosed Session operation very easily used, need not resemble other the instant communication tool that does not have condition (the tool that for instance use UDP corresponds) same, want to expend cell of a few heads to solve the problem of online list. Defect, it is real time sex bad, it is commonly jump in the user turn or exploration message of refresh page ability, newer online list.

Session is monitored, java offerred very agile incident mechanism to monitor Session, can monitor Session found and destroy by melting or burning, monitoring Session place carries data found, change and destroy by melting or burning, the acute that can monitor Session changes He Dun, other platform are a what circumstance I am not quite clear, estimation is about the same also. If can undertake monitoring to the Session of all clients, need not operate troublesome and dangerous Application again.

Xmlhttp is a technology that MS promotes, the function is very complex, can do a lot of businesses, client end is for instance OK HTTP join is opened in simple HTML, request data actively to Server and obtain return data, it is DOM technology a very fundamental application, using it to write the dynamic page that does not have refresh is So Easy simply, the sense that has made the brother that Web develops should understand it has how great.

   One, Session is monitored

Be opposite in Servlet of Session monitor have a lot of interface, the function is very agile, the most commonly used is listening Session and Attribute. Here should clarify a concept, the Session in Servlet is monitored and Attribute listening meaning has a difference, session is monitored those who point to is not us what understand commonly place a Session or destroy by melting or burning a Session, this is the function that Attribute monitors, because Servlet is medium,the grammar that places Session is Session.setAttribute(” Session name ” , want the object) that puts. And Session is monitored, those who monitor is HTTP join, want to have user and Server link only, calculating connective is page of Jsp of a blank, also can spark Session incident, what so the Session of here points to actually is Connection, use count number of current and online user is the equalest. Do not know I said to be clear about. These two kinds of listening way explain respectively below.

1, Session is monitored

Write a Session to monitor above all kind, solid make HttpSessionListener interface, its action is computation has how many online user currently:

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 founds: ” New Java.util.Date());

}

Public Void SessionDestroyed(HttpSessionEvent Se)

{

Count- - ;

System.out.println(” Session destroy by melting or burning: ?new Java.util.Date());

}

Public Static Int GetCount()

{

Return(count);

}

}

Such, be be clear at a glance? Count is defined to be Static, because want to make sure whole system has this one Count only,be. If you are not at ease really, can write it into a single instance kind.

State in Web.xml next this monitor:

<listener>

<listener-class>

Org.bromon.test.SessionCount

</listener-class>

</listener>

Write Test.jsp of page of a test, content is to obtain Count:

<%

Int Count=org.bromon.test.SessionCount.getCount();

Out.println(count);

%>

Those who need an attention is, here involves the operation of any Session far from. Start App Server again, try to join Test.jsp, can see monitor has begun the work.

2, Attribute is monitored

Regard the message inside a station as the system, affirmation should obtain the ID of all person that land, ability is possible each other sends a message. This involves Attribute to monitor. Assume we wrote a module that the user lands, a Session can arise after the user passes identity test and verify, save its pertinent information, for instance:

/ / Check.jsp

<%

String Name=request.getParameter(” Name ” ) ;

Name=new String(name.getBytes(” ISO8859-1 ” ) ) ;

Session.setAttribute(” User ” , name);

%>

The brother that has become Jsp should have not been familiar with this paragraph of code again, a monitor is written to monitor an user to land below, save the ID of all users to a List in the center, this monitor fact makes HttpSessionAttributeListener interface:

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 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 user list:

&ly;%

Java.util.List List=org.bromon.test.OnlineList.getList();

Out.println(” mutual ” List.size() ” renown user already landed: ” ) ;

For(int I=0;I<lise.size();i )

{

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

}

%>

Perhaps you say, what do this have magical, listening Session just, not anxious, see Xmlhttp.

   2, XMLHTTP

The use of XMLHTTP is very much, say we need only here, do not have refresh namely correspond with Server, read this paragraph of code:

<script Language="javascript">

Xml = New ActiveXObject(”Microsoft.XMLHTTP”);

Construction of Var Post=” “;// wants the data that carry

Xml.open(”POST” , xml.open(”POST” ,, Http://localhost:7001/TestWL/index.jsp” , false);//Use POST method opens a join to the server, correspond with asynchronous means

Xml.setrequestheader(”content-length” , post.length);

Xml.setrequestheader(”content-type” , “Application/x-www-form-urlencoded”);

Xml.send(post);// transmits data

Var Res = Xml.responseText;// receives the data that the server returns

Document.write(res);

</script>

Be suddenly enlightened, this paragraph of code opens a HTTP to join namely, transmit data with HTTP format of the standard, if you like, the form that can use XML will deliver. The tectonic way that changes Xml target is mixed with respect to OK and compatible Mozilla Netscape. Will keep a poll below, every other refresh of period of time user list, of course, do not need refresh page:

<html>

<head><title> detector </title>

<script Language="javascript">

Function Detect()

{

Xml = New ActiveXObject(”Microsoft.XMLHTTP”);

Construction of Var Post=” “;// wants the data that carry

Xml.open(”POST” , xml.open(”POST” ,, Http://localhost:7001/TestWL/index.jsp” , false);//Use POST method opens a join to the server, correspond with asynchronous means

Xml.setrequestheader(”content-length” , post.length);

Xml.setrequestheader(”content-type” , “Application/x-www-form-urlencoded”);

Xml.send(post);// transmits data

Var Res = Xml.responseText;// receives the data that the server returns

List.innerText=res;

SetTimeout(” Detect() ” , 5000);// every other 5 seconds of poll

}

</script>

<body Onload= " Detect() " >

<a Id= " List " > </a>

</body>

</html>

Such communication means data bulk is very small, need not deliver whole page afresh, 5 seconds annulus, common PC also can bear larger online number. Tectonic a detector will monitor online list and message, the effect is very good, although your client sits before computer to look on indifferently, key rat is not touched, also can make sure data is delivered immediately, the page also won’t happen jump turn and refresh.

Session is monitored add XMLHTTP communication, develop system of the information inside a relatively perfect station really easy as my eyes.

Ask an author to contact this station, seasonable annotations your full name. Contact mailbox: EDu#chinaz.com (# instead @ ) .

Bookmark:Digg Del.icio.us Reddit

Leave a Comment