The login web application is defined by the following files:
Java classes (Source Packages/com/corejsf/):
JSF pages (Web Pages/):
Configuration files (Web Pages/WEB-INF/:
The index.xhtml page is the first page shown when the user enters the application url into their browser. It has a form with name and password input text fields and a "Login" button.
Two namespace declarations in the html tag declare the tag namespaces used in the page: a global namespace and a namespace that uses a h: prefix. The latter is part of the JSF framework.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
The connection to the user bean is made in the two following lines. When the "Login" button is clicked the setName() and setPassword() methods of the user bean are invoked on the server.
<td><h:inputText value="#{user.name}"/></td>
<td><h:inputSecret value="#{user.password}"/></td>
In JSF applications, Java beans are the conduits between the user interface and the back end of the application. A Java bean is a class that exposes properties and events to an environment such as JSF. They are named and can have different scopes or lifetimes according to the needs of the application.
The user bean is given its name by the following Java annotation.
@Named("user")
The user bean is given a session scope by the following Java annotation.
@SessionScope
Among the scopes that can be declared are:
Short-lived; new instance created with each request.
Not appropriate if data is to persist beyond the request.
Lasts during repeated connections by the same client.
Session tracking usually done with cookies, otherwise through URL rewriting and session IDs.
Lasts for duration of the web application.
Shared among all requests and all sessions.
The user bean has two read/write properties user.name and user.password. The properties are defined by implementing appropriately named getter and setter methods.
public String getName() { return name; } public void setName(String newValue) { name = newValue; }
public String getPassword() { return password; } public void setPassword(String newValue) { password = newValue; }
A property is a named value of a given type that can be read and/or written
Properties are defined using the get/set convention, with the first letter of the property name changed to upper case
The connection to the user bean is made in the line below. When the page is rendered the getName() method of the user bean is invoked.
<h3>Welcome to JavaServer Faces, #{user.name}!</h3>
The underlying technology for JSF is Servlets. The JSF framework provides a means of configuring a Faces Servlet using the file web.xml. Fortunately, you can use same file for most JSF web applications.
The servlet element specifies the class to be executed to run the Faces Servlet.
The servlet-mapping element insures that JSF page requests are processed by the controlling Servlet called the Faces Servlet.
Here are the events:
Browser requests http://daneel.d.umn.edu:29104/login/
This redirects to http://daneel.d.umn.edu:29104/login/faces/index.xhtml
The mapping servlet-mapping rule cause the web server to activate the Faces servlet, which is the entry point to the JSF implementation.
The Faces servlet loads the index.xhtml page.
When the user clicks Login the Faces servlet navigates to welcome.xhtml page.
The welcome-file element insures that index.xhtml is initially loaded.
The context-param element adds support for debugging.