EJB in WAR, CDI and JPA Sample: Online Polling


The following sample provides an application consisting of a servlet, stateless session bean, a JSP, two JPA entities, and an interceptor.


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.


  1. Install the packaged server by typing ‘java -jar OnlinePollingSample.jar’ and following the instructions.
  2. 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.
  3. 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).
  4. In your browser, you should see the message:
    Please select one of the options below.

    This is the administration view.

  5. 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.
  6. 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 (copyright notice for this sample)
          • readme.html (these instructions)
          • server.xml (sample server using Derby shared library)
          • dropins
            • OnlinePollingWeb.war (sample Web application)
      • shared
        • config
          • OnlinePollingSampleDerbyLibs.xml (shared library configuration for Derby)
        • resources
          • OnlinePollingSampleDerbyLibs (pre-requisite Derby Library – downloadable during installation)