Preloader image

The simple application contains a CDI managed bean CalculatorBean, which uses the Calculator EJB to add two numbers and display the results to the user. The EJB is injected in the managed bean using @Inject annotation.

You could run this in the latest Apache TomEE snapshot

The complete source code is below but lets break down to look at some smaller snippets and see how it works.

A little note on the setup:

As for the libraries, myfaces-api and myfaces-impl are provided in tomee/lib and hence they should not be a part of the war. In maven terms, they would be with scope `provided'

Also note that we use servlet 2.5 declaration in web.xml

And we use 2.0 version of faces-config

To make this a cdi-aware-archive (i.e bean archive) an empty beans.xml is added in WEB-INF

   <?xml version="1.0" encoding="UTF-8"?>

   <beans xmlns="" xmlns:xsi=""

We’ll first declare the FacesServlet in the web.xml

    <servlet-name>Faces Servlet</servlet-name>

FacesServlet acts as the master controller.

We’ll then create the calculator.xhtml file.

       <h:outputText value='Enter first number'/>
       <h:inputText value='#{calculatorBean.x}'/>
       <h:outputText value='Enter second number'/>
       <h:inputText value='#{calculatorBean.y}'/>
       <h:commandButton action="#{calculatorBean.add}" value="Add"/>

Notice how we’ve used the bean here. By default, the bean name would be the simple name of the bean class with the first letter in lower case.

We’ve annotated the CalculatorBean with @RequestScoped. So when a request comes in, the bean is instantiated and placed in the request scope.

Here, getX() method of calculatorBean is invoked and the resulting value is displayed. x being a Double, we rightly should see 0.0 displayed.

When you change the value and submit the form, these entered values are bound using the setters in the bean and then the commandButton-action method is invoked.

In this case, CalculatorBean#add() is invoked.

Calculator#add() delegates the work to the ejb, gets the result, stores it and then returns what view is to be rendered.

The return value success is checked up in faces-config navigation-rules and the respective page is rendered.

In our case, result.xhtml page is rendered where use EL and display the result from the request-scoped calculatorBean.

Source Code


import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;
import jakarta.inject.Inject;

public class CalculatorBean {
    Calculator calculator;
    private double x;
    private double y;
    private double result;

    public double getX() {
        return x;

    public void setX(double x) {
        this.x = x;

    public double getY() {
        return y;

    public void setY(double y) {
        this.y = y;

    public double getResult() {
        return result;

    public void setResult(double result) {
        this.result = result;

    public String add() {
        result = calculator.add(x, y);
        return "success";


package org.superbiz.jsf;

import jakarta.ejb.Stateless;

public class Calculator{

    public double add(double x, double y) {
        return x + y;


MyProject web.xml

Faces Servlet jakarta.faces.webapp.FacesServlet 1

Faces Servlet *.jsf

index.jsp index.html


<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN''''>;


<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN''''>;

    <h:commandLink action="back">
        <h:outputText value="Home"/>


/calculator.xhtml success /result.xhtml

/result.xhtml back /calculator.xhtml