JSP和Servlet之间的通信? [英] Communication between JSP and Servlet?

查看:144
本文介绍了JSP和Servlet之间的通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与servlet后端通信的jsp页面.到目前为止,我与该servlet进行通信的方式是通过.getJSON()(这是一种JQuery方法)进行.如果我要发送回的数据采用{key:value}的形式,则效果很好.但是,现在我需要发送更多的数据.我数据库中最大的表包含大约11个属性,并且行数约为20-40.通过JSON发送表不大,但也不小.我正在考虑XML,我想知道是否有人可以给我一些启发.示例代码将不胜感激,链接到教程,文章也将很棒.

I have a jsp page that communicate with a servlet back end. Up until now, the way I communicate with that servlet is via .getJSON() which is a JQuery method. This work great if the data I want to send back is in the form of {key:value}. However, now I need to send a bit more data then that. The largest table in my database, contain roughly eleven attributes, and the number of row is about 20-40. It is not big, but not small to send table via JSON. I am think about XML, and I wonder if any one can shed me some light. Sample codes would be appreciated, link to tutorial, article would be awesome as well.

推荐答案

只需将数据保存在完全有价值的Javabean的集合或地图中,并使用

Just have the data in a collection or map of fullworthy Javabeans and make use of Google Gson to convert it to JSON without any pains. JSON is more compact than XML and much easier to process in JavaScript (it's also the JavaScript Object Notation).

在Gson的帮助下,您所需要做的基本上是:

All you basically need to do with help of Gson is the following:

List<Data> list = dataDAO.list();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Gson().toJson(list));

仅此而已.我之前用示例回答过几次:此处此处此处.

That's all. I've answered this several times before with examples: here, here, here, here and here.

这篇关于JSP和Servlet之间的通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆