WEBSERVICE USING APACHE AXIS TUTORIAL-2 UNDERSTANDING APACHE AXIS (part-2)

Creating and testing an EJB in WebLogic Server.Let us begin by creating our working folder as c:\sam.

WEBSERVICE USING APACHE AXIS TUTORIAL-2 UNDERSTANDING APACHE AXIS (part-2)

--Ads--

WEBSERVICE USING APACHE AXIS   TUTORIAL-2  UNDERSTANDING APACHE AXIS
(part-2)
published in DeveloperIQ..April,2004) (www.developeriq.com)
R.S.RAMASWAMY([email protected]) 

Creating and testing an EJB in WebLogic Server.Let us begin by creating our working folder as 
c:\sam.

We then edit the three files for EJB in that folder. 

sqlremote.java

sqlhome.java)

sqlbean.java 

// sqlremote.java

import javax.ejb.*;

import java.rmi.*; 

public interface sqlremote extends EJBObject

{

  public String showdata(String s) throws RemoteException;

}

 

//sqlhome.java

import javax.ejb.*;

import java.rmi.*; 

public interface sqlhome extends EJBHome

{

  public sqlremote create() throws RemoteException,CreateException;

}

 

 

// sqlejbbean.java

import java.sql.*;

import javax.ejb.*;

import java.rmi.*;

import javax.naming.*; 

public class sqlejbbean implements SessionBean 0

{

  public sqlejbbean()

  {}  1

  public String showdata(String s)

  {

      String r=" ";  2

      try{

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    String url="jdbc:odbc:dbdemo"; 3

    Connection con=DriverManager.getConnection(url);

    Statement st=con.createStatement(); 

    ResultSet rs=st.executeQuery(s); 4

    while(rs.next())

    {

      r=r+rs.getString(1)+"\n"+rs.getString(2)+"\n"+"-------"; 5

    }

         }catch(Exception e1)

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

       return r;

  } 

  public void ejbCreate()   {}  7

  public void ejbRemove()   {} 

  public void ejbActivate()   {} 

  public void ejbPassivate()  {}  8

  public void setSessionContext(SessionContext sc)  {} 

} 

9

Please note that Weblogic7 will work only in Windows 2000. So, we are now working in Windows 2000.

Let us now set HOMEPATH & CLASSPATH.

c:\sam>set JAVA_HOME=d:\jdk1.4.2  0

(We have installed jdk1.4.2 in d :\) 

c:\sam>set WL_HOME=d:\bea\weblogic700 

c:\sam>set path=c:\windows\command;d:\ jdk1.4.2\bin; 1

d:\bea\weblogic700\server\bin;

c:\sam>set classpath=c:\sam;

d:\bea\weblogic700\server\lib\weblogic.jar; 2

We now compile files in c:\sam folder. 

c:\sam>javac    *.java

Create a subfolder named META-INF as follows: 3

( it should be capital letters!) 

c:\sam>md META-INF

c:\sam>cd META-INF  4

c:\sam\META-INF> 

 In META-INF folder, create the following two Deployment-Descriptor files (XML files).  These files are MOST IMPORTANT.  XML files are case-sensitive.   

//   ejb-jar.xml 5

<?xml version="1.0"?>

<!DOCTYPE ejb-jar PUBLIC

"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" 6

"http://java.sun.com/dtd/ejb-jar_1_1.dtd"> 

<ejb-jar>

    <enterprise-beans> 7

       <session>

          <ejb-name>sqlejbbean</ejb-name>

          <home>sqlhome</home> 8

          <remote>sqlremote</remote>

          <ejb-class>sqlejbbean</ejb-class>

          <session-type>Stateless</session-type> 9

          <transaction-type>Container</transaction-type>

       </session>

    </enterprise-beans> 0

</ejb-jar>

 

  1

//weblogic-ejb-jar.xml

<?xml version="1.0"?>

<!DOCTYPE weblogic-ejb-jar PUBLIC 2

"-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN"

"http://www.bea.com.servers/wls700/dtd/weblogic-ejb-jar.dtd"> 

<weblogic-ejb-jar> 3

  <weblogic-enterprise-bean>

     <ejb-name>sqlejbbean</ejb-name>

     <jndi-name>sqlejbJndi</jndi-name> 4

  </weblogic-enterprise-bean>

</weblogic-ejb-jar>

  5

 Now, revert back to c:\sam folder. 

Create the jar file as follows: 

sam>jar  cf   sql.jar      *.class          META-INF\*.xml 6

This command will create sql.jar. 

The next step is to create sql1.jar for deployment in weblogic server.

sam>java   weblogic.ejbc  sql.jar   sql1.jar  7

This will create sql1.jar.  We may get some warning messages, which can be ignored.  Finally, we get the message ?ejbc successful?. 

Now copy sql1.jar to the following folder:

d:\bea\user_projects\mydomain\Applications Start weblogic server by the following procedure: 8

Startmenu->programs->

bea weblogic platform->user projects->mydomain->startserver 

Give     username:  system 9

password:  administrator    

Wait till you get the message: ?started for Running mode?.  Minimize this window.  That completes the job of deployment in ejb server.  We should now create a console-mode client for the ejbean 

// sqlConsoleClient.java 0

import java.ejb.*;

import java.rmi.*;

import javax.rmi.*; 1

import javax.naming.*;

import java.io.*;

import java.util.*;  2

public class sqlConsoleClient

{

   public static void main(String args[]) 3

   {

    try

    { 4

    Properties      props=new Properties();

    props.put

    (Context.INITIAL_CONTEXT_FACTORY, 5

     "weblogic.jndi.WLInitialContextFactory");

    String url="t3://127.0.0.1:7001";

    props.put(Context.PROVIDER_URL,url);  6

    Context context=new InitialContext(props);

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

    sqlhome home=(sqlhome)context.lookup("sqlejbJndi"); 7

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

    sqlremote remote=home.create();

System.out.println("remote ok..");  8

    a=remote.showdata(s);

    System.out.println(a);

        }catch(Exception e1) 9

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

  }

  catch(Exception e1) 0

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

  }

}  1

 

 Now compile the client file:

C:\sam>javac sqlConsoleClient.java 2

Let us now run the client program:

>java  sqlConsoleClient     ?select * from table1?

We will get a few names and numbers.We now proceed to create the jsp file . 3

// sqljspclient.jsp

<html>

<body> 4

<%@  page  import=?javax.ejb.*?       %>

<%@  page  import=?java.rmi.*?        %>

<%@  page  import=?javax.rmi.*?       %> 5

<%@  page  import=?javax.naming.*?    %>

<%@  page  import=?java.util.*?       %>

<%@  page  import=?java.io.*?         %>  6

<%

String  sql=  request.getParameter(?text1?);

Out.println(?please wait?); 7

Properties      props=new Properties();

    props.put

    (Context.INITIAL_CONTEXT_FACTORY, 8

             "weblogic.jndi.WLInitialContextFactory");

    String url="t3://127.0.0.1:7001";

    props.put(Context.PROVIDER_URL,url);  9

    Context context=new InitialContext(props);

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

    sqlhome   home=(sqlhome)context.lookup("sqlejbJndi"); 0

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

    sqlremote remote=home.create();

System.out.println("remote ok..");  1

    a=remote.showdata(s);

    out.println(a);

  %>  2

</body>

</html>

  3

 Create the corresponding html file to invoke the above jsp.

// sqljspclient.htm

<html> 4

<body>

<form   method=post 

        action=?sqljspclient.jsp? /> 5

sql

<input   type=text   name=?text1?  size=60>

<input   type=submit> 6

 

</form>

</body> 7

</html>

We have installed tomcat3.2 in e: drive.  Copy sqljspclient.jsp  &  sqljspclient.htm to e:\tomcat\webapps\root.

 To start the tomcat server, cd to  8

 e:\tomcat\bin>set JAVA_HOME=D:\JDK1.4.2

>SET CLASSPATH=%CLASSPATH%;c:\sam;

 d:\bea\weblogic700\server\lib\weblogic.jar 9

>startup

This will start the webserver. Start the browser and type the URL as:?http://localhost:8080/sqljspclient.htm?.  We get a form.  Type the sql and submit.  We get the correct result (tested and found ok).  Thus, we have created a stateless session EJB, tested it in console mode and also as a jsp deployed in tomcat3.2.  We now proceed to expose our ejb as an XML-WebService, using AXIS by two methods:

  1. as *.jws (known as Drop-in Deployment).( part-3)
  2. by using wsdd file etc.(part-4)

 The next article discusses these aspects in detail.  0

Continued in axis2c.htm Visit  http://in.geocities.com/rsramsam