Creating a simple session EJB

In this recipe, we will create a simple session bean that returns either a formal or an informal salutation based on a string parameter. In the next recipe we will see how to invoke this EJB from a servlet.

Specifically we will create a stateless session bean. A stateless session bean does not remember its previous invocations. A user will call the bean and the bean will return a result. A stateless bean is not good for maintaining the state of an interactive session such as required to maintain a list of purchases. However, it is useful for one-time calculations. Our bean returns one of two simple greetings.

Getting ready

The creation of a session EJB consists of two steps:

  1. Create the EJB class annotated with a session bean annotation
  2. Add business method to the EJB

    These steps have been made easier through the use of annotations.

How to do it...

In this example we will use the @Stateless annotation. Create a Java EE application called SalutationApplication. The application should have both a SalutationApplication-ejb and a SalutationApplication-war module. Add the following Stateless session bean to a package called packt and name the bean Salutation.

Note

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

In this EJB we will add two simple business methods which return different types of greetings.

package packt;
import javax.ejb.Stateless;
@Stateless
public class Salutation {
public String getFormalSalutation(String name) {
return "Dear " + name;
}
public String getInformalSalutation(String name) {
return "Hi " + name;
}
}

How it works...

The process of creating a stateless session EJB involved defining a class to support the desired functionality of the bean and using the @Stateless annotation to specify the class as a stateless session EJB. In order to use the @Stateless annotation we needed to use an import statement.

The two class methods took a string as an argument and returned the string prefixed with either a formal or informal greeting.

There's more...

Annotation is at the heart of the EJB declaration. Annotations are embedded in the application's source code and allow further processing of the source code either:

  • Before a source code file is compiled
  • During development by a compiler, IDE deployment tool or similar application
  • During the execution of the application

An annotation can be applied to many different program elements including but not limited to classes, methods, parameters, fields, and packages. In essence, annotations are used to provide metadata about an element. Metadata is usually defined as data about data. We may have a program element, such as a method, requiring in some circumstances additional information about how it is used.

For example, we might want a particular method to correctly override a base class method. The @Override annotation does exactly this. This annotation is useful should we accidentally mistype the method name and fail to actually override the annotation. We may think we are overriding it but we are not. If we use the @Override annotation and fail to actually override the method a syntax error is issued by the compiler.

The @Stateless annotation provides information to configure the environment and treat the class as an EJB. The annotation is used at runtime and has attributes affecting its visibility. In particular, the mappedName attribute is used in the generation of the bean's JNDI name.

@Stateless(mappedName="salutationBean")
public class Salutation {

The bean can now be referenced in certain contexts using the name: salutationBean. An alias can also be defined for an EJB in the project's ejb-jar.xml file.

Session beans are not limited to a stateless form. The use of the @Stateful annotation declares a bean to be stateful. This means field variables can maintain their values as a user interacts with the application over a period of time called a session. In addition, session beans frequently interact with entities to persist data to a database.

A session bean can also have a local, remote and no-interface client view. The interface used determines the intent and scope of the bean. The no-interface view is new to EJB 3.1. This approach allows the developer to use EJB without having to declare a business interface. In later recipes we will see how these aspects of session beans are used.

See also

The next recipe illustrates how we can use the session bean in other parts of our application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.36