Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 06:56, 9 June 2009 by tanjaluodes (Talk | contribs)

How to make an EJB client in Java ME

From Wiki
Jump to: navigation, search

We will create a small EJB application and we have two way to solve the communication from the mobile phone.


EJB application

The appliaction wait for a string and send an answer: "Hello $string!"

import javax.ejb.Stateless;
import javax.jws.WebService;
public class Hello
public String getHello(String name)
return "Hello " + name + "!";

Easy way with WSA and NetBeans

If the target phone support the WSA we only need to import the WSDL file from the server and the NetBeans wizard will create a sample application to us.


  • Create a new Mobile Application
  • Right click on the project / New / J2ME Web Service Client
  • Write the URL to the WSDL URL box and click on the Retrieve WSDL button
  • Check on the Create Sample MIDLet and click on the Finish


Other way without WSA

If the target phone doesn't support the WSA feature we need to use some alternative way, like kSOAP2. We can't use the benefits of the WSDL and we need edit manually the soap message.

Don't forget to add the libs of the kSOAP2 to the project!


package helloworld;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
public class HelloWorldMidlet extends MIDlet implements CommandListener {
public HelloWorldMidlet() {
// Display
private Display display;
// Main form
private Form form;
// For the message
private StringItem stringItem;
// For the exit command
private Command exitCommand;
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
public void startApp() {
// Create form
stringItem = new StringItem("Hello", "Hello World!");
form = new Form(null, new Item[] {stringItem});
exitCommand = new Command("Exit", Command.EXIT, 1);
// Get display for drawning
display = Display.getDisplay(this);
public void pauseApp() {
public void destroyApp(boolean unconditional) {
public void exitMIDlet() {
public void soapCall() {
SoapSerializationEnvelope soapEnvelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Create your outgoing request
SoapObject soapObject =
new SoapObject("", "getHello");
// Add a parameter
soapObject.addProperty("arg0", "Tibor");
// Assigns the object to the envelope
// Make the call
try {
HttpTransport transport =
new HttpTransport("http://localhost:8080/HelloService/Hello"); , soapEnvelope);
Object responseObject = null;
responseObject = soapEnvelope.getResponse();
catch (Exception e){
System.out.println("Exception : "+ e.getMessage());

Usefull links



62 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.