The application of Ajax JSON-_ajax in JSP pages related

Source: Internet
Author: User
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 ();
}
}
Related Article

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.