ajax的json传值方式在jsp页面中的应用

2016-02-19 09:39 47 1 收藏

岁数大了,QQ也不闪了,微信也不响了,电话也不来了,但是图老师依旧坚持为大家推荐最精彩的内容,下面为大家精心准备的ajax的json传值方式在jsp页面中的应用,希望大家看完后能赶快学习起来。

【 tulaoshi.com - Web开发 】

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(;ilistIds.length;++i){
//alert("show:"+i+"id=:"+listIds[i]);
showView(listIds[i],i);//播放
}
for(var j=listIds.length;j9;++j){
//alert("Stop:"+j);
StopPlayVideo(j);
}
}
}
}
}
});
}

此处由于此代码实现的功能是通过ajax定时访问后台Hashtable,所以为了是系统区别url不同、ajax返回值不同,特意在url后加上r=+Math.random()
java后台处理方法
代码如下:

/**
* 双服务器九宫格显示
*
* @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();
HashtableLong, Long hashTable = videoHashTable.getRht();
Map map = new HashMapString, ListLong();
if (videoHashTable.isUpdateFlag() == true) {
Enumeration en = hashTable.keys();
videoIds = new ArrayListLong();
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;
}
}

关键要把传到jsp的页面存到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();
}
}

来源:https://www.tulaoshi.com/n/20160219/1591636.html

延伸阅读
标签: Web开发
1. RequestDispatcher.forward() 是在服务器端起作用, 当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个Servlet,JSP 或普通HTML文件,也即你的form提交至a.jsp,在a.jsp用到了forward()重定向至b.jsp,此时form提交的所有信息在 b.jsp都可以获得,参数自动传递. 但forward()无法重定向至有frame的jsp文件,可以重...
标签: Java JAVA基础
JSP中使用jsp forward Action来实现页面的跳转功能。 <jsp:forward page="{relativeURL|<%= expression %>}"/> 或 <jsp:forward page="{relativeURL|<%= expression %>}"> <jsp:param name="parameterName" value="{parameterValue|<%= expression %>}"/>+</jsp:forward> 这个action使您...
标签: Web开发
下面是全部代码,已经编译通过。 Chuandi(传递)是名字空间 WebForm1: %@ Page language="c#" Codebehind="WebForm1.aspx.cs" Inherits="chuandi.WebForm1" % HTML  HEAD   titleWebForm1/title  /HEAD  body   form id="Form1" method="post" runat="server"    asp:TextBox id="TextBox1" runat="serve...
标签: Java JAVA基础
2、开始 现在让我们逐步深入的了解xml在jsp中的应用吧. 首先需要定义tld文件和相应tag处理的java类文件.然后在jsp通过定义的语法使用tag, 让我们来看看下面这个XML文件。 ==================taglib.tld=========================== PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/we...
标签: Web开发
使用JSP大约有下列三种跳转方式: 1. response.sendRedirect(); 2. response.setHeader("Location",""); 3. jsp:forward page="" / 经过试验得到下面的一些规则: 一. response.sendRedirect() 此语句前不允许有out.flush(),如果有,会有异常: java.lang.IllegalStateException: Can't sendRedirect...

经验教程

38

收藏

45
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部