You are currently browsing the monthly archive for February 2009.
I found this useful linke: Creating & Parsing JSON data with Java Servlet/Struts/JSP to teach you how-to in 5 mins. But it didn’t explain how-to at client side (browser). I post the sample javascript code.
=====================
util.js
=====================
window.onload = initPage;
function initPage() {
// find the thumbnails on the page
thumbs = document.getElementById("thumbnailPane").getElementsByTagName("img");
// set the handler for each image
for (var i = 0; i < thumbs.length; i++) {
image = thumbs[i];
// create the onclick function
image.onclick = function() {
// find the image name
detailURL = 'images/' + this.title + '-detail.jpg';
document.getElementById("itemDetail").src = detailURL;
getDetails(this.title);
}
}
}
function createRequest() {
try {
request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
function getDetails(itemName) {
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "getDetails.php?ImageID=" + escape(itemName);
request.open("GET", url, true);
request.onreadystatechange = displayDetails;
request.send(null);
}
function displayDetails() {
if (request.readyState == 4) {
if (request.status == 200) {
var detailDiv = document.getElementById("description");
//get json object
var itemDetails = eval('(' + request.responseText + ')');
// Remove existing item details (if any)
var children = detailDiv.childNodes;
for (var i=children.length; i>0; i--) {
detailDiv.removeChild(children[i-1]);
}
// Add new item details
for (var property in itemDetails) {
var propertyValue = itemDetails[property];
if (!isArray(propertyValue)) {
var p = document.createElement("p");
p.appendChild(
document.createTextNode(property + ": " + propertyValue));
detailDiv.appendChild(p);
...
}
The json data will get from XMLHttpRequest.requestText, and use javascript built-in eval() to parse into object. Also you need some knowledge of DOM.
update: 03-22-2009
1. Java Tools for the JSON Format
2. JSON in Java
4. RPC in Javascript using JSON-RPC-Java
5. jabsorb is a simple and lightweight Ajax/Web 2.0 framework that allows you to call methods in a Java web application from JavaScript code running in a web browser as if they were local objects residing directly in the browser. link here
6. Download: JRP (JSON-RPC Page) is a JSP tag library for creating RPC methods for both JSON-RPC and XML-RPC
update: 05-08-2009
Gson is a Java library that can be used to convert Java Objects into its JSON representation.
People often describe Ajax applications as Web pages that don’t need to perform a full page refresh on every click.
This description is accurate, but the underlying motivation is that full page refreshes are distracting and detract
from an enjoyable, immersive user experience. Full page refreshes are even nastier from an architectural point of view
in that they eliminate the option of storing application state in the client, which in turn results in design decisions
that prevent applications from taking advantage of many of the Web’s strongest architectural design points.
The fact that Ajax lets you interact with a server without a full refresh puts the option of a stateful client back on the table.
This has profound implications for the architectural possibilities for dynamic immersive Web applications:
Because application resource and data resource binding is shifted to the client side,
these applications can enjoy the best of both worlds — the dynamic, personalized user experience
we expect of immersive Web applications and the simple, scalable architecture we expect from RESTful applications.
From Ajax and REST, Part 1 and 2.
Here is how to implement it…
1. AJAX patterns: RESTful Service
RESTFul is centered around two basic principles:
- Resources as URLs. A resource is something like a “business entity” in modelling lingo. It’s an entity you wish to expose as part of an API. Almost always, the entity is a noun, e.g. a person, a car, or a football match. Each resource is represented as a unique URL.
- Operations as HTTP methods. REST leverages the exising HTTP methods, particularly GET, POST, PUT, and DELETE. Note that the XMLHttpRequest object, and some wrapper libraries.
1. RESTful Web Services
Read this article to understand concept of RESTFul and especially the table of HTTP request and HTTP response messages. But you should ignore the code implement because we are going to use JAX-WS or JAX-RS.
2. How to use JAX-RS
Implementing RESTful Web Services in Java
Configuring JSON for RESTful Web Services in Jersey 1.0
REST web services with JAXB, JAX-RS and Sun Jersey
3. RESTFul framework
Build a RESTful Web service
4. How to use JAX-WS
Realizing Strategies for Document-Based Web Services With JAX-WS 2.0
Next post let’s talk about AJAX and RESTFul!
Recently I need to create a client application to get web service with SOAP/HTTP. What I have is just WSDL URI. First I use Eclipse to genearte client program but it only automatically does for Axis 1.4 run time. Then I decide to use JAX-WS 2.0 and WSIT. I list the steps and you can follow it.
1. Download “webservices-tools.jar”
This is a specific task which is part of WSIT implementation of JAX-WS. Check this out
https://jax-ws.dev.java.net/files/documents/4202/65101/wsit-1_0-fcs-bin-b26-31_jul_2007.jar
2. Run Ant to generate stub
[build.xml]
<project name=”weatherServiceClient” basedir=”.”>
<property name=”src” value=”src” />
<property name=”build” value=”build” />
<property name=”dist” value=”dist” />
<path id=”jaxws.classpath”>
<pathelement path=”${classpath}”/>
<pathelement location=”lib/webservices-tools.jar”/>
</path>
<path id=”build.classpath”>
<pathelement path=”${classpath}”/>
<pathelement path=”${build}”/>
</path>
<target name=”generateStub” description=”Generate stub”>
<mkdir dir=”build” />
<mkdir dir=”dist” />
<taskdef name=”wsimport” classname=”com.sun.tools.ws.ant.WsImport”>
<classpath refid=”jaxws.classpath”/>
</taskdef>
<wsimport
debug=”true”
verbose=”true”
keep=”true”
sourcedestdir=”${src}”
extension=”true”
destdir=”${build}”
package=”com.example”
wsdl=”http://www.deeptraining.com/webservices/weather.asmx?WSDL”>
</wsimport>
</target>
<target name=”compileClient” description=”Compile client source”>
<javac destdir=”${build}” debug=”on” deprecation=”on” optimize=”on” classpathref=”build.classpath”>
<src path=”${src}” />
</javac>
</target>
<target name=”runClient” description=”Run client”>
<java classname=”com.example.WeatherServiceClient” fork=”true”>
<arg value=”New York”/>
<classpath>
<pathelement location=”${build}”/>
</classpath>
</java>
</target>
<target name=”jarIt”>
<jar jarfile=”${dist}/${ant.project.name}.jar” basedir=”build” />
</target>
<target name=”runClientWithJar” description=”Run client with jar”>
<java classname=”com.example.WeatherServiceClient” fork=”true”>
<classpath>
<pathelement location=”${dist}/${ant.project.name}.jar”/>
</classpath>
</java>
</target>
<target name=”clean” description=”Cleanup”>
<delete dir=”${dist}” />
<delete dir=”${build}” />
</target>
</project>
[command]
c:\Temp\myjaxws-examples>ant generateStub
3. Compile “WeatherServiceClient”
[WeatherServiceClient.java]
package com.example;
public class WeatherServiceClient {
private static String CITY = “Paris”;
public static void main(String args[]) {
if (args.length>0 && args[0] != null){
CITY = args[0];
}
WeatherSoap wsp = new Weather().getWeatherSoap();
String forecast = wsp.getWeather(CITY);
System.out.println(CITY + “’s weather forecast=” + forecast);
}
}
[Command]
c:\Temp\myjaxws-examples>ant compileClient
4. Run “WeatherServiceClient”
c:\Temp\myjaxws-examples>ant runClient
5. jar command
c:\Temp\myjaxws-examples\dist>java -cp .;dis\weahterServiceClient.jar com.example.WeatherServiceClient
6. Structure:
-temp
+myjaxws-examples
-build.xml
+lib
-webservices-tools.jar
+build
-Weather.class
-WeatherServiceClient.class
…
+src
-Weather.java
-WeatherServiceClient.java
+dist
-WeatherServiceClient.jar
7. Reference:
Web Services Made Easy with JAX-WS 2.0





