Without progress bars, refresh regularly to obtain and process server data

Source: Internet
Author: User

After receiving a project, I added a message system similar to bbs. Due to the lack of connectivity of http, the browser needs to regularly access the server to check whether new messages exist. If yes, in the displayed dialog box, refresh the page and display the number of existing messages.

I searched a lot of data on the Internet and tried various methods.

<Meta... Refresh……>

Page embedding in the framework <IFRAME>, JSCode refresh IFRAME SRC

Both methods will cause explicit page refresh, and the effect is not good.

 

Then I used the method for returning the request latency, as follows:

 

<% @ Page contentType = "text/html; charset = GBK" %>
<% @ Page import = "java. util. *" %>
<% @ Page import = "com. tju. msnrl. guard. util. *" %>
<% @ Page import = "com. tju. msnrl. guard. db. util. *" %>

<Html>
<Head>
<Link rel = "stylesheet" href = "css/bottom.css" type = "text/css">
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function RefreshRight (){
Top. bottom_right.location.reload ();
}

Function RefreshMiddle (){
Alert ("You have new messages ");
Top. bottom_middle.location.reload ();
}

Function refreshself (){
Window. Location. Reload ();
}
-->
</SCRIPT>
</Head>

<! --
**************************************** ********************
Determine whether a new message exists,
1) The message listener is set to have new system messages when "User Logon" and "exit"
2) The context global variable is set when the Online User Users "sends the message", "the preliminary review is complete", and "The Final review is complete ".
, Whether there are new messages
Message Processing
1) if there is a new message, the bottom_middle.jsp and bottom_right.jsp pages and the current pages will be refreshed when the page is refreshed to display the updated message.
2) if there is no new message, it will wait for the new message continuously
**************************************** *******************
-->

<Body onload ="
<%
String temp = "";
String temp1 = "";
String temp2 = "";
Boolean needbreak = false;
String username;
While (true) {// cyclically waiting for messages
Thread. Sleep (1000); // thread paused for 1 second
Try {
Messagelistener ML = (messagelistener) application. getattribute ("messagelistener ");
User user = (User) Session. getattribute ("user"); // obtain this user
If (user. getumessagenumber ()> 0 ){
Temp2 = "refreshmiddle ();";
User. setumessagenumber (0 );
Session. setattribute ("user", user );
Needbreak = true;
}
If (Ml! = NULL ){
If (ml. hasNewMarqueeMessage ()){
Ml. NoNewMarqueeMessage ();
Temp1 = "RefreshRight ();";
Needbreak = true;
}
}
If (needbreak ){
Temp = temp1 + temp2 + "RefreshSelf ();";
Application. setAttribute ("messagelistener", ml );
GuardUtil. debug ("refresh the page and display the message ");
Break;
}
}
Catch (Exception e ){
Thread. currentThread (). destroy ();
}
System. out. println ("listening ......");
} // End while
Out. println (temp );
%> "> <! -- End body tag -->
</Body>
</Html>

This method is actually to send a request. If there is no new message, the server will not return it until a new message appears, which leads to a problem between sending and receiving, the page is always a whiteboard, and the status of the page reading is not completed. Therefore, you must place the page in a frame to set its size to 0. In addition, the thread must be destroyed when the user exits or when the user's session times out to prevent connection to the server from occupying resources.

After this method is adopted, because the thread is involved, and generally the web server such as tomcat is not recommended to operate the thread on its own, it may be because this method is used, the server thread is always stopped without any response, as a result, I went on to search and found something. For more information, see the reference article (1). For more information, see the article. Below I will provide a detailed implementation method.

Therefore, I used the ActiveXObject control-based refresh method to return the server segment data through the xml file, as shown below:

The first is the home page: the sender's ame...

<% @ Page contentType = "text/html; charset = GBK" %>
<% @ Page import = "java. util. *" %>
<% @ Page import = "com. tju. msnrl. guard. util. *" %>
<% @ Page import = "com. tju. msnrl. guard. db. util. *" %>

<Html>
<Head>
<Link rel = "stylesheet" href = "css/bottom.css" type = "text/css">
<Script language = "javascript" type = "text/javascript">

<! -- The key lies in this function -->
Function refresh (){
Var oXMLDoc = new ActiveXObject ('msxml ');//Create 'msxml'Object
SURL = "testwindow. jsp ";//Obtain the logon status data address
OXMLDoc. url = sURL; // loadData
Var oRoot = oXMLDoc. root ;//Get returned xmlData Root Node
If (oRoot. children! = Null ){
//Display on the client according to the returned data
Var middleneedrefresh = oRoot. children. item (0). text ;//Obtain the latest value, corresponding to <msg/>
Var rightneedrefresh = oRoot. children. item
1). text; // testwindow. jsp
<Marqueemsg/>
If (rightneedrefresh = "true "){//Refresh the wheel Display message page,More processing available
Top. bottom_right.location.reload ();
}
If (middleneedrefresh = "true "){//Refresh message page
Top. bottom_middle.location.reload ();
Alert ("You have a new message ");
}
}
}

Function dorefresh (){
Var timeoutid = setInterval ("refresh ()", 60000); // refresh every minute
}

</Script>
</Head>

<Body onLoad = "dorefresh ();"> <! -- End body tag -->
</Body>
</Html>

 

The page testwindow. jsp for obtaining server data is as follows and must comply with the xml format:

<% @ Page import = "java. util. *" %>
<% @ Page import = "com. tju. msnrl. guard. util. *" %>
<% @ Page import = "com. tju. msnrl. Guard. DB. util. *" %>
<%

Boolean hasnewmessage = false; // whether a new message exists
Boolean hasnewmarqueemessage = false; // whether a new round-Display message exists

... ... // Embed JSP/ASP code %>

<? XML version = "1.0" encoding = "gb2312"?>
<Msglistener>
<MSG> <% = hasnewmessage %> </MSG>
<Marqueemsg> <% = hasnewmarqueemessage %> </marqueemsg>
</Msglistener>

In this way, we achieved our initial results:

  • 1.Get the latest server data without refreshing the page
  • 2. perform operations on the data, or use ID. innerhtml () to modify the page appearance, or use the message response pop-up window or dialog box to process the page as needed.

However, this has a disadvantage. Using this control requires the client browser to give a low-level security level. Otherwise, the browser may restrict the use of this control, which may not be practical in actual projects.

So I used the current method to regularly refresh <SCRIPT scr =...> to obtain server data. The refresh script SRC browser will display the data in a different way than refreshing the page, that is, refreshing the new status bar. After all, this is not explicitly required by the presentation layer.

The same principle applies to activeobject:

Home Page

<% @ Page contenttype = "text/html; charset = GBK" %>

<% @ Page import = "Java. util. *" %>

<% @ Page import = "com. tju. msnrl. guard. util. *" %>

<% @ Page import = "com. tju. msnrl. guard. db. util. *" %>

<Html>

<Head>

<Link rel = "stylesheet" href = "css/bottom.css" type = "text/css">

<Script language = "javascript" type = "text/javascript">

Var timeoutid;

 

Var rightneedrefresh = true;

Var isTimeOut = true;

Var middleneedrefresh = true;

 

Function refresh2 (){

Dataload. src = "testwindow. jsp ";

Alert ("time:" + isTimeOut );

Alert ("r:" + rightneedrefresh );

Alert ("m:" + middleneedrefresh );

If (isTimeOut ){

ClearInterval (timeoutid );

Alert ("You have timed out and exited. Please log in again! ");

}

If (rightneedrefresh ){

Top. bottom_right.location.reload ();

}

If (middleneedrefresh ){

Top. bottom_middle.location.reload ();

Alert ("You have a new message! ");

}

}

 

Function dorefresh (){

Timeoutid = setInterval ("refresh2 ()", 10000 );

}

 

</SCRIPT>

<SCRIPT id ="Dataload"Language =" JavaScript "type =" text/JavaScript "Defer> </SCRIPT>

</Head>

 

<Body onload ="Dorefresh ();">

</Body>

</Html>

Source code of the testwindow. jsp page

<% @ Page import = "Java. util. *" %>
<% @ Page import = "com. tju. msnrl. Guard. util. *" %>
<% @ Page import = "com. tju. msnrl. Guard. DB. util. *" %>
<%
Boolean hasnewmessage = false;
Boolean hasnewmarqueemessage = false;
Boolean hastimeout = false;
Try {
Messagelistener ML = (messagelistener) application. getattribute ("messagelistener ");
User user = (User) Session. getattribute ("user ");//

If (user. getumessagenumber ()> 0 ){
User. setumessagenumber (0 );
Session. setattribute ("user", user );
Hasnewmessage = true;
}

If (Ml! = NULL ){
If (Ml. hasnewmarqueemessage ()){
ML. nonewmarqueemessage ();
Hasnewmarqueemessage = true;
}

}

If (hasnewmessage | hasnewmarqueemessage ){
Application. setattribute ("messagelistener", ML );
}

} Catch (exception e ){
System. Out. println ("listening error:" + E. getmessage ());
Hastimeout = true;
}

%>

Middleneedrefresh= <%= Hasnewmessage? "True": "false" %>;
Rightneedrefresh= <%= Hasnewmarqueemessage? "True": "false" %>;
IsTimeOut= <%= Hastimeout? "True": "false" %>;

Success!

[Note] I am very grateful to the author of the reference document for giving me the ASP source code. Without his help, I may still be confused. If anyone needs it, leave an email, must be restored ^! ^

[Parameter 1] http://www.itlearner.com/article/2005/1053.shtml

2) http://www.netbei.com/Article/art/art1/200408/816.html

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.