We will use a singleton to support a game application. A PlayerBean
will be created to support the attributes of a player. We will assume that our game will never have more than one player so a singleton is an appropriate choice for this type of bean.
The process of creating a singleton bean uses two steps:
These methods will reflect the functionality desired for the singleton.
Begin this recipe by creating a Java EE application called SingletonExample
. Within the EJB module, create a package called packt
and add the singleton PlayerBean
. In the WAR module, create a servlet
package and add a GameServlet
servlet.
Let's start with the game bean which is declared as a singleton using the @Singleton annotation. This version of the bean maintains only the name of the player.
@Singleton public class PlayerBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Add the GameServlet
that follows. It is assumed that the GameServlet
is invoked from an HTML page that passes the player's name as a request parameter. The use of request parameters will be demonstrated after the servlet has been explained. The name passed is used to set the PlayerBean's
name and later is retrieved and displayed.
public class GameServlet extends HttpServlet { @EJB private PlayerBean player; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { player.setName(request.getParameter("name")); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet GameServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h3>Name: " + player.getName() + "</h3>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } }
Execute the servlet using the URL found in the following screenshot. It will invoke the servlet with"Edison" as its argument.
The servlet was able to access parameters sent using the HttpServletRequest
object's getParameter
method. In this example the name parameter was identified using the word name
. The name of the player returned from the getParameter
method was then used as the argument to the PlayerBean
class' setName
method. Next, the PlayerBean
class' getName
method was used to retrieve the name and then displayed.
The singleton session bean also supports the @PostConstruct and @PreDestroy annotations. The @PostConstruct annotation specifies which method to invoke after the bean has been instantiated but before any business methods are invoked. The @PreDestroy annotation designates the method to execute before the bean is destroyed.
In this example we assumed that a support file or possible database entry needs to be created before any of the singleton's methods are executed. In the initialize
method these files or database operations are performed. Likewise, in the destroy
method, any cleanup operations are performed.
@Singleton public class PlayerBean { String name; @PostConstruct private void initialize() { this.name = "Ultimate Software Warrior"; // Initialize player file/database System.out.println("PlayerBean initialized " + this.name); } @PreDestroy private void destroy() { // Clean up player files/database System.out.println("PlayerBean destroyed " + this.name); } ... }
18.191.157.197