To create a Java EE web application with JSF, build a Maven WAR project, map FacesServlet to *.xhtml in web.xml, write a CDI bean annotated with @Named and @RequestScoped, and create an .xhtml page that binds components to the bean with expression language. JSF is now called Jakarta Faces, so current code uses jakarta.faces.* packages and the jakarta.faces.html tag library namespace.
This guide uses Jakarta EE 10 (Jakarta Faces 4.0) and Java 17 or newer. The older javax.faces approach shown in many tutorials only works on Java EE 8 and earlier servers.
JSF moved from Oracle to the Eclipse Foundation, and the package prefix changed with Jakarta EE 9:
| Era | Package and namespace | Typical server |
|---|---|---|
| JSF 2.3 (Java EE 8) | javax.faces, http://xmlns.jcp.org/jsf/html | Older WildFly, Payara 5 |
| Faces 3.0 (Jakarta EE 9) | jakarta.faces, http://xmlns.jcp.org/jsf/html | Transitional |
| Faces 4.0 (Jakarta EE 10) | jakarta.faces, jakarta.faces.html | WildFly 27+, Payara 6, Open Liberty |
Also note that @ManagedBean is deprecated and removed from current Faces versions. Use CDI beans with @Named and a scope instead.
You need a JDK, Maven and a Jakarta EE 10 server such as WildFly, Payara or Open Liberty. The server supplies Faces and CDI, so the API dependency is provided:
If you deploy to plain Tomcat 10.1, which has no Faces or CDI, you must add a Faces implementation (Eclipse Mojarra) and a CDI implementation (Weld) to the WAR instead of using provided.
Create src/main/webapp/WEB-INF/web.xml. Faces servlets can register themselves on a compliant server, but an explicit mapping keeps the behaviour predictable:
Add an empty beans.xml (with bean-discovery-mode="all" or annotated) under WEB-INF if your server needs it for CDI to scan the archive. In CDI 4 an annotated bean such as @Named @RequestScoped is discovered by default.
A backing bean holds the data and actions behind a page. Place it in src/main/java/com/example/HelloBean.java:
@Named exposes the bean to expression language under the name helloBean. The scope decides its lifetime:
| Scope | Lifetime |
|---|---|
@RequestScoped | One HTTP request |
@ViewScoped | While the user stays on the same view (needs Serializable) |
@SessionScoped | The user session (needs Serializable) |
@ApplicationScoped | The whole application |
In Faces 4.0, use jakarta.faces.view.ViewScoped for view scope, not the old javax annotation.
Create src/main/webapp/index.xhtml. Facelets pages are XHTML, so tags must be well formed:
The input is bound to helloBean.name, and the button calls greet(). Faces runs its lifecycle on submit: it converts and validates the input, updates the bean, invokes the action, and renders the response. With required="true", an empty field shows an error through h:message, and the action does not run.
An action method can return the name of the next view. If greet() returned the string "result", Faces would show result.xhtml. For simple links, use h:link with the outcome attribute:
With Faces 2.2 and newer, implicit navigation like this needs no faces-config.xml entries. Add faces-redirect=true to the outcome ("result?faces-redirect=true") for a redirect, which avoids duplicate form submissions on reload.
mvn clean package.target/jsf-web-app-1.0.0.war to the deployment folder of your server.http://localhost:8080/jsf-web-app-1.0.0/index.xhtml and submit the form.Entering "Ada" displays:
Yes, as Jakarta Faces. Version 4.0 is part of Jakarta EE 10, and Faces 4.1 belongs to Jakarta EE 11.
Check that the FacesServlet mapping matches the URL, that the page is under src/main/webapp, and that you used the context path of the deployed WAR.
The bean is probably not discovered by CDI. Confirm that it has @Named and a scope, that the imports use jakarta.*, and that your server runs CDI.
Yes. PrimeFaces and other component libraries add many ready-made components. Pick a release that supports the jakarta namespace.
Set up a Maven WAR with the Jakarta EE API as provided, map FacesServlet to *.xhtml, write a @Named CDI bean with a scope, and bind it from a Facelets page using the jakarta.faces.html namespace. Avoid the old javax.faces imports and @ManagedBean, which belong to earlier releases.