Populating Text Fields From JSON String

Continuing from our first scenario where we presented a Welcome to the user Terrance Streeter, this example demonstrates how we allow the same user to look at their own Profile page where they can view their personal data and update it as necessary. For the sake of simplicity, we will obtain only the users first and last name, their email address and password. These are typically what you allow the user to make changes on for a Profile page. We will not perform any field validation for this example as that would detract from the lesson.

Look under the JavaScript tab and note, once again, we will provide an ID value for the user as we use an AJAX request to a Java Server Page. We supply an additional parameter named "action" whose value is "select". This signifies to the JSP that we just want to perform a lookup on the database table and return the users data. For the click event on the Save button we perform another AJAX request, however this time the action value is changed to "update" signifying that we want the JSP to update the database table with new data. For this example, you can only allow for a change to the email address or password. Unfortunately with the password field you can't really see the characters, only dots, but you can count the number of dots to ensure you have the right number of characters. Or you can look at the JSON string as it arrives in FireBug. You can refresh the page to prove that the changes took affect.

Under the Java tab, you can see how we branch off the business logic by testing for the value of the "action" parameter. For a more sophisticated setup, your web application could allow for other actions such as "delete" if the user didn't want to be a member on your website any longer. Note how we take advantage of the GSON class, which is a dependency for JED, to create a JSON string and return it to the client side.

For our controller JSP, also note that we are introducing three additional objects: the Query class, the Field class, and the WhereCondition class. The Query class is responsible for assembling the correct syntax for SQL statements: SELECT,INSERT,UPDATE,DELETE. If we want to perform an UPDATE, we will need to involve instances of all these classes along with the Database class. The comments in the JSP will explain how they all come together.


First Name: 
Last Name: 
Email Address: