Show TOC

Creating and Implementing an EJB ProjectLocate this document in the navigation structure

Use

In this tutorial, you first have to create and implement an EJB project. This provides you with the service that is used by the application client.

Prerequisites

The Start of the navigation path Window Next navigation step Open Perspective Next navigation step Other Next navigation step Java EE End of the navigation path perspective is opened.

Procedure

1. Create the EJB Project

You develop the EJB classes in an EJB project in the Java EE perspective.

  1. Choose Start of the navigation path File Next navigation step New Next navigation step Project End of the navigation path from the main menu.

  2. Select Start of the navigation path EJB Next navigation step EJB Project End of the navigation path, then choose Next .

  3. Enter CalculatorServiceEJB in the Project Name field.

  4. Leave the Default Configuration for SAP Libraries in the Configurations field. The default configurations include the EJB Module 3.0 and Java 6.0 project facets.

  5. Select Add Project to an EAR . Enter CalculatorServiceEAR in the EAR Project Name field. This instantly creates the enterprise project. You can use also an already existing enterprise project.

  6. Choose Finish .

Result

The EJB project and its corresponding Enterprise Application project appear in the Project Explorer .

2. Implement the Session Bean

To use the calculator service from application clients, you must provide a remote interface.

The interface itself is a normal Java interface (also known as POJI - Plain Old Java Interface).

Implementing the Interface

  1. Copy the Java source below.

  2. Expand the project CalculatorServiceEJB .

  3. In the context menu of ejbModule folder, choose Paste .

                         package com.sap.blogs.appclient.calculator.ejb;
    
    /**
     * Simple calculator with a single operation - addition
     */
    public interface CalculatorRemote {
            /**
             * Addition operation
             * 
             * @param a
             *            the first addend
             * @param b
             *            the second addend
             * @return the sum of the two addends
             */
            long add(int a, int b);
    }
    
                      

The implementation of this interface is a simple Java object. The annotation @Stateless means that the EJB bean is stateless and the annotation @Remote indicates that the CalculatorRemote.class is a remote interface for this bean.

Implementing the Session Bean Class

  1. Copy the Java source below.

  2. Expand the project CalculatorServiceEJB .

  3. In the context menu of ejbModule folder choose Paste .

                         package com.sap.blogs.appclient.calculator.ejb;
    
    import javax.ejb.Remote;
    import javax.ejb.Stateless;
    
    @Stateless
    @Remote(CalculatorRemote.class)
    public class CalculatorBean {
            public CalculatorBean() {
            }
    
            public long add(int a, int b) {
                    return a + b;
            }
    }
    
                      

Result

You have implemented the session bean class.