• US Inquiries / 1 877 517 6540
  • Canadian Inquiries / 1 866 206 4644
submit to reddit

April 22, 2015 / WASKB-017

J2EE Tutorial - Basic J2EE Development Using Rational Application Developer v6 Tutorial


This tutorial will help you understand how to develop Session EJB, Servlets and JSP files using RAD v6. You will develop a very simple application that calculates the square root of a number. If the number is missing or negative, system displays an error page.

Launch and Configure RAD v6

Start RAD v6 (Start Menu Programs->IBM Rational->IBM Rational Application Developer V6.0->Rational Application Developer).

Enter a short directory name as the workspace root folder. Click on OK.

System will show the Welcome screen.

At the bottom right hand corner, click on the Enable/Disable roles icon. Click on the Advanced J2EE role as shown above. System shows/hides menu items based on the selected roles.

Close the Welcome screen. System will show the J2EE perspective.

Create the Projects

In the Project Explorer view, right click on Enterprise Applications and select New->Enterprise Application Project.

Enter Test Application as the name of the project. Click on the Show Advanced button.

Note that the default J2EE level is 1.4 and that the new project will be targeted to WebSphere Application Server v6.0. The target server determines the J2EE JAR files that are added to the project's class path. In RAD v6, a test server is automatically created.

Click on Finish.

In the Project Explorer view, right click on EJB Projects and select New->EJB Project. Enter Test EJB as the project's name. Click on the Show Advanced button. As the EAR project, select Test Application.

Click on Next. Accept the default name of the EJB Client project and JAR file name.

What is a EJB client project? It is a plain Java project that holds the remote/local home and component interfaces, and generated stub classes. The project contains minimal classes and interface for a EJB client application to compile and execute.

Click on Finish.

How does the EJB project refer to the home and component interfaces in the EJB client project? The EJB client project is setup in the enterprise application project (Test Application) as a utility JAR project. The EJB project - Test EJB - has a Java JAR dependency on the client project. (The concept of a utility JAR project is fairly old and a discussion of this is beyond the scope of this tutorial).

In the Project Explorer view, right click on Dynamic Web Projects and select New->Dynamic Web Project. Enter Test Web as the project's name. Click on the Show Advanced button. Set the EAR project to Test Application. Change the context root to testweb.

Click on Next.

Switch on the check box next on the Test_EJBClient.jar file. The EJB client layer (the Web project in our case) should build a dependency on the client JAR file only and never to the actual EJB JAR file. The EJB JAR file is installed only in the application server machine where the EJB module is deployed. The client JAR file can be freely distributed anywhere.

Click on Next. In the Features secreen you can add support for Struts or JSTL. Our simple application does not need any additional features. Click on Finish at this point. In the Confirm Perspective Switch dialog, click on No.

Build the Model Layer

Our model layer will be very simple. We will have a JavaBean that will hold the results of the square root operation. We will have a session EJB that will contain the business logic.

The JavaBean is a Data Transfer Object (DTO). Both the EJB and client layer need access to this class. For a small application, it is convenient to create such common classes in the EJB client project. The client project is already setup as a utility JAR project and the EJB and Web project already has dependency setup to that project. Also, this way, you need to distribute just the client JAR file to the client layer. For more complex applications, you should create Java projects to hold common code and manually configure them as utility JAR projects.

Expand Other Projects and right click on Test EJBClient. Select New->Package. Enter com.webage.beans as the package name and click on Finish. Right click on the newly created package and select New->Class. Enter SquareRootResultBean as the class name and click on Finish. Implement the java.io.Serializable interface from the class. Add two member variables as shown below.

public class SquareRootResultBean implements java.io.Serializable {  
 private double sourceNumber;  
 private double resultValue;  

Right click anywhere in the editor and select Source->Generate Getters and Setters. Click on the Select All button. Click on OK. Save and close the SquareRootResultBean.java file.

Now, we will add the session bean. Expand EJB Projects->Test EJB. Right click on Deployment Descriptor : Test EJB. Select New->Enterprise Bean.

Select Session bean as the type of EJB to be created. Enter SimpleSession as the bean name. Set the default package to com.webage.ejbs. Click on Next. We will accept all default values in this screen (stateless nature and remote interface only). Click on Finish.

System will add the EJB to the class diagram. Save and close the class diagram.

Expand Test EJB->ejbModule->com.webage.ejbs. Double click on SimpleSessionBean.java. Add a new method to the class as shown below.

public SquareRootResultBean getSuqareRoot(double val) {  
      SquareRootResultBean r = new SquareRootResultBean();  

      r.setResultValue(Math.pow(val, 0.5));  

     return r;  

Press Control+Shift+O to import necessary packages. Press Control+S to save and compile the class.

From the Outline view, right click on the getSquareRoot method and select Enterprise Bean->Promote to Remote Interface.

We are done with the EJB development. Now, we must generate the deployment code. In the Project Explorer view, right click on Test EJB and select Deploy.

After the code generation completes, you can inspect the generated classes in the Test EJB and Test EJBClient projects. The client project has the stub classes. The other generated classes go into the EJB project.

Unit Test the EJB

From the Servers view, select the WebSphere Application Server v6.0 server and start it. (Note: Unlike, WSAD, in RAD, a server must be started before we can add projects to it. If the server is not started, RAD will start it in debug mode. It is faster for us to start the server in non-debug mode and then add the projects to it).

Wait for the server to start. Ignore a few error messages shown in the console. Right click on the server and select Add or Remove Projects. Add the Test Application project to the server and click on Finish. It takes a second or two to deploy the project.

Right click on the server and select Run universal test client.

Click on JNDI Explorer. Expand ejb->com->webage->ejbs and click on SimpleSessionHome as shown above.

On the left hand side, expand SimpleSession->SimpleSessionHome. Click on create(). Click on the Invoke button. This will obtain a reference to the remote interface. Click on the Work with Object button.

In the left hand side, expand SimpleSession 1 and click on the getSquareRoot method.

On the right hand side, enter 4.0 as the method parameter value. Click on Invoke. This will return a SquareRootResultBean object. Click on the Work with Object button.

On the left hand side, expand Objects->SquareRootResultBean. Click on the getResultValue() method. Click on Invoke. Make sure that you see 2.0 as the return value of the method.

With our model layer unit tested, we can proceed to build the client layer - the web layer in our case.

Develop the Controller

In the Project Explorer view, expand Dynamic Web Projects->Test Web. Right click on Deployment Descriptor : Test Web. Select New->Reference.

Select EJB Reference and click on Next.

Enter SimpleSessionRef as the reference name and select the SimpleSession EJB as shown above. Click on Finish.

Right click on Deployment Descriptor : Test Web. Select New->Servlet. Enter SquareRootServlet as the name of the Servlet. Click on Next. Enter com.webage.servlets as the package name. Click on Finish.

Expand Java Resources->JavaSource->com.webage.servlets. Double click on SquareRootServlet.java. Add the following import statements.

import javax.naming.*;  
import com.webage.beans.*;  
import com.webage.ejbs.*;

Change the function argument parameter names of the doGet methods as shown below.

protected void doGet(HttpServletRequest **request**, HttpServletResponse **response**) throws ServletException, IOException {

Enter the following code in the doGet method.

protected void doGet(HttpServletRequest request,  
      HttpServletResponse response) throws ServletException, IOException {  
      double val = 0.0;  
      String errorMessage = null;  

     try {  
           val = Double.parseDouble(request.getParameter("value"));  
      } catch (NumberFormatException e) {  
           errorMessage = "Please enter a valid number.";  
      } catch (NullPointerException e) {  
           errorMessage = "Please enter a value.";  

     if (errorMessage != null) {  
           request.setAttribute("errorMessage", errorMessage);  
           request.getRequestDispatcher("/failure.jsp").forward(request, response);  

     try {  
           SimpleSessionHome sh;  
           SimpleSession s;  
           Context ctx = new InitialContext();  

          Object o = ctx.lookup("java:comp/env/SimpleSessionRef");  
           sh = (SimpleSessionHome) PortableRemoteObject.narrow(o,  
           s = sh.create();  

          SquareRootResultBean rb = s.getSuqareRoot(val);  
           request.setAttribute("resultBean", rb);  
           request.getRequestDispatcher("/success.jsp").forward(request, response);  
      } catch (Exception e) {  
          throw new ServletException("doGet failed", e);  

Press Control+Shift+O to resolve the import statements. Save and compile the class.

Develop the View

In the Project Explorer view, right click on Test Web and select New->JSP File. Enter success.jsp as the file name and click on Finish.

Switch to the Source view of the editor. Set the contents of the <BODY> tag as follows.

 <P>Square root of ${resultBean.sourceNumber} is ${resultBean.resultValue}.</P>  
 <form action="SquareRootServlet">  
 Number: <input type="text" name="value"> <input type="submit" value="Calculate">  

Note: JSP 2.0 allows us to insert JSP Expression Language (EL) statements. For example, ${resultBean.sourceNumber} EL will show the sourceNumber property of the resultBean object that was saved in the request context. JSP EL can significantly simplify JSP development.

Save and close the file.

Create another JSP file called failure.jsp. Switch to the Source view of the editor. Set the contents of the <BODY> tag as shown below.

   <form action="SquareRootServlet">  
   Number: <input type="text" name="value"> <input type="submit" value="Calculate">  

Save and close the file.

Test the Application

First, we need to associate the web project with the WebSphere v6 test server. Right click on the Test Web project and select Properties. Select the Server property.

Select WebSphere Application Server v6.0 as the default server runtime. Click on OK.

Expand Test Web->Deployment Descriptor : Test Web->Servlets. Right click on SquareRootServlet and select Run->Run on Server.

Test the error conditions and success condition.


In this tutorial, we learned the following items.

  1. How to create the J2EE projects in RAD v6.
  2. How to create a Session bean with a remote interface.
  3. How to add a J2EE application project to a test server.
  4. How to unit test a Session EJB.
  5. How to develop a Servlet that uses EJB.
  6. How to unit test a Servlet.


  1. WA1283 J2EE Programming Using Rational Application Developer (RAD) v6