Accessing a session bean using dependency injection

A session bean has limited value by itself. To be useful it needs to be used by a client such as a servlet or JSF page. In this recipe we will see how to use dependency injection to use a session bean in a servlet.

Getting ready

The essential steps to access a session EJB using dependency injection include:

  1. Inject the EJB using the @EJB annotation
  2. Access its methods as needed

    First we need a session bean. To keep things simple, we will use the Salutation session EJB developed in the previous recipe. We will add our servlet to the SalutationApplication.

How to do it...

We will be developing a HyperText Transfer Protocol (HTTP) based servlet named SalutationServlet. This servlet will use the Salutation ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.