WEBSERVICE
USING APACHE AXIS- TUTORIAL-2 J2ME CLIENT FOR EJB & EJB-WEBSERVICE
R.S.RAMASWAMY ([email protected])
(a) creating J2me client
for accessing ejb directly
(b) creating j2me client
for accessing ejb as webservice.
Let's see the first part now.
Create and complie j2meServlet.java in
the working folder ie. c:\sam
c:\sam> edit j2meServlet.java
import java.io.*;
import javax.ejb.*;
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
{
public
void doPost
(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html");
{
System.out.println("Please
wait----!");
props.put
(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(Context.PROVIDER_URL,
"t3://localhost:7001");
sqlRemote remote = home.create();
System.out.println(s);
"select * from table1 where name
like '"+s1+"' ";
System.out.println(sql);
new DataOutputStream(response.getOutputStream());
System.out.println(msg);
}
catch(Exception
e1)
{ System.out.println(" " + e1);
}
}
c:\sam> javac j2meServlet.java
copy
j2meServlet.class to:
d:\tomcat 4.1\webapps\root\web-inf\classes
Next step is to create a midlet for J2ME.Create ejbmidlet.java in our current working folder.
c:\sam> edit ejbmidlet.java
//ejbmidlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class ejbmidlet extends MIDlet
implements CommandListener
{
Display display1;
Form form1,form2;
Command button1,button2;
TextField text1;
StringItem s1;
public ejbmidlet()
{
display1 = Display.getDisplay(this);
text1 = new TextField(null,"a",100,TextField.URL);
form1 = new Form("Type in a URL:");
form1.append(text1);
button1 = new Command("SEND",Command.OK,1);
button2 = new Command("Back",Command.OK,1);
form1.addCommand(button1);
form1.setCommandListener(this);
}
public void startApp()
{
display1.setCurrent(form1);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional)
{}
public void commandAction(Command cmd,Displayable d)
{
String url =
"http://localhost:8080/servlet/j2meServlet";
if(cmd==button1)
{
try
{
String
s = text1.getString();
url = url+"?text1="+s;
System.out.println(url);
HttpConnection hc =
(HttpConnection)Connector.open(url);
hc.setRequestMethod(HttpConnection.POST);
DataOutputStream
dos =
new DataOutputStream(hc.openOutputStream());
dos.writeUTF(s);
dos.flush();
System.out.println(s +"
Sent");
DataInputStream dis =
new DataInputStream(hc.openInputStream());
String msg=dis.readUTF();
System.out.println(msg);
form2 = new Form("Get Result");
s1 = new
StringItem(null,msg);
System.out.println(s1);
form2.append(s1);
form2.addCommand(button2);
form2.setCommandListener(this);
display1.setCurrent(form2);
System.out.println("displayed");
}catch(Exception e){}
}
else if (cmd == button2)
{
text1.setString(" ");
display1.setCurrent(form1);
}
}
}
No need to complie the midlet file.
It is taken care of by J2ME Wireless Toolkit. For running the toolkit from our working folder
set the path as below
c:\sam> set path=%path%;c:\j2mewtk\bin;
c:\sam> ktoolbar
This will open the Toolkit window.Create a new Project by clicking the
button 'New project'.and
give the project name as 'ejbdemo1' as project name.Just minimize the window.
Now copy ejbmidlet.java from our
working folder to 'src' folderof 'ejbdemo1'.
c:\sam> copy ejbmidlet.java
c:\j2mewtk\apps\ejbdemo1\src
Then go to the toolkit and click 'settings'.
In the 'midlets' tab,add midlet class name ie. ejbmidlet
Click 'Build' for compilation. After source file is compiled
successfuly, Click 'Run' button and execute our midlet. (tomcat41 is running).
Type a letter 'a' in the textbox and click 'send'.
Midlet program sends the letter to
the servlet which'll form sqlquery and contact ejb for
fetching the result.
c:\sam> edit j2meaxisservlet.java
j2meaxisservlet.java
import java.net.URL;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class j2meaxisservlet
extends HttpServlet
{
public
void doPost (HttpServletRequest
request, HttpServletResponse response)
throws
ServletException, IOException
{
response.setContentType("text/html");
try{
String url =
"http://localhost:8080/axis/services/sqlservice";
String method
= "getdata";
String s = request.getParameter("text1");
String
s1=s+"%";
String sql="select * from table1 where name like
'"+s1+"' ";
System.out.println("sql is"+sql);
Service service = new Service();
Call call = (Call)
service.createCall();
System.out.println("point1....call");
call.setTargetEndpointAddress ( new
java.net.URL(url));
call.setOperationName ( new QName("sqlservice",method));
call.setReturnType(XMLType.XSD_STRING);
System.out.println("Call Ok");
Object[] params = new Object[] {
sql };
String s2 =
(String) call.invoke(params);
DataOutputStream out1 = new
DataOutputStream(response.getOutputStream());
out1.writeUTF(s2);
System.out.println(s2);
}catch(Exception
e) { System.out.println(""+e); }
}
}
copy j2meaxisservlet.class to
d:\tomcat 4.1\webapps\axis\web-inf\classes
Start tomcat4.1 server and do the procedure mentioned in the
first part for deploying midlet.
c:\sam> edit j2meaxismidlet.java 2
javaismidlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class j2meaxismidlet extends MIDlet
implements CommandListener
{
Display display1;
Form form1,form2;
Command button1,button2;
TextField text1;
StringItem s1;
public
j2meaxismidlet()
{
display1 = Display.getDisplay(this);
text1 = new TextField(null,"a",100,TextField.URL);
form1 = new Form("Type in a URL:");
form1.append(text1);
button1 = new Command("SEND",Command.OK,1);
button2 = new Command("Back",Command.OK,1);
form1.addCommand(button1);
form1.setCommandListener(this);
}
{
display1.setCurrent(form1);
}
public void pauseApp() {}
public
void destroyApp(boolean
unconditional) {}
public
void commandAction(Command cmd,Displayable
d)
{
String url
= "http://localhost:8080/axis/servlet/j2meaxisservlet";
if(cmd==button1)
{
try
{
String
s = text1.getString();
url = url+"?text1="+s;
System.out.println(url);
HttpConnection hc =
(HttpConnection)Connector.open(url);
hc.setRequestMethod(HttpConnection.POST);
DataOutputStream dos
=
new DataOutputStream(hc.openOutputStream());
dos.writeUTF(s);
dos.flush();
DataInputStream dis =
new DataInputStream(hc.openInputStream());
String msg=dis.readUTF();
System.out.println(msg);
form2 = new Form("Get Result");
s1 = new
StringItem(null,msg);
System.out.println(s1);
form2.addCommand(button2);
form2.setCommandListener(this);
display1.setCurrent(form2);
System.out.println("displayed");
}catch(Exception e){}
else if (cmd == button2)
{
text1.setString(" ");
display1.setCurrent(form1);
}
}
That completes our rather long tutorial! Student-readers are requested to actually try
all the above
lessons in this carefully crafted tutorial..