1 / 123

JavaServer ™ Faces Web Applications

26. JavaServer ™ Faces Web Applications. If any man will draw up his case, and put his name at the foot of the first page, I will give him an immediate reply. Where he compels me to turn over the sheet, he must wait my leisure. Lord Sandwich

cai
Download Presentation

JavaServer ™ Faces Web Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 26 • JavaServer ™ Faces Web Applications

  2. If any man will draw up his case, and put his name at the foot of the first page, I will give him an immediate reply. Where he compels me to turn over the sheet, he must wait my leisure. Lord Sandwich Rule One: Our client is always right Rule Two: If you think our client is wrong, see Rule One. Anonymous

  3. A fair question should be followed by a deed in silence. Dante Alighieri You will come here and get books that will open your eyes, and your ears, and your curiosity, and turn you inside out or outside in. Ralph Waldo Emerson

  4. OBJECTIVES In this chapter you will learn: • Web application development using Java Technologies and Netbeans. • To create JavaServer Pages with JavaServer Faces components. • To create web applications consisting of multiple pages. • To validate user input on a web page. • To maintain state information about a user with session tracking and cookies.

  5. 26.1Introduction • 26.2   Java Web Technologies • 26.2.1  Servlets • 26.2.2  JavaServer Pages • 26.2.3  JavaServer Faces • 26.2.4  Web Technologies in Netbeans • 26.3   Creating and Running a Simple Application in Netbean • 26.3.1  Examining a JSP File • 26.3.2  Examining a Page Bean File • 26.3.3  Event-Processing Life Cycle • 26.3.4  Relationship Between the JSP and Page Bean Files • 26.3.5  Examining the XHTML Generated by a Java Web Application • 26.3.6  Building a Web Application in Netbeans

  6. 26.4   JSF Components • 26.4.1  Text and Graphics Components • 26.4.2  Validation Using Validator Components and Custom Validators • 26.5   Session Tracking • 26.5.1  Cookies • 26.5.2  Session Tracking with the SessionBean Object • 26.6   Wrap-Up • 26.7   Web Resources

  7. 26.1 Introduction • This chapter assumes Java programming experience • See Java How to Program, Seventh Edition • Web-based applications create web content for web browser clients • AJAX—provides interactivity and responsiveness users typically expect of desktop applications

  8. 26.2 Java Web Technologies • Continually evolve to provide developers with higher levels of abstraction and greater separation of the application’s tiers • Separation makes web applications more maintainable and extensible • Netbeans + Visual Web Pack • Drag-and-drop web-application GUI development • Business logic placed in separate Java classes

  9. 26.2.1 Servlets • Use the HTTP request-response model of communication between client and server • Extend a server’s functionality by allowing the server to generate dynamic content • Servlet container executes and interacts with servlets • Packages javax.servlet and javax.servlet.http contain the servlet classes and interfaces.

  10. 26.2.1 Servlets • Servlet container • Receives HTTP requests from clients • Directs each request to the appropriate servlet • Servlet processes the request and returns response to the client—typically an XHTML or XML document • Servlets implement the Servlet interface of package javax.servlet • Ensures that each servlet can execute in the framework • Declares methods used by the servlet container to manage the servlet’s life cycle

  11. 26.2.1 Servlets • Servlet life cycle • Begins when the servlet container loads it into memory—usually in response to the first request for the servlet • init method • Called only once by container during a servlet’s life-cycle to initialize the servlet • service method • Called once per request • Receives the request • Processes it • Sends a response to the client • destroy method • Called to release any resources held by the servlet when container terminates servlet

  12. 26.2.2 JavaServer Pages • Extension of servlet technology • Each JSP is translated by the JSP container into a servlet • Help separate presentation from content • Help web application programmers create dynamic content • By reusing predefined components • By interacting with components using server-side scripting

  13. 26.2.2 JavaServer Pages • JavaBeans and custom tag libraries encapsulate complex, dynamic functionality • Custom tag libraries • Allow Java developers to hide code for database access and other complex operations in custom tags • Enable web-page designers who are not familiar with Java to enhance web pages with powerful dynamic content and processing capabilities • The JSP classes and interfaces are located in packages javax.servlet.jsp and javax.servlet.jsp.tagext.

  14. 26.2.2 JavaServer Pages • Four key components • Directives • Actions • Scripting elements • Tag libraries • Directives • Messages to the JSP container • Specify page settings • Include content from other resources • Specify custom tag libraries for use in JSPs • Actions • Encapsulate functionality in predefined tags • Often are performed based on the information sent to the server as part of a particular client request • Can create Java objects for use in JSPs

  15. 26.2.2 JavaServer Pages • Scripting elements • Insert Java code that interacts with components in a JSP to perform request processing • Tag libraries • Enable programmers to create custom tags • Enable web-page designers to manipulate JSP content without prior Java knowledge • JavaServer Pages Standard Tag Library (JSTL) • Provides many common web application tasks • Static content • XHTML or XML markup • Known as fixed-template data or fixed-template text • Literal text is translated to a String literal in the servlet representation of the JSP

  16. 26.2.2 JavaServer Pages • First request for a JSP • Container translates the JSP into a servlet • Servlet handles the current and future requests to the JSP • JSPs rely on the same request/response mechanism as servlets to process requests from and send responses to clients

  17. Performance Tip 26.1 • Some JSP containers translate JSPs into servlets at the JSP’s deployment time (i.e., when the application is placed on a web server). This eliminates the translation overhead for the first client that requests each JSP, as the JSP will be translated before it is ever requested by a client.

  18. 26.2.3 JavaServer Faces • Web application framework • Simplifies the design of an application’s user interface • Further separates a web application’s presentation from its business logic • Framework • Simplifies application development by providing libraries and sometimes software tools to help you organize and build your applications

  19. 26.2.3 JavaServer Faces • JSF custom tag libraries • Contain user interface components that simplify web-page design • Includes a set of APIs for handling component events • You design the look-and-feel of a page with JSF by adding custom tags to a JSP file and manipulating their attributes • You define the page’s behavior in a separate Java source-code file.

  20. 26.2.4 Web Technologies in Netbeans • Netbeans web applications • Consist of one or more JSPs built in the JavaServer Faces framework • Each has the file-name extension .jsp and contains the web page’s GUI elements • Netbeans allows you to • Design pages visually by dragging and dropping JSF components onto a page; • Customize a web page by editing its .jsp file manually • Every JSP file created in Netbeans represents a web page and has a corresponding JavaBean class called the page bean

  21. 26.2.4 Web Technologies in Netbeans • JavaBean class must have • Default (or no-argument) constructor • get and set methods for all of its properties. • page bean • Defines properties for each of the page’s elements • Event handlers • Page life-cycle methods • Other supporting code for the web application • Every web application built with Netbeans has a page bean, a RequestBean, a SessionBean and an ApplicationBean

  22. 26.2.4 Web Technologies in Netbeans • RequestBean • Request scope • Exists only for the duration of an HTTP request • SessionBean • Session scope • Exists throughout a user’s browsing session or until the session times out • Unique SessionBean object for each user • ApplicationBean • Application scope • Shared by all instances of an application • Exists as long as the application remains deployed • Application-wide data storage or processing • One instance exists, regardless of the number of open sessions

  23. 26.3 Creating and Running a Simple Application in Netbeans • Static Text components display text that cannot be edited by the user

  24. Tag libraries available for use in this web application Configure head section of web page Outline Time.jsp (1 of 2 )

  25. Set up a Static Text element Outline Time.jsp (2 of 2 )

  26. 26.3.1 Examining a JSP File • Netbeans generates a JSP file in response to your interactions with the Visual Editor • All JSPs have a jsp:root element • version attribute indicates the version of JSP being used • One or more xmlns attributes. Each xmlns attribute specifies a prefix and a URL for a tag library, allowing the page to use tags specified in that library. • All JSPs generated by Netbeans include the tag libraries for • JSF core components library • JSF HTML components library • JSP standard components library • JSP user interface components library

  27. 26.3.1 Examining a JSP File • The jsp:directive.page element • contentType attribute • specifies the MIME type and the character set the page uses • pageEncoding attribute • specifies the character encoding used by the page source • These attributes help the client determine how to render the content • All pages containing JSF components are represented in a component tree with the root JSF element f:view (of type UIViewRoot) • All JSF component elements are placed in this element • Many webuijsf page elements have a binding attribute to bind their values to properties in the web application’s JavaBeans • JSF Expression Language is used to perform these bindings.

  28. 26.3.1 Examining a JSP File • webuijsf:head element • title attribute that specifies the page’s title • webuijsf:link element • can be used to specify the CSS stylesheet used by a page • webuijsf:body element • defines the body of the page • webuijsf:form element • defines a form in a page. • webuijsf:staticText component • displays text that does not change

  29. 26.3.1 Examining a JSP File • JSP elements are mapped to XHTML elements for rendering in a browser • Can map to different XHTML elements, depending on the client browser and the component’s property settings • webuijsf:staticText component • Typically maps to an XHTML span element • A span element contains text that is displayed on a web page and is used to control the formatting of the text • style attribute of webuijsf:staticText represented as part of the corresponding span element’s style attribute

  30. Fig. 26.2 | Sample JSF component tree.

  31. 26.3.2 Examining a Page Bean File • Page bean classes • Inherit from class AbstractPageBean (package com.sun.rave.web.ui.appbase • Provides page life-cycle methods • A webuijsf:staticText component is a StaticText object (package com.sun.webui.jsf.component).

  32. Outline Time.java (1 of 8 )

  33. Outline Time.java (2 of 8 )

  34. Outline Time.java (3 of 8 )

  35. Outline Time.java (4 of 8 )

  36. Code that was inserted by the designer to create a StaticText object Outline Time.java (5 of 8 )

  37. Outline Time.java (6 of 8 )

  38. Programmatically change the text in the StaticText object Outline Time.java (7 of 8 )

  39. Outline Time.java (8 of 8 )

  40. Outline Time.java

  41. 26.3.3 Event-Processing Life Cycle • Netbeans application model • Places methods init, preprocess, prerender and destroy in the page bean that tie into the JSF event-processing life cycle • These represent four major stages—initialization, preprocessing, prerendering and destruction • init method • Called by the JSP container the first time the page is requested and on postbacks • Postback occurs when form data is submitted, and the page and its contents are sent to the server to be processed • invokes its superclass version, then tries to call the method _init, which handles component initialization tasks

  42. 26.3.3 Event-Processing Life Cycle • preprocess method • Called after init, but only if the page is processing a postback • prerender method • Called just before a page is rendered by the browser • Should be used to set component properties • destroy method • Called after the page has been rendered, but only if the init method was called • Handles tasks such as freeing resources used to render the page

  43. 26.3.4 Relationship Between the JSP and Page Bean Files • Page bean • has a property for every element that appears in the JSP file

  44. 26.3.5 Examining the XHTML Generated by a Java Web Application

  45. Outline Time.html (1 of 2 )

  46. Outline Time.html (2 of 2 )

  47. 23.6.6 Building a Web Application in Netbeans

  48. Fig. 26.5 | Visual Editorwindow in Design mode.

  49. Fig. 26.6 | Palette in Netbeans.

  50. Fig. 26.7 | Projectswindow for the WebTime project.

More Related