Make EJB client in Java ME
Article Metadata
This article creates a small Enterprise Javabeans (EJB) application that waits for a string and then sends a response.
Contents |
EJB application
The appliaction wait for a string and send an answer: "Hello $string!"
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.
Steps:
- 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) {
exitMIDlet();
}
}
}
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);
form.addCommand(exitCommand);
form.setCommandListener(this);
// Get display for drawning
display = Display.getDisplay(this);
display.setCurrent(form);
soapCall();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void exitMIDlet() {
display.setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
public void soapCall() {
SoapSerializationEnvelope soapEnvelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Create your outgoing request
SoapObject soapObject =
new SoapObject("http://www.developer.nokia.com/Community/Wiki/", "getHello");
// Add a parameter
soapObject.addProperty("arg0", "Tibor");
// Assigns the object to the envelope
soapEnvelope.setOutputSoapObject(soapObject);
// Make the call
try {
HttpTransport transport =
new HttpTransport("http://localhost:8080/HelloService/Hello");
transport.call(null , soapEnvelope);
Object responseObject = null;
responseObject = soapEnvelope.getResponse();
System.out.println(responseObject);
stringItem.setText(responseObject.toString());
}
catch (Exception e){
System.out.println("Exception : "+ e.getMessage());
e.printStackTrace();
}
}
}







26 Sep
2009
The Title of this Article "Make EJB Client in J2ME" EJB stands for Enterprise java Beans.Technology which is mainly used in with J2EE Client-Server Enterprise Applications.This article created small EJB Client Application.Steps are like 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 Mid-let and click on the Finish. This article has demonstrated a new way to use existing java technology to create application that consumes web service through MIDlet.
30 Sep
2009
This is an innovative concept in J2ME.This article contains an important code on to make EJB client in Java ME. In the code snippet, a class from EJB package, that is stateless, is imported. As we know that EJB package is used to make Client side web services. So this package provides methods that will support webservices. Here a short example is explained in briefly. A text box is given and when a command OK is pressed , an outgoing request is done and an SoapObject’s instance is created. And that instance is used to make a call And then through this call a string is sent to display.
So this provides an important information about SoapObject, EJB etc.. This is a nice concept and it will aid to Java ME developer to develop new thing or we can say an innovative thing. Here the entire thing is illustrated with code and some snapshots and it clearly understands. I really like this article.