WEBSERVICE USING APACHE AXIS- TUTORIAL-2 J2ME CLIENT FOR EJB & EJB-WEBSERVICE

In the previous section,we've dealt with how to write WAP client for accessing ejb. In the same way, we are going to see how to write J2ME client for ejb.We divide this part into two.

WEBSERVICE USING APACHE AXIS- TUTORIAL-2 J2ME CLIENT FOR EJB & EJB-WEBSERVICE

WEBSERVICE USING APACHE AXIS- TUTORIAL-2   J2ME CLIENT FOR EJB & EJB-WEBSERVICE
R.S.RAMASWAMY ([email protected])

In  the previous section,we've dealt with how to write WAP client for accessing ejb. In the same way, we are going to see how to write J2ME client for ejb.We divide this part into two. They are

(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

 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 class j2meServlet extends HttpServlet

{

 public void doPost

   (HttpServletRequest request,

   HttpServletResponse response)

       throws ServletException,IOException

 {

   response.setContentType("text/html");

   try

    {

    System.out.println("Please wait----!");

    Properties     props = new Properties();

   props.put 0

(Context.INITIAL_CONTEXT_FACTORY,   "weblogic.jndi.WLInitialContextFactory");

   props.put(Context.PROVIDER_URL,

              "t3://localhost:7001"); 1

    Context   ctx = new InitialContext(props);

    System.out.println("Context ok");

    sqlHome home = (sqlHome)ctx.lookup("sqlJndi"); 2

       System.out.println("Home ok");

    sqlRemote   remote = home.create();

   System.out.println("Remote ok"); 3

   String s = request.getParameter("text1");

  System.out.println(s);

   String s1=s+"%"; 4

   String            sql=

"select * from table1 where name like '"+s1+"' ";

     System.out.println(sql); 5

   String msg= remote.getdata(sql);

  DataOutputStream   out =

  new DataOutputStream(response.getOutputStream()); 6

  out.writeUTF(msg);

  System.out.println(msg);

  } 7

  catch(Exception e1)

  {   System.out.println(" " + e1);   }

 } 8

 } 

c:\sam> javac j2meServlet.java

 copy j2meServlet.class  to: 9

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 0

//ejbmidlet.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*; 1

import javax.microedition.io.*;

import java.io.*;

public class ejbmidlet  extends MIDlet implements CommandListener 2

{

     Display        display1;

     Form           form1,form2; 3

     Command        button1,button2;

     TextField  text1;

     StringItem   s1; 4

 public ejbmidlet()

        {

          display1 = Display.getDisplay(this); 5

          text1    = new TextField(null,"a",100,TextField.URL);

          form1 = new Form("Type in a URL:");

          form1.append(text1); 6

          button1 = new Command("SEND",Command.OK,1);

          button2 = new Command("Back",Command.OK,1);

          form1.addCommand(button1); 7

          form1.setCommandListener(this);

        }

 public void startApp() 8

         {

              display1.setCurrent(form1);

         } 9

  public void pauseApp() {}

         public void destroyApp(boolean unconditional)    {}

  public void commandAction(Command cmd,Displayable d) 0

          {

 String     url = "http://localhost:8080/servlet/j2meServlet";

             if(cmd==button1) 1

             {

               try

               { 2

                    String s = text1.getString();             

                    url = url+"?text1="+s;

                    System.out.println(url); 3

 

  HttpConnection  hc =

         (HttpConnection)Connector.open(url); 4

  hc.setRequestMethod(HttpConnection.POST);

  DataOutputStream dos =

     new DataOutputStream(hc.openOutputStream()); 5

  dos.writeUTF(s);

  dos.flush();

  System.out.println(s +" Sent"); 6

 

  DataInputStream   dis =

     new DataInputStream(hc.openInputStream()); 7

  String msg=dis.readUTF();

  System.out.println(msg);

  form2 = new Form("Get Result"); 8

               s1 = new StringItem(null,msg);

               System.out.println(s1);

               form2.append(s1); 9

               form2.addCommand(button2);

               form2.setCommandListener(this);

               display1.setCurrent(form2); 0

               System.out.println("displayed");    

              }catch(Exception e){}

        } 1

             else if (cmd == button2)

             {

               text1.setString(" "); 2

               display1.setCurrent(form1);

            }              

          } 3

}

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   4

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.  5

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'. 6

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. Recordset for the name starting with letter 'a' will be displayed in the wireless browser.Let us now create a J2ME client accessing ejb webservice.create and complie j2meaxisservlet.java  7

c:\sam> edit j2meaxisservlet.java

j2meaxisservlet.java

import java.net.URL; 8

import org.apache.axis.client.Service;

import org.apache.axis.client.Call;

import org.apache.axis.encoding.XMLType; 9

import javax.xml.rpc.ParameterMode;

import javax.xml.namespace.QName;

import javax.servlet.*; 0

import javax.servlet.http.*;

import java.io.*; 

public class j2meaxisservlet extends HttpServlet 1

{

    public void doPost (HttpServletRequest request, HttpServletResponse response)

                            throws ServletException, IOException 2

    {

        response.setContentType("text/html"); 

        try{ 3

            String url = "http://localhost:8080/axis/services/sqlservice";

            String method = "getdata"; 

           String s = request.getParameter("text1"); 4

           String s1=s+"%";

           String sql="select * from table1 where name like '"+s1+"' ";

           System.out.println("sql is"+sql);  5

            Service service = new Service(); 

            Call call = (Call) service.createCall();

    System.out.println("point1....call");  6

            call.setTargetEndpointAddress ( new java.net.URL(url));

            call.setOperationName ( new QName("sqlservice",method));

             call.addParameter("sql",XMLType.XSD_STRING,ParameterMode.IN); 7

            call.setReturnType(XMLType.XSD_STRING);

            System.out.println("Call Ok");           

            Object[] params = new Object[] { sql };  8

         String s2 = (String) call.invoke(params); 

        DataOutputStream out1 = new DataOutputStream(response.getOutputStream());

        out1.writeUTF(s2); 9

        System.out.println(s2);             

        }catch(Exception e) { System.out.println(""+e);  }

    } 0

}

 c:\sam> javac j2meaxisservlet.java 

copy j2meaxisservlet.class  to 1

  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.*; 3

import javax.microedition.io.*;

import java.io.*; 

public class j2meaxismidlet  extends MIDlet 4

              implements CommandListener

{

     Display        display1; 5

     Form           form1,form2;

     Command        button1,button2;

     TextField  text1; 6

     StringItem   s1; 

        public j2meaxismidlet()

        { 7

          display1 = Display.getDisplay(this);

  text1   = new TextField(null,"a",100,TextField.URL);

          form1 = new Form("Type in a URL:"); 8

          form1.append(text1);

          button1 = new Command("SEND",Command.OK,1);

          button2 = new Command("Back",Command.OK,1); 9

          form1.addCommand(button1);

          form1.setCommandListener(this);

        } 0

          public void startApp()

         {

              display1.setCurrent(form1); 1

         }

         public void pauseApp() {} 

  public void destroyApp(boolean unconditional)    {} 2

  public void commandAction(Command cmd,Displayable d)

          {

 String     url = "http://localhost:8080/axis/servlet/j2meaxisservlet"; 3

             if(cmd==button1)

             {

               try 4

               {

                    String s = text1.getString();             

                    url = url+"?text1="+s; 5

                    System.out.println(url); 

  HttpConnection   hc =

        (HttpConnection)Connector.open(url);  6

  hc.setRequestMethod(HttpConnection.POST); 

  DataOutputStream     dos =

       new DataOutputStream(hc.openOutputStream()); 7

  dos.writeUTF(s);

  dos.flush();

   System.out.println(s +" Sent");                    8

  DataInputStream   dis =

        new DataInputStream(hc.openInputStream()); 

  String msg=dis.readUTF();  9

  System.out.println(msg); 

               form2 = new Form("Get Result");

               s1 = new StringItem(null,msg); 0

               System.out.println(s1);

                form2.append(s1);

               form2.addCommand(button2); 1

               form2.setCommandListener(this);

               display1.setCurrent(form2);

              System.out.println("displayed");                                     2

           }catch(Exception e){}

              }

             else if (cmd == button2) 3

             {

               text1.setString(" ");

               display1.setCurrent(form1); 4

            }              

           }

} 5

That completes our rather long tutorial! Student-readers are requested to actually try all the above lessons in this carefully crafted tutorial..

visit http://in.geocities.com/rsramsam

Tutorials

  1. Apache Axis2 - Apache Axis2 Tutorial
  2. Why Web Services?
  3. Java Building a Simple Web Service ? A Tutorial Tutorial
  4. Apache Geronimo Application server Tutorial
  5. Apache Axis2 Tutorial, Develop the Next Generation of Apache Web Services using Apache Axis2
  6. SOA and Web Services
  7. Web Services Examples in NetBeans
  8. SOA and Web Services
  9. J2EE Web Service Development with Attachments Using Axis
  10. J2EE Web Service Development with Attachments Using Axis
  11. WEBSERVICE USING APACHE AXIS TUTORIAL-2 UNDERSTANDING APACHE AXIS
  12. Web Services - Web Services Tutorials
  13. Developing Axis Web services with XML Schemas.
  14. What is Service-Oriented Architecture?
  15. WEBSERVICE USING APACHE AXIS -TUTORIAL-2 UNDERSTANDING APACHE AXIS
  16. WEBSERVICE USING APACHE AXIS TUTORIAL-2 UNDERSTANDING APACHE AXIS (part-2)
  17. WEBSERVICE USING APACHE AXIS TUTORIAL-1
  18. WEBSERVICE USING APACHE AXIS TUTORIAL-2 UNDERSTANDING APACHE AXIS
  19. WEBSERVICE USING APACHE AXIS - TUTORIAL-2 AXIS FOR EJB-WEBSERVICE (part-5)
  20. Web Services Tutorials and Links
  21. WEBSERVICE USING APACHE AXIS TUTORIAL-2
  22. WEBSERVICE USING APACHE AXIS- TUTORIAL-2 J2ME CLIENT FOR EJB & EJB-WEBSERVICE
  23. Web Service
  24. Java Client webservice
  25. Ejb Webservice
  26. SOAP with Attachments API for Java
  27. SOAP Header
  28. WSDL program
  29. Application Using JAX-RPC
  30. Security in Web Service
  31. JAX-RPC Advance Concepts
  32. Database driven webservices
  33. Apache Axis2 - Apache Axis2 Tutorial
  34. Apache Axis2 Introduction
  35. Downloading and Installing Apache Axis2
  36. Apache Axis2 Hello World Example
  37. Axis2 client - Axis2 Client example
  38. Axis2 ant wsdl2java - Learn WSDL2java utility of Axis2 with example
  39. Axis2 Eclipse plugin Tutorial
  40. Installing axis2 eclipse plugin