In this chapter, I'm going to demonstratesetting up and using a JavaServer Faces application.This will be based on much of the same software as we downloaded in chapter one.So if you skipped that chapter, be sure to reviewthe video Setting Up a Runtime Architecture to download the software.We're going to create a new project forthis application, so right-click on the Project menuand choose New, then go to Other, thengo to the Web, and choose Dynamic Web Project.Click Next and then on the second screen you'll be askedto specify the project name, runtime, web module version, and configuration.

I'll name my Faces application SampleApplication.The runtime should be Apache Tomcat with the most recent version that youdownloaded, which is 7.0 for me, as should the dynamic web module version.For the configuration though, you should change it fromthe default to the latest version of JavaServer Faces.This will ensure that your web project is set up to use JavaServer Faces.With that out of the way, I will click Nextagain and keep clicking Next until I get to this page.On this page, I'm asked to specify the JavaServer Faces implementation library.

Here, we will need to choose User Library under Type.If you haven't used this function in other projects, you'll mostlikely get an error message saying there are no user libraries available.Click on the Download Library button on the right side andEclipse will automatically search for compatiblelibraries with your JavaServer Faces implementation.For JSF 2.2, the library is Mojarra 2.2.0.So I'll go ahead and click on that and choose Next.

Accept the terms and click Finish to download this library now.When the library is downloaded, ensure that the box ischecked next to its name on the list and click Finish.With that, the SampleApplication project will be created.Now let's create a sample page to test that the server is working properly.So far, the process of setting up a Faces application has been similar tothat of setting up a servlet, but this is the point at which things diverge.Unlike a servlet, a Faces application is primarily a seriesof webpages, so it actually requires no Java code to run.

Instead, all of the code will come in the form of XHTML and JSP files.An XHTML file is very similar to an HTML file, but instead of being based onthe Standard Generalized Markup Language, or SGML, XHTMLis based off of Extensible Markup Language, or XML.Practical differences are fairly small, but XHTMLdoes support shorthand tags for empty element.So you could type something like br andthen just close the tag within that one tag.

XHTML also gives error messages when askedto display improperly formatted materials, whereas HTMLwill simply try to display the file no matter how poorly formatted it is.With that in mind, I'll create a newitem in the web content folder from SampleApplication.In the wizard, go to New > Other > Web and then HTML File.Then when you are asked for a name, give itwhatever name you want, but change the extension to XHTML.I'll call this one TestPage.xhtml.

When you click Next again, you'll be asked what type of HTML you want to use.Since this is an XHTML file, go ahead and scrolldown and choose the XHTML traditional template, and click Finish.Now you'll have a new XHTML file with a preformatted header.To test that this works, let's add a line in the body and we'll give it the syntaxh1, This is a header, and it automatically closes the tag for you.Now save your document, right-click on it, and chose Run As > Run on Server.

You'll be asked to specify the server, so chosethe Tomcat server that we deployed in chapter one.If everything is set up correctly, then anew page should appear that looks like this.The header text is in bold and the URL isLocalhost8080/SampleApplication/Faces/TestPage.xtml.Now that we have a basic Faces application set up, we can start coding.Before we get too far, however, the nextvideo contains a quick refresher on XML coding,so be sure to check it out if you're unfamiliar with XML or just want a review.

Resume Transcript Auto-Scroll

Author

Released

6/24/2014

Two of the core components driving Java EE web applications are servlets (Java classes that can listen for and respond to HTTP requests) and JavaServer Faces (JSF), a framework for constructing server-side user interfaces. This course shows how to create basic servlets and Facelets (Faces' view templates) and build simple data-management applications with Java EE. Author Patrick Royal also shares parallel-processing tips that save you time on both ends of servlet transactions, along with tricks for extending servlets and increasing your Java application's cross-compatibility.