EJB in WAR, CDI and JPA Sample: Online Polling

This sample is a single WAR module that provides two user interface views.

  1. An online polling view that displays a question, a list of answers to choose from, and current voting results.
  2. An administration view that provides options for voting data to be initialized (empty, default seed data, or from the database if it has been populated). The administration view is always shown on startup, to allow an initialization preference to be set. It can always be returned to by clicking the "Admin" button from the online polling view.
This sample demonstrates the technologies of packaging an EJB in a WAR, Contexts and Dependency Injection (CDI), and Java Persistence API (JPA). The application consists of a servlet, stateless session bean, a JSP, two JPA entities, and an interceptor. The servlet uses CDI annotations to inject the stateless session bean, and then call various business methods declared by the bean. The stateless session bean (PollBean) uses a JPA EntityManager to perform queries and updates to the database. Before the bean populates the database, the interceptor (PollInterceptorImpl) uses a JPA EntityManager to clear the database. Finally, the servlet includes a small JSP to demonstrate CDI, by referencing the stateless session bean from within an EL expression to generate output.

This sample can be installed onto runtime versions and later.

Installation Instructions

Online installation (requires or later):

  1. Install the sample by running the command below from the bin directory of your server installation:
    installUtility install OnlinePollingSample

Any missing features required by the server will be installed for you.

Offline installation:

  1. Download the OnlinePollingSample.jar using the download button at the top of the page and place it in the root of your server installation.
  2. Extract the sample into the Liberty installation by running the command below from the root of your server installation:
    java -jar OnlinePollingSample.jar

Any missing features required by the server will need to be installed separately.

Running The Application

  1. Start the OnlinePollingSample server by running the command 'bin/server run OnlinePollingSample' from the root of the Liberty profile installation, and confirm the application started by looking in the console output:
    CWWKZ0001I: Application OnlinePollingSampleWeb started in XX.XX seconds.
  2. In your browser, enter the URL for the application: http://localhost:9123/OnlinePollingSampleWeb/ (where port 9123 assumes the httpEndpoint provided in the sample server.xml has not been modified).
  3. In your browser, you should see the message:
    Please select one of the options below.
    This is the administration view.
  4. To vote simply select the radio button of the appropriate answer and clicks "Vote!". The page will automatically be refreshed to display the latest voting results, and automatically commits the voting results to the database with each vote.
  5. After selecting how to initialized the voting data from the administration screen, the online polling view will appear.


The OnlinePollingSampleWeb.war file contains the source and binaries for the application, and can be used to import it to Eclipse.

This sample does not include the open source dependencies. The installer will provide a list of the open source pre-requisite libraries and where they can be downloaded from. To help obtaining these libraries, the installer provides the option to download them on your behalf.

Sample Structure

  • wlp
    • usr
      • servers
        • OnlinePollingSample
          • Copyright.txt
          • dropins
            • OnlinePollingSampleWeb.war
          • lafiles
            • LA_en
            • LI_en
          • readme.html
          • server.xml
      • shared
        • config
          • OnlinePollingSampleDerbyLibs.xml
        • resources
          • OnlinePollingSampleDerbyLibs
            • derby-