Join Patrick Royal for an in-depth discussion in this video What are Faces?, part of Java EE: Servlets and JavaServer Faces.
What is JavaServer Faces technology? Whereas a servlet controls the back-end sequence of requests to and from a server, Faces define the user interface. In a sense, they act as the public face of the application. As you might expect, they're commonly used alongside servlets to help the end user efficiently understand and manipulate the server. A typical workflow with a JavaServer Faces might look like this. First, we create a webpage. Then we associate that webpage with components via a set of tags.
We can then associate these components on that webpage to server-side data through a set of managed beans and then we can create a web deployment descriptor. In addition, you can use application configuration resource files to configure custom components, beans, and page navigation rules. As with Java servlets, you don't necessarily have to reinvent the wheel every time you design a new application interface. The JavaServer Faces technology includes a programming model and libraries of some of the most common tags that you might use.
The prebuilt tags also have tag handlers to help implement them. As a result, common tasks like dropping components onto a webpage can be done very quickly and easily. On the other hand, if your needs don't exactly match any of the prebuilt tags, they can be customized and extended in a number of different ways. The life cycle of a JavaServer Faces web application has two main phases. The first phase is called execute. During this phase, the application view is built or refreshed. The parameters of the request are applied.
The values for all of the components are recalculated based on the request. Those values are applied to the managed beans and then the actual code of the application is run based on those values. In essence, the execute phase consists of translating whatever input was provided into an understandable format and then running the application based on that input. The second phase of the web application is called the render phase. Once the application has been run based on the provided inputs, the render phase simply consists of generating the new user interface based on those inputs.
The output is usually generated in HTML or XHTML, which is then displayed in the client's web browser. Basically, the render phase is just a reverse of the execute phase. It takes an output in machine-readable format and translates it into human-readable format. Altogether, a JavaServer Faces web application will take human input, translate it into a series of server requests, run the requests, and then translate the output back into something a human could understand.
Now that we have a good idea of how Faces and servlets work, let's investigate some of the differences between programming locally and programming on a server.
- Creating simple Java EE servlets
- Setting up a JavaServer Faces application
- Developing managed beans
- Working with web context
- Handling servlet life-cycle events
- Developing parallel-capable servlets
- Optimizing Java EE code
- Enabling security
- Creating custom components and renderers