HomeDocumentation > Reference > Samples > Java EE sample applications > singletonejb-javaee6 - A simple JSF+Singleton+ejb injection application
{scrollbar}

Application overview

This sample demonstrates @Singleton and @EJB annotations described in EJB3.1 lite as well as the usage of JSF framework. @Singleton annotation defines a singleton session bean which can be shared across applications and with its states maintained during the application's lifecycle. @EJB annotation shows how to have another session bean injected to your backing beans.

Application content

singletonejb-javaee6 application consists of following list of packages and classes.

org.apache.geronimo.samples.javaee6.singletonejb.backbeans

  • AddCalculatorBackBean.java
  • SubtractCalculatorBackBean.java

org.apache.geronimo.samples.javaee6.singletonejb.sessionBeans

  • SingletonCalculator.java

The list of web application files in the application is depicted in the following.

|- WEB_INF

+- web.xml

+- geronimo-web.xml

|- index.html

|- index.xhtml

|- SubtractCal.xhtml

Application implementation

The web.xml defines the welcome-file of the web application index.html and declares the involvement of JSF framework with a standard servlet Faces Servlet for request processing.

xml <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>

geronimo-web.xml describes information about the project (e.g. module's unique identification, any dependencies) inside the <sys:environment> tag. It is a good idea to give this module some sort of unique identification, so that it can later be referenced by some other deployable application. The path specified in the* <context-root>* tag will be the first segment of the URL used to access this web application. So to access this web application the url will be http://<hostname>:<port>/Singletonejb-javaee6. Geronimo uses Apache MyFaces as JSF implementation and the component is started by default and available server-wide. Therefore you do no have to add the dependency for myFaces component.

xml <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2"> <sys:environment> <sys:moduleId> <sys:groupId>org.apache.geronimo.samples</sys:groupId> <sys:artifactId>singletonejb-javaee6</sys:artifactId> <sys:version>${version}</sys:version> <sys:type>car</sys:type> </sys:moduleId> <sys:dependencies/> <sys:hidden-classes/> <sys:non-overridable-classes/> </sys:environment> <context-root>/singletonejb-javaee6</context-root> </web-app>

index.xhtml uses the standard HTML tag library to render a form for user input and bind the components on the form with the managed beans properties respectively.

html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>Facelet Title</title> </h:head> <h:body> <h:form> <h:outputText value="Add Calculator" /> <hr/> <h:inputText value="#{AddCalculatorBackBean.currentInput}"/> <h:commandButton action="#{AddCalculatorBackBean.add}" value="Add"/> <br/> <h:outputText escape="false" value="#{AddCalculatorBackBean.output}"/> <h:commandLink value="Go to Subtract Calculator" action="#{AddCalculatorBackBean.goToSubtract}"/> </h:form> </h:body> </html>

AddCalculatorBackBean.java and SubtractCalculatorBackBean.java hold the form data and invoke a singleton session bean SingletonCalculator for the calculation. Also the JSF managed beans use @EJB annotation for bean injection. We can see two references of SingletonCalculator for Add and Subtract beans. Because both backing beans AddCalculatorBackBean and SubtractSingletonCalculatorBackBean refer to the same object identity, each calculation will be preformed on the result from last calculation.

50% javaAddCalculatorBackBean.java package org.apache.geronimo.samples.javaee6.SingletonEJB.backbeans; import org.apache.geronimo.samples.javaee6.SingletonEJB.sessionBeans.SingletonCalculator; import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name = "AddCalculatorBackBean") @RequestScoped public class AddCalculatorBackBean { @EJB private SingletonCalculator calculatorfacade; private String currentInput = "0.0"; private String output; /** Creates a new instance of AddCalculatorBackBean */ public AddCalculatorBackBean() { currentInput = "1.0"; ... } } 50% javaSubtractCalculatorBackBean.java package org.apache.geronimo.samples.javaee6.SingletonEJB.backbeans; import org.apache.geronimo.samples.javaee6.SingletonEJB.sessionBeans.SingletonCalculator; import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name = "SubtractSingletonCalculatorBackBean") @RequestScoped public class SubtractCalculatorBackBean { @EJB private SingletonCalculator calculatorfacade; private String currentInput = "0.0"; private String output; /** Creates a new instance of SingletonCalculatorBackBean */ public SubtractCalculatorBackBean() { currentInput = "1.0"; } public String subtract() { ... } }

SingletonCalculator.java do the actual calculation. It's a singleton session bean and can only be initialized once for all requests. It will be destroyed only when the application is stopped.

java import javax.ejb.Singleton; import java.text.DecimalFormat; @Singleton public class SingletonCalculator { String output = "Start the calculator process:<br/>"; double result = 0; public double add(double d) { double tmp = result + d; this.output += result + " + " + d + " equals " + tmp + "<br/>"; result = tmp; return result; } public String getOutput() { return this.output; } public double sub(double d) { double tmp = result - d; this.output += result + " - " + d + " equals " + tmp + "<br/>"; result = tmp; return result; } }

Get the source code

Please refer to Samples General Information for informations on obtaining and building the source for this and other samples.

Build the web application

Once all the sources get checked out the next step is to build singleton-javaee6 sample. It requires Maven 2 or above for building the binaries.

From the <singletonejb-javaee6_home> directory run the following command.

mvn clean install

This process will take a couple of minutes. The binaries will be generated in the corresponding target directory .

Deploy the web application

Deploying sample application is pretty straight forward as we are going to use the Geronimo Console.

  1. Scroll down to Deploy New from the Console->Navigation panel.
  2. Load singletonejb-javaee6-war-version.war from singletonejb-javaee6-war\target folder in to the Archive input box.
  3. Press Install button to deploy application in the server.

Test the web application

The application is visible at http://localhost:8080/singleton-javaee6.

Input a value and click Add for calculation.

Click Go to Subtract Calculator and input a value again, then click subtract. You will see the output is based on the result from the previews step.