Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

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.

Ads

Tutorials   
Jersey - Java Framework for RESTful Web services Web Services in Java Tutorial for Beginners Web Services Growth Apache Axis2 Tomcat: Installing Apache Axis2 on Tomcat Introduction to Web services technologies Installing axis2 eclipse plugin Axis2 Eclipse plugin Tutorial Axis2 ant wsdl2java - Learn WSDL2java utility of Axis2 with example Axis2 client - Axis2 Client example Apache Axis2 Hello World Example Downloading and Installing Apache Axis2 Apache Axis2 Introduction Apache Axis2 - Apache Axis2 Tutorial Database driven webservices JAX-RPC Advance Concepts Security in Web Service Application Using JAX-RPC WSDL program SOAP Header SOAP with Attachments API for Java Ejb Webservice Java Client webservice Web Service WEBSERVICE USING APACHE AXIS- TUTORIAL-2 J2ME CLIENT FOR EJB & EJB-WEBSERVICE WEBSERVICE USING APACHE AXIS TUTORIAL-2 Web Services Tutorials and Links WEBSERVICE USING APACHE AXIS - TUTORIAL-2 AXIS FOR EJB-WEBSERVICE (part-5) WEBSERVICE USING APACHE AXIS TUTORIAL-2 UNDERSTANDING APACHE AXIS WEBSERVICE USING APACHE AXIS TUTORIAL-1 WEBSERVICE USING APACHE AXIS TUTORIAL-2 UNDERSTANDING APACHE AXIS (part-2) WEBSERVICE USING APACHE AXIS -TUTORIAL-2 UNDERSTANDING APACHE AXIS What is Service-Oriented Architecture? Developing Axis Web services with XML Schemas. Web Services - Web Services Tutorials WEBSERVICE USING APACHE AXIS TUTORIAL-2 UNDERSTANDING APACHE AXIS J2EE Web Service Development with Attachments Using Axis J2EE Web Service Development with Attachments Using Axis SOA and Web Services Web Services Examples in NetBeans SOA and Web Services Apache Axis2 Tutorial, Develop the Next Generation of Apache Web Services using Apache Axis2 Apache Geronimo Application server Tutorial Java Building a Simple Web Service ? A Tutorial Tutorial Why Web Services? Apache Axis2 - Apache Axis2 Tutorial
Ads

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

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

   props.put(Context.PROVIDER_URL,

              "t3://localhost:7001");

    Context   ctx = new InitialContext(props);

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

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

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

    sqlRemote   remote = home.create();

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

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

  System.out.println(s);

   String s1=s+"%";

   String            sql=

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

     System.out.println(sql);

   String msg= remote.getdata(sql);

  DataOutputStream   out =

  new DataOutputStream(response.getOutputStream());

  out.writeUTF(msg);

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

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.addParameter("sql",XMLType.XSD_STRING,ParameterMode.IN);

            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);  }

    }

}

 c:\sam> javac j2meaxisservlet.java 

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

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);

        }

          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/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();

   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);

            }              

           }

}

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

Advertisements

Ads
Share on Google+Share on Google+

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

Posted on: March 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: WEBSERVICE USING APACHE AXIS- TUTORIAL-2 J2ME CLIENT FOR EJB & EJB-WEBSERVICE   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads