Application of ajax json value passing Method on jsp page

Source: Internet
Author: User

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 ();
}
}

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.