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.
The RegistrationApplication
consists of the following classes:
Attendee
An entity representing a person attending the conferenceAbstractFacade
A facade-based class as detailed in Chapter 4,AttendeeFacade
The facade class for the Attendee
classRegistrationManager
Used to control the registration processRegistrationServlet
The GUI interface for the applicationThe steps used to create this application include:
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:
3.137.41.205