Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

JSON Interactions Between Server-Side Java and Client-Side YUI

Learn how to fetch and persist data on the server with a Java server-side implementation, JSON, and the Yahoo! User Interface.


advertisement
ecause it facilitates communication via an interoperable format, JavaScript Object Notation (JSON), the Yahoo! User Interface library (YUI) allows you to implement the server side in any technology. The server will render JSON text for a given request, and YUI parses the JSON response to create JavaScript variables for use in UI widgets.

Using a Java server-side implementation, this article demonstrates the server-side interaction for fetching and persisting data with the YUI library DataTable widget. A number of Java frameworks are available for bridging the Java server-side layer and JavaScript UIs developed with YUI. JAbsorb, an AJAX framework that renders JSON response text to the client, is the framework used in this article.

JSON Breakdown

JSON is a lightweight format for serializing object attributes to be able to transmit from the server to the client or vice versa. Here is a sample:


{"checking": {"balance":879.780029296875, "openedOn":"Sun Jul 19 20:33:30 PDT 2009", "number":123456 } }

This is the JSON representation of the Java class BankAccount:

class BankAccount { public static final int CHECKING = 0; public static final int SAVINGS = 1; private int type; private int number; private float balance; private Date openedOn; }

The server renders this response for a request like this:

http://localhost:8080/pwds/GetBankDetails?id=1

JSON-RPC

JAbsorb is a lightweight AJAX framework for exposing Java objects on the server to JavaScript client programs. It tunnels every method call to the JavaScript proxy object, passes it to the server-side instance, and passes the response back to the JavaScript program.

The demonstration in this article implements a servlet to parse requests from the client program and delegate them to their corresponding server-side functions. Keep in mind that this approach runs the risk of making fine-grained requests to the server and thereby degrading performance.

The servlet JSONServlet is an abstract base class that parses the request, delegates to an AJAX function class, and renders the response returned by the function. The implementation looks like Listing 1.

A typical AJAX function class that uses the JSONServlet will override the execute method to perform the desired operation for the call and return a JSON object to render in the HTTP response. For example, the following function queries a bank account for the given bank ID:

public class GetBankDetails extends JSONServlet { @Override protected JSONObject execute(JSONRequest request) throws JSONServletException, JSONException { String id = request.getArgument("id"); if(id != null) { Bank b = BanksRepository.instance() .getBank(Integer.parseInt(id)); if(b != null) { JSONObject bank = new JSONObject(); BankAccount chkAc = b.getCheckingAc(); JSONObject checking = new JSONObject(); checking.put("number", chkAc.getNumber()); checking.put("balance", chkAc.getBalance()); checking.put("openedOn", chkAc.getOpenedOn()); bank.put("checking", checking); return bank; } } throw new JSONServletException("Unable to get bank details"); } }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap