简体   繁体   English

通过POST请求通过AJAX将数据从表发送到Servlet

[英]Sending data from table to servlet through AJAX via POST request

I have a JSP with a table and once the 'submit' button is pressed I would like to send all the information from the table to a servlet through AJAX via POST request. 我有一个带表的JSP,一旦按下“提交”按钮,我想通过POST请求通过AJAX将表中的所有信息发送到servlet。

I have set up the request and response via the servlet and it is tested and working for a few variables. 我已经通过servlet设置了请求和响应,它已经过测试并且可以处理几个变量。

However, I would like to know, whats the best way to send a lot of information, ie information from the table? 但是,我想知道,发送大量信息(即表中的信息)的最佳方法是什么? The table could have 30-40 rows with each row having three columns. 该表可以有30-40行,每行有三列。 I need to preserve the information for each row since each row is processed independently of the others. 我需要保留每一行的信息,因为每一行都是独立处理的。

I would like to know, whats the best way to send a lot of information through AJAX 我想知道,通过AJAX发送大量信息的最佳方法是什么

You can use JSON or XML to send the table data that contains records one for each row. 您可以使用JSONXML发送包含每个记录一个的表数据。 You can use various JSON or XML parsing library to form java object back from JSON or XML string and vice-verse. 您可以使用各种JSONXML解析库从JSON或XML字符串反过来形成Java对象。

You can use GSON and JSON library for this purpose. 您可以为此使用GSONJSON库。

While adding the rows you can create the tree kind of structure in the frontend. 添加行时,您可以在前端创建树型结构。 That could be some thing like this : 可能是这样的:

Each row will be an object and it has three attribute eg : 每行将是一个对象,并且具有三个属性,例如:

var rows_data = {firstName:"amit", lastName:"kumar", age:28};

Keep adding this rows into the array using javascript. 继续使用JavaScript将这些行添加到数组中。

var rowArray= []; rowArray.push(rows_data);

While posting you can post this javascript array and from the servlet iterate the list and process the results. 发布时,您可以发布此javascript数组,并从servlet迭代列表并处理结果。

Hope it helps to solve your problem. 希望它能帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM