Sending data from table to servlet through AJAX via POST request - java

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.
I have set up the request and response via the servlet and it is tested and working for a few variables.
However, I would like to know, whats the best way to send a lot of information, i.e. information from the table? The table could have 30-40 rows with each row having three columns. I need to preserve the information for each row since each row is processed independently of the others.

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.
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.
Hope it helps to solve your problem.

I would like to know, whats the best way to send a lot of information through AJAX
You can use JSON or XML to send the table data that contains records one for each row. You can use various JSON or XML parsing library to form java object back from JSON or XML string and vice-verse.
You can use GSON and JSON library for this purpose.

Related

Technique for server receiving chunked data from client

I'm working with a web application that has a javascript front-end and Tomcat/java server. The front-end is simply a table where the user can modify the table and add more rows and fill out the form. I wrote some code so that the user can save the table. It sends the data in the form to the server through an http request, the server writes the data to an oracle database, everything works great.
As the table grows however, it is no longer feasible to send it all in one request. One suggestion was to split the rows up into different requests, but then if one request succeeds and another fails the oracle table will be incorrect.
What is the preferred method to send the server multiple chunks of data while the server does not begin to work on it until all the data has been received? I've seen plenty of SO questions about TCP connections regarding data chunking, but not really around http requests.
Thanks.
Why are you trying to send the whole table on each request? Just send the difference. I assume that you have ids for the rows or some identifier. Send the list of ids of deleted rows, list of new rows and list of updated rows. That way, you won't have to send all table data.
Do you really need to send the entire table every time user changes it? Why not simply create an API which allows you to modify just the rows which were modified? It seems its much better solution. This way, the client will send only the relevant data, e.g. the single added/modified row.

Pass data from editable HTML table to servlet

So i have a JSP page, that contents editable table with checkbox in each row, the question is, how can i pass the data from selected rows to servlet?
Can i pass it as a parameter, or get html code in servlet to parse it and get selected data?
Thank you.
Your question needs some more specifics. But here is a perfect example of how to implement editable tables:
https://code.google.com/p/jquery-datatables-editable/
It is very easy to implement and basic knowledge of jQuery will help you play around as much as you like. It even shows examples of how to use data at Java servlets.
It depends on when you want pass the values to the servlet. If you want to make all the selections and then submit, a html form submit will be the best option. once you submit the form, the data will be available in request.getParameterValues('names'). If you want to send values immediately after user selects, then you can use ajax through an onchange event

How to page a table generated by an ajax call on runtime

I am generating a table depending on the user's selected choice by calling a servlet using AJAX call onchange of dropdown. The table is generating fine, but the problem is I want to page the table as well as it has a lot of rows. I have tried a lot of jquery plugins, but haven't been able to page the table as most plugins want table id which is only generated on ajax call.Please Help...!
Simply call the paging plugin in the success callback for the AJAX request, since at that point the table will exist and you should know the id of the table. If you don't know the id of the table at that point the easiest solution would be to modify the server-side code to pass that back as part of the response (as a property of a JSON object).
If that's not an option then you'll have to traverse the DOM in order to get a reference to the table, then get the id from that. I'd need to know the HTML structure of the page to tell you exactly how that would look.
You can use the this plugin
http://tablesorter.com/docs/
You can trigger the sorting externally by refering this url
http://tablesorter.com/docs/example-trigger-sort.html

How to dynamic edit a table using a JSP?

I'm trying to set delete buttons on each row of my table in order to let users to delete some rows of the table. However i'm having a terrible time to remove this rows dynamic and remove the data from database... ( no idea how to use the button onclick from html with jsp )
I decided to use some javascript inside the Jsp which did the tricky to remove the row but now i can't delete the entry from the database...
I would like to know if there is anyway to delete data from the database using the javascript and what should be the easiest way to do it ?
If you want to use JavaScript then you'll need to send an AJAX request to your server and then remove the row from the view upon success of the previous action.
For an easy and simple approach to AJAX you can try PrototypeJS. However, if you'd like to have really fancy dynamic features you may be more interested in jQuery.
Without knowing more from your project, I'd suggest to use a Servlet in the server side to receive the AJAX request.
I would like to use Ajax with div and InnerHTML. Onclick to the Remove button I will one javascript function which will make a call to server with unique id to delete the record from database , when we get response back I will hide innerHTML representing that row based on div Id.

JSP delete row and save

I am working in Spring MVC 2, Jsp, Dojo, Javascript.
Actually I am populating Jsp page table-grid with list of objects coming in form command object. Let say 3 records displayed in grid. I am deleting third record with JavaScript getElementById.. delete-row/removeChild functions. That record is deleted from presentation i.e. grid. Now when I save this. It takes 3 records to server side instead of 2. It should take 2 records because the third record was deleted. I am using Dojo to dragNdrop grid rows.
If you're using a grid component that maintains a datastore - e.g. the DojoX DataGrid, you might be removing the markup for the row, but not telling the datastore to purge the row data. When the save occurs, the datastore sends all three rows.
If you are using the DataGrid, you should delete the row from the DataStore, which will be reflected automatically in the UI.
When I have this kind of issue, I always check the cache related headers in my response.
Could it be that the http request supposed to fetch saved data from the server in order to refresh the view doesn't hit the server, but instead hit the browser cache?
Could not resolve issue but another logic fulfills my need. Spring form tags were used to bind this for with objectclass. Converting deleted item row's id to negative and hiding this row at client side does the trick. When form submits this negative id converted to positive value and deleted from DB.

Categories

Resources