You are on page 1of 7

Build J2ME Web

Services Application
with A1200

July 7, 2006

TECHNICAL ARTICLE
Build your J2ME Web Services Application with A1200
By
MOTODEV Staff

owadays, XML is the standard way clients to interact with backend servers so as to use the remote
N services. The Motorola A1200 handset supports JSR 172 optional packages, J2ME Web services,
which includes two independent parts: the JAX-RPC and JAXP. J2ME JAX-RPC APIs subset
solves how to access the SOAP/XML Web services and JAXP APIs subset solves how to processes the
XML messages.

In this article, we first introduce some basic concepts in J2ME Web services and show its limitations
compared to standard J2SE JAXP and JAX-RPC; then we build a little J2ME Web services application
with the Motorola SDK, SUN WTK2.2 stub generator and Apache Axis.

Basic concepts

XML eXtensible Markup Language


SOAP Simple Object Access Protocol. It is the transport protocol standard in Web
services, which defines data transport and encoding.
WSDL Web Service Description Language, defines how to use the Web services
UDDI Universal Description, Discovery and Integration. It is used in Web services
registry and discovery.
JAXP Java API for XML processing
JAX-RPC Java API for XML-Based RPC

JSR 172 limitations

JSR172 stipulates a set of lightweight APIs to be used in resource-limited devices. It provides access to
Web services available in J2ME and Web services processing the on the client side. Compared to its
corresponding J2SE super set, it has many limitations.

The JSR172 JAX-RPC APIs (a subset of J2SE’s JAX-RPC) do not support the server end; the handset
can only consume Web services but can’t supply them. It does not support UDDI, which means it can’t
discover Web services. It does not support SAAJ (SOAP with Attachment API for Java). It does not
support dynamic proxies or dynamic invocation interface (DII).

The JSR172 JAXP APIs (a subset of J2SE’s JAXP) do not support DOM, because the DOM is
considered to be too heavy for handset. It does not support XSLT. Also, SOAP message handlers are not
supported.
Example

A J2ME-based service client uses generated stub classes to access a Web service. The following steps
show how a J2ME JAX-RPC Subset client interacts with a Web service endpoint. Tomcat is a
JSP/Servlet container and Axis is an implementation of SOAP that is used to offer Web services.

J2ME application

JAX-RPC stub

SOAP/XML

JSR172 Run Web services


Time Server(Tomcat/Axis)

MIDP/CLDC

Figure 1: Basic structure of J2ME JAX-RPC application

Steps to build the demo application

On the server side:

Download Tomcat 5.5.17 and Axis 1.4 from the URL below and install

http://mirror.vmmatrix.net/apache/tomcat/tomcat-5/v5.5.17/bin/apache-tomcat-5.5.17.zip
http://apache.justdn.org/tomcat/tomcat-5/v5.5.17/bin/apache-tomcat-5.5.17-admin.zip
http://mirror.vmmatrix.net/apache/ws/axis/1_4/axis-bin-1_4.zip

Please view the installation guides for Apache and Tomcat for details on installation. After installation,
test Tomcat and Axis with the URL below (For this example, we’ll use 18080 as the TCP port for
Tomcat)

http://localhost:18080
http://localhost:18080/axis

Create the java class of the Web service and deploy it on the server side

Set the environment variables like below,

AXIS_HOME = C:\axis-1_4
AXIS_LIB = %AXIS_HOME%\lib
AXISCLASSPATH = %AXIS_LIB%\axis.jar; %AXIS_LIB%\commons-discovery-0.2.jar;
%AXIS_LIB%\commons-logging-1.0.4.jar; %AXIS_LIB%\jaxrpc.jar; %AXIS_LIB%\saaj.jar;
%AXIS_LIB%\log4j-1.2.8.jar; %AXIS_LIB%\xml-apis.jar; %AXIS_LIB%\xercesImpl.jar

Compile the ResponseService.java, and copy the ResponseService.class to


<TOMCAT_HOME>\webapps\axis\WEB-INF\classes\servicepack

ResponseService.java, it only says “Hello” to the client.

package servicepack;
public class ResponseService {
public String helloClient() {
return "Hello Client, this is server!" ;
}
}

Below is the WSDD(Web Service Deployment Descriptor ), it is used to deploy ResponseService.


Please copy it to <TOMCAT_HOME>\webapps\axis\WEB-INF\classes\servicepack.

< deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"


xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<service name="urn:helloClient" provider="java:RPC">


<parameter name="className" value="servicepack.ResponseService" />
<parameter name="allowedMethods" value="helloCient" />
</service>

</deployment>

Deploy the ResponserService to Axis, in <TOMCAT_HOME>\webapps\axis\WEB-


INF\classes\servicepack, type:

java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient -


lhttp://localhost:18080/axis/services/AdminService deploy.wsdd
Figure 2: helloservice deployed

Generate the WSDL file of the Web service in document/literal style from java class

Edit the Axis configuration file “<TOMCAT_HOME>\webapps\axis\WEB-INF\server-config.wsdd”, to


add style="document" use="literal" as the helloClient service attribute. This has to be done
because default Axis generated WSDL in the rpc/encoded style, but for J2ME Web services the
implementations must generate stubs that use the document style and literal use (document/literal).

<service name="urn:helloClient" provider="java:RPC" style="document" use="literal">


<parameter name="allowedMethods" value="helloClient" />
<parameter name="className" value="servicepack.ResponseService"/>
</service>

Then click the urn:helloClient (wsdl) hyperlink to generate the WSDL file of the Web service.

The helloClient service can also be tested by inputting the URL below, which will generate the SOAP
response.

http://localhost:18080/axis/services/urn:helloClient?method=helloClient

On the client side


Generate a stub from the WSDL description of the service

In the Sun Wireless Tool Kit 2.2 Utilities, select Stub Generator.
Four files were generated in the com.mot.WSDemo package:
ResponseService.java,
ResponseService.class,
ResponseService_Stub.java,
ResponseService_Stub.class
Figure 3: WTK 2.2 stub generator dialog

Import the stub files into the J2ME package, instantiate an instance of the stub and invoke methods on
the stub corresponding to the service endpoints

replyService = new ResponseService_Stub();


replyService._setProperty(Stub.SESSION_MAINTAIN_PROPERTY, new
Boolean(true));

try{
String result = replyService.helloClient();
}
catch(Exception e){
System.out.println(e.toString());
}

Package the stub with the J2ME client application and run it on the Motorola emulator
Figure 4: Calling the Web service from J2ME Client in Motorola Emulator

Conclusion

The JSR172 APIs are powerful and easy to use, but a lack of dynamic proxies and dynamic invocation
interface (DII) support makes the J2ME application less flexible. Because not all handsets support
JSR172 (Motorola began supporting JSR172 in the A1200 and ROKR E2 handsets), sometimes the open
source J2ME software like KSOAP is still a good alternative.

Sample Application

WSDemo.zip

References
JSR 172: J2ME Web Services Specification
Apache web services-Axis
Apache Tomcat
J2EE Web Services – The Ultimate Guide, Richard Monson-Haefel

You might also like