• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Super simple AJAX for Java apps using JQuery and JSON ...

Back in April I wrote about Java to/from JSON serialization using XStream. After developing several AJAX applications using PHP and JQuery, I found that for a lot of scenarios a very simple approach is not only easy but very effective. In the case of PHP, you can effectively enable AJAX in your apps using jQuerys $.getJSON() and PHP json_encode().

I think we can use a very similar approach for Java web applications, enabling the use of AJAX through a very simple, elegant and extensible architecture, without the use of complex frameworks and extra configurations.

To build an example I used XStream to create a utility method that can serialize any POJO or Collection to JSON notation:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.*;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.io.Writer;
 
public class SJJT {
    public static String toJSON(Object obj){
        XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
            public HierarchicalStreamWriter createWriter(Writer writer) {
                return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
            }
        });
        return xstream.toXML(obj);
    }
}

Using it, you can create a JSP or servlet for each feature you want to expose via AJAX and then consume it using jQuerys $.getJSON(). The following example lets the use select a car brand and then fetches a model list using this approach:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>spartanjava.com - Simple JSON sample</title>
        <script type= ext/javascript src=<c:url value=/js/jquery.js/>></script>
        <script type= ext/javascript>
            function listCars(){
                var brand = $("#brand").val();
                $.getJSON("<c:url value=/carHandler/listCars.jsp/>",
                          {"brand": brand},
                          listCarsCallback);
            }
 
            function listCarsCallback(data){
                var listHTML = "";
                $.each(data, function(i, car) {
                    listHTML += "<li>" + car["model"] + "</li>";
                });
 
                $("#carList").html(listHTML);
            }
        </script>
</head>
 
<body>
<form>
    <select id="brand">
        <option>Ferrari</option>
        <option>Porsche</option>
    </select>
    <input type="button" value="List" onclick="listCars();"/>
</form>
 
<ul id="carList">
</ul>
</body>
</html>

You can find the source code of this sample as a complete web app here.

Using this approach you can probably solve most (or all) of the scenarios you will face while enabling AJAX in your Java web apps. Leave a comment on the pro/cons you think this approach has over using one of the mainstream Java AJAX frameworks (i.e. DWR).

 Original Source:
http://www.spartanjava.com/2009/super-simple-ajax-for-java-apps-using-jquery-and-json/

AddThis Social Bookmark Button

Posted at 10:11:15 am | Permalink | Posted in Java  jQuery  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange