JSP page:
Copy Code code 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 ': ',
' DataType ': ' 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 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);
}
}
}
}
}
});
}
Here because this code implements the function is through the AJAX timed access backstage Hashtable, therefore in order to be the system difference URL different, the Ajax return value dissimilarity, specially after the URL adds the R=+math.random ()
Java Background processing method:
Copy Code code as follows:
/**
* Dual Server Nine Sudoku 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 save the page uploaded to the JSP into the map and pass the value through JSON.
class and method of Sendmessage.sendobject (map) method:
Copy Code code 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 ();
}
}