溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ajax的json傳值方式在jsp頁面中的應(yīng)用是怎樣的

發(fā)布時(shí)間:2021-10-08 14:32:56 來源:億速云 閱讀:123 作者:柒染 欄目:web開發(fā)

本篇文章給大家分享的是有關(guān)ajax的json傳值方式在jsp頁面中的應(yīng)用是怎樣的,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

jsp頁面

 代碼如下:


$(document).ready(function() {
setInterval(function myTimer()
{
//alert('a');
getViews();
},1000);
});
//播放
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){//如果data.updateFlag=0 不刷新
//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);//播放
}
for(var j=listIds.length;j<9;++j){
//alert("Stop:"+j);
StopPlayVideo(j);
}
}
}
}
}
});
}


此處由于此代碼實(shí)現(xiàn)的功能是通過ajax定時(shí)訪問后臺Hashtable,所以為了是系統(tǒng)區(qū)別url不同、ajax返回值不同,特意在url后加上r=+Math.random()
java后臺處理方法

 代碼如下:


/**
* 雙服務(wù)器九宮格顯示
*
* @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;
}
}


關(guān)鍵要把傳到j(luò)sp的頁面存到map中,在通過json傳值。
SendMessage.sendObject(map)方法所在類及方法

 代碼如下:


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

以上就是ajax的json傳值方式在jsp頁面中的應(yīng)用是怎樣的,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI