You are here

Integrate J2ME Mobile Clients into Web Service 2 -- Create Web Service and J2EE Client

Blog Terms: 

2 Create Web Service

2.1 Create Web Service Project

Enter the %AXIS2_HOME%/samples (Here is D:\ProgramFiles\axis2\samples) , there are many Web Service good example here. If you are not clear about how to create one Web Service from nothing, just like me, those examples are good turorials. Now I will use one and modify it to be our first Web Service.

2.1.1 Copy Project quickstart and rename it to firstservice

You can use your own project name, but if you are one beginner like me, I suggest you had better follow me, because any change here maybe cause one Web Service exception such as wrong namespace. Once you know the workflow, you can do everything as you will.
The copied new Project should have the following structure:

- firstservice
- resources
--- services.xml
- src
-- samples
--- quickstart
---- service
------ pojo

The next work please change the structure and file name as following, I am not advertising here, this will let you know clearly what kind of configuration should be changed later.

- firstservice
- resources
--- services.xml
- src
-- www
--- easywms
---- com

2.1.2 Open, and modify the code as here, it is easy?


public class FirstService{

public String request(String name, String time) {
return "Hello, " + name + ", it is " + time;


2.1.3 Modify services.xml

Open service.xml in resources/META-INF, modify it as following:
<service name="FirstService" scope="application" typeNamespace="http://easywms.www/types"
My Fisrt Service
<messageReceiver mep=""
<messageReceiver mep=""
<schema schemaNamespace="http://easywms.www/xsd"/>
<parameter name="ServiceClass"></parameter>

The service.xml file tells Axis2 about the service. It points out the class and the methods to be turned into operations. It also specifies whether the service should use a "in-out" receiver, or an "in only" receiver.
Pay attention to the Namespace(red font) in this xml configuration file, at deployment, if we do not specify a schema target
namespace, then the target namespace will be calculated using the
package name of the service implementation class. For example, if the
full qualified name of the service implementation class is then the schema namespace will be,

If you want to provide your own value for that, you can do that by adding the following element into the services.xml file, just as the red font in our service.xml.
<schema schemaNamespace=http://FirstService.www/xsd/>

2.1.4 Modify build.xml

Build.xml is very important for ANT, open it and change something as following, please notice the red font, that is the difference from project quickstart:

<project name="firstservice" basedir="." default="generate.service">

<property environment="env"/>
<property name="AXIS2_HOME" value="../.."/>

<property name="build.dir" value="build"/>

<path id="axis2.classpath">
<fileset dir="${AXIS2_HOME}/lib">
<include name="*.jar"/>

<target name="compile.service">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.dir}/classes"/>

<!--First let's compile the classes-->
<javac debug="on"

<target name="generate.wsdl" depends="compile.service">
<taskdef name="java2wsdl"
<java2wsdl className=""
<pathelement path="${axis2.classpath}"/>
<pathelement location="${build.dir}/classes"/>

<target name="generate.service" depends="compile.service">
<!--aar them up -->
<copy toDir="${build.dir}/classes" failonerror="false">
<fileset dir="${basedir}/resources">
<include name="**/*.xml"/>
<jar destfile="${build.dir}/FirstService.aar">
<fileset excludes="**/Test.class" dir="${build.dir}/classes"/>

<target name="clean">
<delete dir="${build.dir}"/>

2.1.5 Build Web Service

Run cmd and enter the path %AIXS2_HOME%/samples/firstservice/ , in this case, it is D:\ProgramFiles\axis2\samples\fisrtservice, and type to run
ant generate.service

This will create one build folder in the firstservice project with such structure:
creates the following directory

- firstservice
- services.xml
- www
- easywms
- com
- FirstService.class
--- FirstService.aar

2.1.6 Deploy Web Service

Rename the classes directory to FirstService, and copy
it to the %TOMCAT_HOME%/webapps/axis2/WEB-INF/services directory, in this case, it is D:\ProgramFiles\Tomcat6.0\webapps\axis2\WEB-INF\services. Otherwise, copy the build/FirstService.aar file to the %TOMCAT_HOME%/webapps/axis2/WEB-INF/services directory.
Then check to make sure that the service has been properly deployed
by viewing the list of services at:


And you can find our first service:

Service EPR : http://localhost:8080/axis2/services/FirstService
Service Description : FirstService
Service Status : Active
Available Operations

* request

You can also checkout the WSDL at:


And the schema at:


Now our first service is working, to quickly test the service. Try
pointing your browser to the following URL:


Now will get the following response SOAP message:


<ns:return>Hello, Guy, it is 10oclock</ns:return>


Till now, we have built one Web Service successfully.

2.2 Create Normal Java Web Service Client

Before we do it with J2ME client, I will create one normal Java client, so that we can know how the client works with Web Service, this is helpful, I have spent much time tolearn J2ME client and WS, and I found the normal WS client is very good tutorial for me to understand the whole structure and workflow.

2.2.1 Generate WSDL

WSDL is Web Services Description Language, Axis can use it to generate Stub java source code.
Run cmd and enter the path %AIXS2_HOME%/samples/firstservice/ , in this
case, it is D:\ProgramFiles\axis2\samples\fisrtservice, and type to run
ant generate.wsdl
This will run the task "generate.wsdl" in build.xml in 2.1.4, and generate one "FirstService.wsdl" in build folder.

Notice: we can use Axis2 Eclipse Plugins to do this. Here is the turorial.

2.2.2 Generate Stub Java Code

What is Stub? Web service stub provides replacement implementations for another Web service that the Web service under test interacts with. Now I will generate the client-side stub code used by the client to access the
Web service. The stub is class that is a local Java object that acts
as a proxy for the Web service instance. The WS client makes a local
method call to the stub and the stub, in turn, calls the Web service on
the server.

Run cmd and enter the path %AIXS2_HOME%/samples/firstservice/build/, in this case, it is D:\ProgramFiles\axis2\samples\firstservice\build, and run this:
%AXIS2_HOME%\bin\WSDL2Java -uri FirstService.wsdl -p -d adb -s
(D:\ProgramFiles\axis2\bin\WSDL2Java -uri FirstService.wsdl -p -d adb -s)

It will create one "src" folder with Stub Java code inside (in www/com path)

Notice: we can use Axis2 Eclipse Plugins to produce the stub code. Here is the turorial.

2.2.3 Create Client project

Open Eclipse, create one Java Project named "firstclient", create "src" as source folder and one folder with name "lib", copy the jar files in the following list from
%AIXS2_HOME%/lib (D:\ProgramFiles\axis2\lib) to firstclient/lib, and import them as Libraries in "Java Build Path"; copy the java stub file in src folder that created in 2.2.2. And your project in Eclipse will be like this image:

Now, create one class named "FirstClient" with the following code:

public class FirstClient {
public static void main(String[] args) {
FirstServiceStub stub;
try {
double c_value = 32;
stub = new FirstServiceStub("http://localhost:8080/axis2/services/FirstService");
FirstServiceStub.Request request = new FirstServiceStub.Request();

request.setTime("11 oclock");

FirstServiceStub.RequestResponse requestresponse = stub.request(request);

System.out.println("The response from FirstService: " +requestresponse.get_return());
} catch (Exception e) {

Run it as "Java Application", we will see the message from Web Service FirstService:
The response from FirstService: Hello, Catty, it is 11 oclock

Till now, we have finished one Web Service and one normal java client. Next chapter, I will build one j2ME client.
This is the source of FirstService and FirstClient.



What happen to chapter3 {:?}

Where is Client for j2ME?

There's nothing in your article that talks about J2ME client as mentioned in the title. I think its a little misleading if have it in the subject, but no where to be seen in the content.

Dear All,..

I want to invoke object from web service from j2me and using jsr 172, what is the object type should i use? I can't use String[],List(util).. need help.thx.