File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Determing if Form Data Changed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Determing if Form Data Changed" Watch "Determing if Form Data Changed" New topic

Determing if Form Data Changed

Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 70

I have a Form with 30 input fields. The form is intitally populated with Data from a Database and user can update one field or 30.. When the update occurs I have to write one record for every field that the user changed ( Nice DB design , I had no part in it ) Is there a easy way to determine which field was updated ? Or do I have to compare each field in my Servlet to figure this out. ( I use to code in CICS (mainframe) and when we needed to determine if a field changed we would check an attribute flag thats why I asked this question)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

Comparing to the original to see what's changed is the first thing that comes to mind. There is no innate concept of 'changed field' in HTTP.

[Asking smart questions] [About Bear] [Books by Bear]
Anuj Batra

Joined: Sep 18, 2011
Posts: 24
you can use javascript to do this.

on lost focus event of form input fields you can check if the input value is changed.
if yes then set a hidden prameter the specifies the value whether yes or no
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

That is a problematic approach as it wil false positive fields that are set back to their original value. If that's not an issue it is one way to approach it but I generally would rather explicitly deal with this on the server rather than implicitly on the client.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19869

If I recall right, the CICS environment would have had the same possibility of false-positives. However I agree that the straight-forward method of comparing the data sent to the browser with the data coming back from the browser is a good way to go.
Ashish Kataria

Joined: Aug 06, 2011
Posts: 14

The problem lies in not to check if each entry has been changed, but rather, in adding a record for each changed record. If the comparison is not done properly, you might end up in adding records for attribute whose values haven't changed(which is a major back end bug).
And as explained by the marshal..this is the first thing that comes to mind.
I would suggest you to go with the tried and tested traditional way.

Everything's tough until you try your best
-Ashish Kataria,
Software Engineer
Consider Paul's rocket mass heater.
subject: Determing if Form Data Changed
jQuery in Action, 3rd edition