Creating the Registration Application

A RegistrationApplication is developed in this recipe. It provides the ability of attendees to register for a conference. The application will record their personal information using an entity and other supporting EJBs. This recipe details how to create this application.

Getting ready

The RegistrationApplication consists of the following classes:

  • Attendee An entity representing a person attending the conference
  • AbstractFacade A facade-based class as detailed in Chapter 4,
  • AttendeeFacade The facade class for the Attendee class
  • RegistrationManager Used to control the registration process
  • RegistrationServlet The GUI interface for the application

The steps used to create this application include:

  1. Creating the Attendee entity and its supporting classes
  2. Creating a RegistrationManager EJB to control the registration process
  3. Creating a RegistrationServlet to drive the application

    The RegistrationManager will be the primary vehicle for the demonstration of interceptors.

How to do it...

Create a Java EE application called RegistrationApplication. Add a packt package to the EJB module and a servlet package in the application's WAR module.

Next, add an Attendee entity to the packt package. This entity possesses four fields: name, title, company, and id. The id field should be auto generated. Add getters and setters for the fields. Also add a default constructor and a three argument constructor for the first three fields. The major components of the class are shown below without the getters and setters.

@Entity
public class Attendee implements Serializable {
private String name;
private String title;
private String company;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Attendee() {
}
public Attendee(String name, String title, String company) {
this.name = name;
this.title = title;
this.company = company;
}
}

Next, add an AttendeeFacade stateless session bean which is derived from the AbstractFacade class. Details of the AbstractFacade can be found in the Chapter 4, Creating an entity facade recipe. The AbstractFacade class is not shown here.

@Stateless
public class AttendeeFacade extends AbstractFacade<Attendee> {
@PersistenceContext(unitName = "RegistrationApplication-ejbPU")
private EntityManager em;
protected EntityManager getEntityManager() {
return em;
}
public AttendeeFacade() {
super(Attendee.class);
}
}

Add a RegistrationManager stateful session bean to the packt package. Add a single method, register, to the class. The method should be passed three strings for the name, title, and company of the attendee. It should return an Attendee reference. Use dependency injection to add a reference to the AttendeeFacade. In the register method, create a new Attendee and then use the AttendeeFacade class to create it. Next, return a reference to the Attendee.

@Stateful
public class RegistrationManager {
@EJB
AttendeeFacade attendeeFacade;
Attendee attendee;
public Attendee register(String name, String title, String company) {
attendee = new Attendee(name, title, company);
attendeeFacade.create(attendee);
return attendee;
}
}

In the servlet package of the WAR module, add a servlet called RegistrationServlet. This servlet will follow the same structure as detailed in the Chapter 1, Accessing a session bean using dependency injection recipe. Use dependency injection to add a reference to the RegistrationManager. In the try block of the processRequest method, use the register method to register an attendee and then display the attendee's name.

public class RegistrationServlet extends HttpServlet {
@EJB
RegistrationManager registrationManager;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet RegistrationServlet</title>");
out.println("</head>");
out.println("<body>");
Attendee attendee = registrationManager.register("Bill Schroder", "Manager", "Acme Software");
out.println("<h3>" + attendee.getName() + " has been registered</h3>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
...
}

Execute the servlet. The output should appear as shown in the following screenshot:

How to do it...

How it works...

The Attendee entity holds the registration information for each participant. The RegistrationManager session bean only has a single method at this time. In later recipes we will augment this class to add other capabilities. The RegistrationServlet is the client for the EJBs.

See also

See Chapter 1, Accessing a session bean using dependency injection recipe for details on the creation of a servlet and Chapter 4, Creating an entity facade recipe for details about the entity facade.

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

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