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/
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.
