[英]Jquery with Servlet to fetch database JSON array object
Stackoverflow team members. Stackoverflow 团队成员。
I am struggling to get the solution of my problem.我正在努力解决我的问题。 Actually in my application I want to make use of Jquery and Json with Servlet.
实际上,在我的应用程序中,我想通过 Servlet 使用 Jquery 和 Json。 In my Application There is one JSP Servlet page to enter detail entry like
user_name
, user_address
etc.在我的应用程序中有一个 JSP Servlet 页面可以输入详细条目,例如
user_name
、 user_address
等。
All this data will be send to database using jquery ajax.所有这些数据将使用 jquery ajax 发送到数据库。 Now I want to retrieve all records that are inserted to database using json in the form of json array object.
现在我想检索使用 json 以 json 数组 object 的形式插入到数据库的所有记录。
I am able to insert record to database but I don't know how to get them back from database to json object array so i can use them again.我能够将记录插入数据库,但我不知道如何将它们从数据库中恢复到 json object 数组,以便我可以再次使用它们。 in some another jsp servlet page.
在另一个 jsp servlet 页面中。
Help me solve my problem.帮我解决我的问题。
Best Regards此致
Yogendra约根德拉
First of all you need to do an Ajax call to your servlet, see following code:首先,您需要对您的 servlet 进行 Ajax 调用,请参见以下代码:
$.getJSON("yourServlet", function(json) {
alert("JSON Received Data: " + json);
//Logic to Parse the received JSON
});
</script>
Secondly construct JSON object at server side with its specific format, something like:其次在服务器端构造 JSON object ,格式如下:
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 732-1234",
"646 123-4567"
]
}
Now Construct a list of inserted records in the database, see following sample code that showing how to construct list of records in JSON:现在在数据库中构造插入记录列表,请参见以下示例代码,该示例代码显示了如何构造 JSON 中的记录列表:
List mybeanList = new ArrayList();
mybeanList.add(myBean1);
mybeanList.add(myBean2);
JSONArray jsonArray = JSONArray.fromObject(mybeanList);
System.out.println("==== : "+jsonArray);
Map map = new HashMap();
map.put("beanlist", jsonArray);
JSONObject jsonObject = JSONObject.fromObject(map);
return jsonObject;
Finally parse the received JSON response inside your jsp (using javascript or any other alternatives)...最后在 jsp 中解析收到的 JSON 响应(使用 javascript 或任何其他替代方案)...
Go through with this tutorial , if you face any trouble with JSON. Go 通过本教程,如果您遇到 JSON 的任何问题。
Well you need to establish a connection with your database server side, pull the records out of the database ( possibly filtering based on data from the ajax request? look at data
attribute for .getJSON
) then your server needs to format this in JSON.那么您需要与您的数据库服务器端建立连接,将记录拉出数据库(可能基于来自 ajax 请求的数据进行过滤?查看
.getJSON
的data
属性)然后您的服务器需要将其格式化为 JSON。 In PHP you'll use json_encode($data_array)
-- then simply echo
this back to the client.在 PHP 中,您将使用
json_encode($data_array)
- 然后简单地将其echo
显给客户端。 For the jQuery request:对于 jQuery 请求:
$.getJSON('http://site.com/ajax/get-latest-posts', {
success: function(jsonObject) {
// jsonObject[0].author, for example.
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.