Home

Architecture

WebApps

Database

Administration

Web Search

News

Java Servers

Web Services

Enterprise

Learning

Contact Us

JumpPoint - Java - EJB - Servlets - JSP - JMS - JNDI - JDBC - RMI

Creating and test an EJB

You create an EJB with JDeveloper just as you create other things - with File | New.


(1) In the System Navigator right click on Project1. Click on "New."



(2) In the Categories list select "
Enterprise JavaBeans."



(3) In the Items list select "
Stateless Session Bean."





(4) Click on "
OK"
   




(5) The Enterprise JavaBean Wizard starts.



Select all the defaults and click "
Next >."
   
(5) The Enterprise JavaBean Wizard starts. Select all the defaults and click "Next >."
   
When finished, you will be pointed at the bean - and the main screen will appear as shown at the right.

Currently you have a fairly uninteresting stateless session bean that does nothing. Let's make it a little interesting.



Click on the "
Fields" tab at the bottom.
   
So far no fields are defined.

To add one Click on the

"+
Add" button.
   
We'll create a String field called "info."



Note:

The Accessors get() and set() methods are already selected. So are the "Add get() and Add set() methods."



Let's leave things this way - so we get Remote interfaces to get and set the field.



Click on "
OK"
   
Now we can see that the stateless session EJB has a String field called info.



Now click on the "
Methods" tab at the bottom
   
After clicking on the "Methods" tab, you can see all the methods associated with the new EJB.



Included are the the new
String getInfo() and setInfo(String) methods that we just generated.
   
Now Right click on the file MySessionEJBBean.java and select "Code Editor" and change the method getInfo as shown: public String getInfo() {
return "StartInfo";
}
   

We're now finished with generating the EJB. You can go back and add features to it at any time. Just point to it in the Navigator, right click, and select "Class Editor." You can also click on "Code editor" to edit the actual Java code.


There is one more thing to do with the EJB, and that is to create some deployment XML files for it. Right click on the Bean in the Navigator window and select
Build. This will generate an EJB-JAR.XML file and an ORION-EJB-JAR.XML file (for Oracle9i App Server) as well.


We're now finished with the EJB. You can now deploy it to the internal Oracle9i Application Server and test it. Or if you wish to skip the section below just click below and we'll go on to the next section that deploys the EJB to WebLogic.

On to ==> Deploy the EJB to WebLogic!


Test the EJB on OC4J

You're still here! OK. Let's test it out on Oracle9i AS. JDeveloper gives you some tools to use for testing out on it's internal server.

(1) After you create the EJB, just right click on the EJB.



Then click on "Run" or "Debug" to deploy the EJB to the internal Oracle9i server..



Note that the menu also has several other options including
the ability to go back and modify the EJB using the EJB Class Editor and the ability to create a test client. Let's do that now.
   
(2) Right click on the EJB again.





This time choose
Create Sample Java Client.
 
 
   
(3) Click on OK



Note: This client is just for the internal Oracle9iAS (OC4J) instance. We will use a slightly client to connect to the external WebLogic server that runs over RMI IIOP.
   
Doing this generates the following client code:

Note:
This client code for internal testing does not use RMI-IIOP. The general form of EJB clients for a remote server such as WebLogic are shown in the lesson Running the EJB. See also EJB vendor conventions for connecting to other servers.

try {

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.evermind.server.rmi.RMIInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "admin");
env.put(Context.SECURITY_CREDENTIALS, "welcome");
env.put(Context.PROVIDER_URL, "ormi://localhost:23891/current-workspace-app");
Context ctx = new InitialContext(env);
MySessionEJBHome mySessionEJBHome = (MySessionEJBHome)ctx.lookup("MySessionEJB");
MySessionEJB mySessionEJB;

// Use one of the create() methods below to create a new instance
// mySessionEJB = mySessionEJBHome.create( );

} catch (Throwable ex){

ex.printStackTrace();

}

(4) Edit the generated EJB client program. Un-comment the line that does the Home.create(). And add in lines to call getInfo() and report the results.

(5) Now right-click on the test program and click on
Run.

mySessionEJB = mySessionEJBHome.create( );
String igot=MySessionEJB.getInfo();

System.out.println("Session bean returns: "+igot);

   

After you generate and run the client, let's go onto WebLogic.

On to ==> Deploy the EJB to WebLogic!