Jsp page:
Copy codeThe Code is as follows: $ (document). ready (function (){
SetInterval (function myTimer ()
{
// Alert ('A ');
GetViews ();
},1000 );
});
// Play
Function getViews (){
$. Ajax ({
'Url': "$ {pageContext. request. contextPath}/video/getVideos. action? R = "+ Math. random () +" & open = 1 ",
'Data ':'',
'Ype ype ': 'json ',
'Type': 'get ',
'Error': function (data ){
Alert ("error ");
Return false;
},
'Success': function (data ){
If (null! = Data &&''! = Data ){
// Alert (data. updateFlag );
If (data. updateFlag = 0) {// if data. updateFlag = 0 is not refreshed
// Alert ("data. updateFlag = 0 ");
}
Else {
If (data. videoIds! = Null & data. videoIds! = ""){
Var listIds = data. videoIds;
Var I = 0;
For (; I <listIds. length; ++ I ){
// Alert ("show:" + I + "id =:" + listIds [I]);
ShowView (listIds [I], I); // play
}
For (var j = listIds. length; j <9; ++ j ){
// Alert ("Stop:" + j );
StopPlayVideo (j );
}
}
}
}
}
});
}
The function implemented by this Code is to access the background Hashtable through ajax at regular intervals, so in order to distinguish between different URLs and different ajax return values, add r = + Math. random () after the url ()
Java background processing method:Copy codeThe Code is as follows :/**
* Dual-server nine cells display
*
* @ Return
*/
@ Action (value = "getVideos", results = {
@ Result (name = SUCCESS, location = "videos2.jsp ")
})
Public String getVideos (){
If (open = 301 ){
Return SUCCESS;
} Else {
Try {
VideoHashTable videoHashTable = VideoHashTable. getInstance ();
Hashtable <Long, Long> hashTable = videoHashTable. getRht ();
Map map = new HashMap <String, List <Long> ();
If (videoHashTable. isUpdateFlag () = true ){
Enumeration en = hashTable. keys ();
VideoIds = new ArrayList <Long> ();
While (en. hasMoreElements ()){
Long key = (Long) en. nextElement ();
// Vth. get (key );
VideoIds. add (key );
}
Map. put ("videoIds", videoIds );
Map. put ("updateFlag", 1L );
VideoHashTable. setUpdateFlag (false );
System. out. println ("getVideos:" + videoIds );
SendMessage. sendObject (map );
} Else {
Map. put ("updateFlag", 0L );
SendMessage. sendObject (map );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return null;
}
}
The key is to store the pages uploaded to jsp in map and pass the values through json.
SendMessage. sendObject (map) method class and Method:Copy codeThe Code is as follows: package com. supcon. honcomb. utils;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. http. HttpServletResponse;
Import org. apache. http. HttpResponse;
Import org. apache. struts2.ServletActionContext;
Public class SendMessage {
Public static void sendMessage (String responseText ){
Try {
PrintWriter out = ServletActionContext. getResponse (). getWriter ();
Out. print (responseText );
Out. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
Public static void sendObject (Object obj) throws Exception {
PrintWriter pw;
String rtn = "";
HttpServletResponse response = ServletActionContext. getResponse ();
Response. setContentType ("text/html ");
Response. setCharacterEncoding ("UTF-8 ");
Rtn = JsonUtil. JsonFromObject (obj );
Pw = response. getWriter ();
Pw. write (rtn );
Pw. flush ();
Pw. close ();
}
}