JSF - PrimeFaces Hello World Example using Apache Tomcat and Maven

2 minute read

PrimeFaces is an open source component library for JavaServer Faces (JSF). It provides a collection of mostly visual components (widgets) that can be used by JSF programmers to build the UI for a web application. An overview of these widgets can be found at the PrimeFaces showcase.

Apache Tomcat, is an open-source web server developed by the Apache Software Foundation. It implements several Java EE specifications and provides a “pure Java” HTTP web server environment for Java code to run in. Tomcat is released under Apache License 2.0 and is open-source software.

The following post illustrates a basic example in which we will configure, build and run a Hello World PrimeFaces example using Tomcat and Maven.

Tools used:

  • JSF 2.2
  • PrimeFaces 6.1
  • Tomcat 7
  • Maven 3.5

The code is built and run using Maven. Specified below is the Maven POM file which contains the needed dependencies for JSF and PrimeFaces.

In order to run the Hello World PrimeFaces application, a servlet container is needed and in this example, the Apache Tomcat implementation will be used. The deployment of the code and application server will be fully automated using the tomcat7-maven-plugin.

The Tomcat plugin takes as input two configuration parameters so that the HTTP listener port is set to “9090” and the context path is set to “codenotfound”.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


  <name>JSF - PrimeFaces Hello World Example using Apache Tomcat and Maven</name>




    <!-- Servlet -->
    <!-- JSF -->
    <!-- PrimeFaces -->

      <!-- tomcat7-maven-plugin -->

The remaining code of the example is identical to a previous PrimeFaces Hello World example running on Jetty. Feel free to check it out if you would like to know more details.

In order to run the above example open a command prompt and execute following Maven command:

mvn tomcat7:run

Maven will download the needed dependencies, compile the code and start a Tomcat instance on which the web application will be deployed. The result should be the following startup trace ending with: 'Starting ProtocolHandler'.

[INFO] Running war on http://localhost:9090/codenotfound
[INFO] Using existing Tomcat server configuration at c:\code\jsf-primefaces\jsf-primefaces-apache-tomcat\target\tomcat
[INFO] create webapp with contextPath: /codenotfound
dec 30, 2017 7:08:49 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-9090"]
dec 30, 2017 7:08:49 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
dec 30, 2017 7:08:49 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
dec 30, 2017 7:08:51 AM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.2.15 ( 20171010-0603 637515cda4a29e3ba435e847fa14d55f2fff71a7) for context '/codenotfound'
dec 30, 2017 7:08:51 AM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.
dec 30, 2017 7:08:52 AM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
INFO: Running on PrimeFaces 6.1
dec 30, 2017 7:08:52 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-9090"]

Open a web browser and enter following URL: http://localhost:9090/codenotfound/. The below page should be displayed:

jsf primefaces apache tomcat hello world example

Enter a first and last name and press the Submit button. A pop-up dialog will be shown with a greeting message.

jsf primefaces apache tomcat hello world example greeting

github mark If you would like to run the above code sample you can get the full source code here.

This concludes the PrimeFaces on Apache Tomcat example. If you found this post helpful or have any questions or remarks, please leave a comment.

Leave a comment