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

{

public sqlejbbean()

{}

public String
showdata(String s)

{

String
r=" ";

try{

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

String url="jdbc:odbc:dbdemo";

Connection con=DriverManager.getConnection(url);

Statement st=con.createStatement();

ResultSet rs=st.executeQuery(s);

while(rs.next())

{

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

}

}catch(Exception e1)

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

return r;

}

public void ejbCreate() {}

public void ejbRemove() {}

public void ejbActivate() {}

public void ejbPassivate() {}

public void
setSessionContext(SessionContext sc) {}

}

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

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

d:\bea\weblogic700\server\bin;

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

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

We
now compile files in c:\sam folder.

c:\sam>javac *.java

Create
a subfolder named META-INF as
follows:

( it should be capital letters!)

c:\sam>md
META-INF

c:\sam>cd META-INF

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

<?xml version="1.0"?>

<!DOCTYPE ejb-jar PUBLIC

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

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

<ejb-jar>

<enterprise-beans>

<session>

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

<home>sqlhome</home>

<remote>sqlremote</remote>

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

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

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

</session>

</enterprise-beans>

</ejb-jar>

//weblogic-ejb-jar.xml

<?xml version="1.0"?>

<!DOCTYPE weblogic-ejb-jar PUBLIC

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

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

<weblogic-ejb-jar>

<weblogic-enterprise-bean>

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

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

</weblogic-enterprise-bean>

</weblogic-ejb-jar>

Now, revert back to c:\sam
folder.

Create
the jar file as follows:

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

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

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:

Startmenu->programs->

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

Give username: system

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

import java.ejb.*;

import java.rmi.*;

import javax.rmi.*;

import javax.naming.*;

import java.io.*;

import java.util.*;

public class sqlConsoleClient

{

public static
void main(String args[])

{

try

{

Properties props=new
Properties();

props.put

(Context.INITIAL_CONTEXT_FACTORY,

"weblogic.jndi.WLInitialContextFactory");

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

props.put(Context.PROVIDER_URL,url);

Context
context=new InitialContext(props);

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

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

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

sqlremote
remote=home.create();

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

a=remote.showdata(s);

System.out.println(a);

}catch(Exception e1)

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

}

catch(Exception
e1)

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

}

}

Now
compile the client file:

C:\sam>javac sqlConsoleClient.java

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 .

// sqljspclient.jsp

<html>

<body>

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

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

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

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

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

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

<%

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

Out.println(?please
wait?);

Properties props=new Properties();

props.put

(Context.INITIAL_CONTEXT_FACTORY,

"weblogic.jndi.WLInitialContextFactory");

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

props.put(Context.PROVIDER_URL,url);

Context
context=new InitialContext(props);

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

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

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

sqlremote
remote=home.create();

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

a=remote.showdata(s);

out.println(a);

%>

</body>

</html>

Create
the corresponding html file to invoke the above jsp.

//
sqljspclient.htm

<html>

<body>

<form method=post

action=?sqljspclient.jsp? />

sql

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

<input type=submit>

</form>

</body>

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

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

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

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

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