One requirement of a JavaServer Faces application is that all requests to the application that reference previously saved JavaServer Faces components must go through FacesServlet. A FacesServlet instance manages the request processing life cycle for web applications and initializes the resources required by JavaServer Faces technology.
Before a JavaServer Faces application can launch the first JSP page, the web container must invoke the FacesServlet instance in order for the application life cycle process to start. The application life cycle is described in the section The Life Cycle of a JavaServer Faces Page.
To make sure that the FacesServlet instance is invoked, you provide a mapping to it. The mapping to FacesServlet can be a prefix mapping, such as /guess/*, or an extension mapping, such as *.faces. The mapping is used to identify a JSP page as having JavaServer Faces content. Because of this, the URL to the first JSP page of the application must include the mapping.
In the case of prefix mapping, there are two ways to accomplish this:
The page author can include an HTML page in the application that has the URL to the first JSP page. This URL must include the path to FacesServlet, as shown by this tag, which uses the mapping defined in the guessNumber application:
Users of the application can include the path to FacesServlet in the URL to the first page when they enter it in their browser, as shown by this URL that accesses the guessNumber application:
The second method allows users to start the application from the first JSP page, rather than start it from an HTML page. However, the second method requires users to identify the first JSP page. When you use the first method, users need only enter
In the case of extension mapping, if a request comes to the server for a JSP page with a .faces extension, the container will send the request to the FacesServlet instance, which will expect a corresponding JSP page of the same name to exist containing the content. For example, if the request URL is http://localhost/bookstore6/bookstore.faces, FacesServlet will map it to the bookstore.jsp page.
If you are using NetBeans IDE, the time to map the FacesServlet instance is when you create your JavaServer Faces project with NetBeans IDE:
In NetBeans IDE, select File->New Project.
In the New Project dialog, select Web from the Categories tree.
Select Web Application from the Projects panel.
Fill out the information in the Name and Location screen of the wizard.
Select the JavaServer Faces check box in the Framewoks screen.
Enter the mapping, such as *.faces, to the FacesServlet instance in the Servlet URL Mapping field.
After your project is open in NetBeans IDE, you can change the mapping to the FacesServlet instance by doing the following:
Expand the node of your project in the Projects pane.
Expand the Web Pages and WEB-INF nodes that are under the project node.
After the web.xml file appears in the editor pane, click Servlets at the top of the editor pane. The FacesServlet configuration appears in the editor pane.
If you prefer to edit the web.xml file directly, perform the following steps to configure a mapping to the FacesServlet instance:
Include a servlet element in the deployment descriptor.
Inside the servlet element, include a display-name element and set it to FacesServlet.
Also inside the servlet element, add a servlet-name element and set it to FacesServlet.
Add a third element, called servlet-class, inside the servlet element and set it to javax.faces.webapp.FacesServlet. This is the fully-qualified class name of the FacesServlet class.
After the servlet element, add a servlet-mapping element.
Inside the servlet-mapping element, add a servlet-name element and set it to FacesServlet. This must match the name identified by the servlet-name element described in step 3.
Also inside the servlet-mapping element, add a url-pattern element and set it to whatever mapping you prefer. This will be the path to FacesServlet. Users of the application will include this path in the URL when they access the application. For the guessNumber application, the path is /guess/*.