使用session对象进行前后台数据交流之后台向前台传递数据
session; java; jsp; jQuery; javascript;
这里简要介绍一下刚结束的小训练中积累的关于数据传递的知识。
本来想使用ajax进行数据的接收和传递,因为老司机说ajax很方便,相比之下session “只能传很少一点数据,不是很好使”。
所以我就上网去搜索ajax的例子,也找到了一些,其中这个是最promising的:
js部分:
$.ajax({
dataType : "json",
type : "POST",
url : "getPathMap",
success : function(data) {
var points = eval(data);
alert(points[1][0]+points[1][1]);
},
error : function(e, type, msg) {
alert(type + "===" + msg);
}
});
java部分:
public String getPathMap(HttpServletRequest request, HttpServletResponse response) {
PointsInfo pointsInfo = new PointsInfo();
try {
/*** 根据条件取值生成二维数据,并转成json ***/
// JSONArray jsonArray =
// JSONArray.fromObject(pointsInfo.getPointLine(" or id < '20' "));
String[][] aaa = { { "xxxx", "oooo" }, { "fu", "ck" } };
JSONArray jsonArray = JSONArray.fromObject(aaa);
response.setContentType("text/html;charset=UTF-8");
response.setContentType("application/json");
PrintWriter pw = response.getWriter();
pw.print(jsonArray); // 轨迹图条件,取少量数据
pw.flush();
pw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
然而并没有成功移植到我们的训练项目的代码中,鉴于当时时间很紧,任务较多,我们最终没有
get to the bottom of this problem,而是转而去了别的方法,也就是之前老司机不推荐的session。
首先先要声明的是,我对session或不管这技术的名字是什么,都不了解,只是根据参考资料中提到的方法以及提供的例子
编出了可运行的结果,现在到了总结知识的阶段,于是想把解决的方法总结出来。我认为我们使用的技术是session,但我
并不100%确定。请读者见到代码后自行判断。告罪告罪。。。。。(Copyright © . All Rights Reserved)
所谓session,表示客户端与服务器的一次会话。
session对象是一个JSP内置对象。
session对象在第一个JSP页面被装载时自动创建,完成会话期管理。
从客户端打开浏览器并连接到服务器开始,到客户端关闭浏览器离开这个服务器结束,被称为一个会话。
当一个客户访问一个服务器,可能会在服务器的几个页面之间切换,服务器应当通过某种办法知道这是一个客户,就需要session对象。
session对象是HttpSession类的实例。
我们使用的技术是struts2,struts2中的session是一个Map,这个map种保存的是session对象。
好了我自己也说得云里雾里,直接上代码吧!
import java.util.Map;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class [ClassNameMasked] extends ActionSupport{
public String execute() throws REXPMismatchException {
// TODO Auto-generated method stub
RConnection connection = null;
System.out.println("执行包含|包|的脚本");//[comments masked]
System.out.println("[contents masked]");
Map session = ActionContext.getContext().getSession();//关键代码
try{
connection = new RConnection();
connection.eval("source('[path masked]\[filename masked].R')");
int i=0,t,m;
String node[]=connection.eval("[command masked]").asStrings();
m=node.length;
System.out.println("点有"+m+"个");
for(i=0;i<m;i++){
System.out.println(node[i]);
}
String tt[]=connection.eval("[command masked]").asStrings();
t=tt.length;
System.out.println("边有"+t/2+"条");
for(i=0;i<t/2;i++){
System.out.println("from to:"+tt[i]+"->"+tt[i+t/2]);
}
String x="";//连接无向边数组
String y="";//连接有向边数组
for(i=0;i<t;i++){
y=y+tt[i]+"-";
}
String node_str="";//连接节点数组
for(i=0;i<m;i++){
node_str=node_str+node[i]+"-";
}
//以下为关键代码
session.put("aaa", x);//传递无向
session.put("bbb", y);//传递有向
session.put("count", m);//传递节点数
session.put("node_set",node_str);//传递节点
}catch (RserveException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.close();
return SUCCESS;
}
}
对于关键代码中session.put()
的网上的解释是:
取得的getSession()的类型是Map型的。所以只有put()方法。没有setAttribue();
只有在jsp页面中取的时候才用到getAttribue(“result”):同样的,application也是一样的。都是map类型的。
这样即实现了将后台Java方法产生之数据发送到前台为javascript所使用的动作。当然这只是该行为的第一部分,下一部分 为在前台对接收到的数据进行处理并运用。这也是我的下一篇博客将要围绕的内容。