Show TOC

Developing the Business LogicLocate this document in the navigation structure

Use

Now, you need to create and implement the Address and Student classes, and the session bean StudsBean that defines the business methods required in our sample scenario.

Procedure

Creating and Implementing the Address class

  1. In the Project Explorer, expand your EJB DC

  2. In the context menu of ejbModule , choose New → Class.

  3. Enter com.sap.tc.cm.ejb.example in the Package field. Enter Address in the Name field. Choose Finish . You have created the Address class.

  4. To implement the class, open the Java editor and add the following source code:

    package com.sap.tc.cm.ejb.example;

    public class Address {

    private String town;

    private String street;

    public String getStreet() {

    return street;

    }

    public void setStreet(String street) {

    this.street = street;

    }

    public String getTown() {

    return town;

    }

    public void setTown(String town) {

    this.town = town;

    }

    }

Creating and Implementing the Student class

  1. In an analogous manner, create a Student class.

  2. To implement it, use the following source code:

    package com.sap.tc.cm.ejb.example;

    public class Student {

    private String name;

    private Address address;

    private int age;

    public Address getAddress() {

    return address;

    }

    public void setAddress(Address address) {

    this.address = address;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    }

Creating and Implementing the Session Bean

  1. In the Project Explorer , expand your EJB DC.

  2. In the context menu of ejbModule , choose New → Other . Expand EJB and choose EJB Session Bean 3.0 . Choose Next .

  3. Enter StudsBean in the EJB Name field and com.sap.tc.cm.ejb.example in the Default EJBPackage field . Set the Local option and choose Next .

  4. The next screen confirms that the Business Interface StudsLocal is automatically created. Choose Finish

  5. To implement the StudsBean session bean, use the following source code:

    package com.sap.tc.cm.ejb.example;

    import javax.ejb.Stateless;

    @Stateless(name="StudsBean")

    public class StudsBean implements StudsLocal {

    public Student getStudent(String name) {

    // creating new Student

    Student student = new Student();

    student.setName(name);

    student.setAge(25);

    // creating new Address and setting it to the Student

    Address a = new Address();

    a.setTown("MyTown");

    a.setStreet("MyStreet");

    student.setAddress(a);

    return student;

    }

    }

  6. Switch to StudsLocal.java and insert the following source code:

    package com.sap.tc.cm.ejb.example;

    import javax.ejb.Local;

    @Local

    public interface StudsLocal {

    public Student getStudent(String name);

    }

Result

You have created and implemented all classes and interfaces you need for your EJB DC.