The Java EE 6 Tutorial, Volume I

Building, Packaging, Deploying and Running the Application

The example Facelets application described in this chapter can be built, packaged, and deployed using the Java EE 6 SDK with NetBeans IDE. For details on how to obtain this software and configure your environment to run the examples, see Chapter 2, Using the Tutorial Examples. The source code for this example is also available in the tut-install/examples/web/guessnumber directory.

ProcedureTo Create the Example Facelets Application with NetBeans IDE

To create the example Facelets project, use the following procedure.

  1. In NetBeans IDE, from the File menu, choose New Project.

    The New Project wizard opens.

  2. In the wizard, select Java Web as the category and Web Application as the project type and click Next.

    The New Web Application wizard opens.

  3. In the Project Name field, type guessNumber, and click Next.

  4. In the Server and Settings page, select Server as GlassFish v3 from the Server menu, select Java EE version as Java EE 6 Web from the Java EE version menu, and then click Next.

  5. In the Frameworks page, select the JavaServer Faces checkbox and click Finish.

    A new Project is created and is available in the Projects window. A default file, index.xhtml, is created and opened in the Editor.

ProcedureTo Create the Application

  1. Right-click the Project node, and select New->Java package.

  2. In the Package Name field, type guessNumber and click Finish.

    A new package is created and placed under Source Packages node of the Project.

  3. Right-click the Source Packages node and select New->Java Class.

  4. Type the name of the class file as UserNumberBean, select the name of package as guessNumber and click Finish.

    A new Java class file is created and opened in the IDE.

  5. Replace the content of the Java class file with the example code from the UserNumberBean.java file listed in Developing a Backing Bean, and save the file.

  6. Create two new XHTML pages and name them greeting.xhtml and response.xhtml respectively:

    1. Right-click the project node and choose New->Other.

      The New File wizard opens.

    2. Choose Category as Web and then File Type as XHTML and click Next.

    3. Enter greeting.xhtml in the XHTML File name field and click Finish.

      A new XHTML web page is created and placed under Web Pages node.

    4. Repeat the above steps but enter the name of file as response.xhtml to create a second web page.

  7. Edit the XHTML files and add Facelets content to them:

    1. Replace the content of greeting.xhtml with the example greeting.xhtml code listed in Creating Facelets Views and save the file.

    2. Similarly replace the content of response.xhtml with the example response.xhtml code and save the file.

  8. Add Duke's image as part of the application by copying the wave.med.gif image file from the tutorial example and saving it as a resource.

    1. Create a folder named resources under Web Pages.

    2. Create a subfolder, images under resources folder.

    3. Save the wave.med.gif image in resources/images folder.

  9. Edit the web.xml file to modify the welcome page to greeting.html.

  10. Right-click the Project Node and select Build from the menu, to compile and build the application.

  11. Right-click the Project Node and select Deploy, to deploy the application to Sun GlassFishTM Enterprise Server v3.

  12. Access the application by typing the following URL in the browser:


    http://localhost:8080/guessNumber