You are on page 1of 3

How to deploy and run an EJB

application using a J2EE server.


Take the following steps to deploy and run an EJB application using a J2EE server.
1. Write code for a Remote interface and save the file as Welcome.java. The code
for Welcome.java is given below:

import javax.ejb.*;
import java.rmi.RemoteException;

public interface Welcome extends EJBObject


{
   public String printMessage() throws RemoteException;
}

2. Write code for a Home interface and save the file as WelcomeHome.java. The
code for Welcome.java is given below:

import javax.ejb.*;
import java.rmi.RemoteException;

public interface WelcomeHome extends EJBHome


{
   public Welcome create() throws CreateException, RemoteException;
}

3. Write code for a bean class and save the file as WelcomeBean.java. The code for
WelcomeBean.java is given below:

import javax.ejb.*;

public class WelcomeBean implements SessionBean


{
   public void ejbActivate()
   {
      System.out.println(”ejbActivate()”);
   }

   public void ejbPassivate()


   {
      System.out.println(”ejbPassivate()”);
   }
   public void ejbCreate()
   {
      System.out.println(”ejbCreate()”);
   }

   public void ejbRemove()


   {
      System.out.println(”ejbRemove()”);
   }

   public void setSessionContext(SessionContext ctx)


   {
      System.out.println(”setSessionContext()”);
   }

   public String printMessage()


   {
      return “Welcome to your first EJB program. You have a long way to go.”;
   }
}

4. Set up a directory structure as follows:


5. Set the path and classpath in the environment. It is assumed that Java sdk is
installed at the location C:jdk1.3 and j2ee sdk is installed at C:j2sdkee1.3.1. The
path should be set as C:jdk1.3bin, The classpath should be set as
C:j2sdkee1.3.1libj2ee.jar;C:j2sdkee1.3.1bin;C:WelcomeServJavaFile;C:Welcome
Client;C:WelcomeServclasses;.;
6. Compile the Remote interface, the Home interface, the bean class, and the client
file. This will store the class files for the Remote interface, the Home interface,
and the bean class in the classes folder. The class file for the client will be stored
with the WelcomeClient.java file in the Client folder.
7. Now open a new window on the command prompt and start the J2EE server using
the j2ee -verbose command.
8. Start the deploytool utility by typing deploytool in a new command prompt
window. A new application deployment tool screen will be displayed.
9. To launch a new application, go to File>New>Application.
10. A new application screen will be displayed. Enter the name of the application as
C:WelcomeWelcomeApp.
11. Now the application deployment screen will be displayed as shown below:
12. To create a new JAR file, go to File>New>Enterprise Bean.
13. A screen showing a new enterprise bean wizard will be displayed. From the
combo box, select WelcomeApp. In the JAR display name, enter WelcomeApp.
Then click the Edit button.
14. A screen will be displayed showing the edit contents of the WelcomeAppJAR
screen. Select the class files from the tree. For this, select Serv under the
Welcome folder. Click the Add button. Then click the Ok button.
15. A screen will be displayed that will prompt to choose the type of enterprise bean.
Choose the Session bean radio button. Under the Session bean, choose the
Stateless radio button.
16. A screen will be displayed that will ask whether to use a Bean-Managed or
Container-Managed transaction. Select the Bean-Managed radio button. Click the
Finish button.
17. To deploy the application, go to tools and click Deploy on the Application
Deployment Tool screen.
18. A screen will be displayed showing Deploy welcomeApp. Select welcomeApp in
the Object to Deploy combo box. Select the Return Client Jar check box. Ensure
that the client Jar file name is C:WelcomeWelcomeAppClient.jar. Click the Next
button.
19. A screen will be displayed asking for the JNDI name. Enter the name that was
entered in the WelcomeClient code. The JNDI name in the code is
WelcomeJNDI. Click the Finish button.
20. The application will be deployed as shown below:
21. After the deployment is complete, click the Ok button.
22. Now, the client needs to be packaged. In the Application Deployment Tool
screen, go to File>New>Application Client.
23. The New Application Client wizard screen will be displayed as shown below:
Click the Edit button.
24. A screen saying Edit contents of Application client will be displayed.
25. Now from the tree, choose WelcomeClient.class from Client under C:Welcome.
Click the OK button, and then click the Next button. Click the Finish button.
26. Now it is time to run the application client. Open a new command prompt
window. Go to C:Welcome. Set the path for the application client using set
APPCPATH=WelcomeAppClient.jar. Press enter and then type the following
command:
27. runclient -client WelcomeApp.ear -name WelcomeClient -textauth

The user will be asked to enter a user name and a password. Enter user name as
user1 and password as user1. The printMessage() method will be invoked and the
output will be displayed.

You might also like