Module 3 Web Component Model Web Component Model 3 Objectives ► Describe the role of web components in a Java EE application ► Define the HTTP request-response model ► Compare Java servlets and JSP components ► Describe the basic session management strategies ► Manage thread safety issues in web components ► Describe the purpose of web-tier design patterns Developing Applications for Java EE Platform 69 Ω Omega Ω 1 Role of Web Components in a Java EE Application Web Component Model 3 ► Web-centric Java EE application architecture: ► EJB component-centric Java EE application architecture: Developing Applications for Java EE Platform 70 Ω Omega Ω Web Component Model 3 HTTP Request-Response Model Developing Applications for Java EE Platform 71 Ω Omega Ω 2 Web Component Model 3 The GET and POST Requests Developing Applications for Java EE Platform 72 Ω Omega Ω Form Data Web Component Model 3 HTML snippet: <FORM ACTION=’form_test’ METHOD=’POST’> <INPUT NAME=’input1’ SIZE=’20’/> <INPUT TYPE=’SUBMIT’ VALUE=’OK’/> </FORM> Browser form: Browser request: POST /bank/form_test HTTP/1.1 ... request headers... input1=this+is+a+test Developing Applications for Java EE Platform 73 Ω Omega Ω 3 Web Component Model 3 Content Type and the Response Header ► The server response includes a Content-Type header that contains MIME type values including but not limited to: ─ text/html ─ text/xml ─ image/jpeg ► Examples of additional response headers include: ─ Content-Encoding ─ Content-Length ─ Cache-Control Developing Applications for Java EE Platform 74 Ω Omega Ω Web Component Model 3 Comparison of Servlets and JSP™ Components Developing Applications for Java EE Platform 75 Ω Omega Ω 4 Web Component Model 3 Life Cycle of a Web Component Developing Applications for Java EE Platform 76 Ω Omega Ω Web Component Model 3 The service Method The web container calls the service method once for each incoming request. The service method then typically completes the following operations: ► Validates any form data ► Updates the application’s data model ► Collects data from the model to be rendered ► Renders the data in HTML or passes the request and the data to another component responsible for rendering them Developing Applications for Java EE Platform 77 Ω Omega Ω 5 Web Component Model 3 Servlet and JSP Component Examples The examples on the following slides illustrate the similarities and differences between servlets and JSP components. ► Servlet example ► JSP component example ► Servlet and JSP component collaboration ► Run-time behavior of servlets and JSP components ► Web context root and alias mapping Developing Applications for Java EE Platform 78 Ω Omega Ω 79 Ω Omega Ω Web Component Model 3 Servlet Example Developing Applications for Java EE Platform 6 The following code generates the same output as the preceding servlet example: Web Component Model 3 JSP Component Example Developing Applications for Java EE Platform 80 Ω Omega Ω Web Component Model 3 Servlet and JSP Component Collaboration Most modern web applications use servlets and JSP components in collaboration. Their capabilities are similar, but they are expressed differently.: Developing Applications for Java EE Platform 81 Ω Omega Ω 7 Web Component Model 3 Run-Time Behavior of Servlets and JSP Components ► Because JSP components are translated into servlets, JSP components and servlets share run-time behaviors: • Life cycle and container management • API and container services • Client session access ► Both can be entered on multiple threads concurrently and must be implemented accordingly. Developing Applications for Java EE Platform 82 Ω Omega Ω Web Component Model 3 Web Component Thread Model int x; public void doGet(){ Developing Applications for Java EE Platform 83 Ω Omega Ω 8 Web Component Model 3 Implications for the Developer ► Because the web container enters the service method on multiple concurrent threads to support multiple simultaneous requests, the developer must ensure that web components are thread-safe: ► Use instance variables cautiously ► Use class variables very cautiously ► Provide access to external resources cautiously ► Use synchronization constructs to denote critical sections: synchronized (this) { // This section is only entered by 1 thread at a time } Developing Applications for Java EE Platform 84 Ω Omega Ω Web Component Model 3 Web Context Root and Alias Mapping Servlets and JSP components are packaged into a web application. ► Static content, such as HTML or images, is included. ► A web application URI has the following form: http://server:port/context_root/resource ► The context root maps to a web application ► In the deployment descriptor a url pattern is specified that maps from a JSP or Servlet resource to a url: http://www.mybank.com/bank/main Developing Applications for Java EE Platform 85 Ω Omega Ω 9 Web Component Model 3 Session Management The HTTP protocol is stateless. Conversational state might be stored on either the browser or the server: The Java EE model provides a simple mechanism for storing conversational state on the server. Developing Applications for Java EE Platform 86 Ω Omega Ω Web Component Model 3 Problems With Web-Tier Development The complexities of web-based user interface development include: ► Addressing HTTP’s stateless request-response sequence ► Avoiding the creation of large numbers of servlets and JSP components that handle different types of requests ► Using JSP components and servlets effectively Developing Applications for Java EE Platform 87 Ω Omega Ω 10 Web Component Model 3 Model 1 and Model 2 Architectures Two design strategies are involved in using JSP components in the web tier: ► Model 1 architecture: • JSP components handle request processing through <jsp:useBean> classes, the JSTL, and custom tags. • JSP components render data that is retrieved from the business logic tier. ► Model 2 architecture: • Servlets handle request processing, interact with the business logic, and collect data for display. • JSP components render the data for display. Developing Applications for Java EE Platform 88 Ω Omega Ω Web Component Model 3 Traditional MVC Architecture Developing Applications for Java EE Platform 89 Ω Omega Ω 11 Web Component Model 3 Model 2 Architecture as MVC Developing Applications for Java EE Platform 90 Ω Omega Ω Web Component Model 3 MVC in the Java EE Platform The MVC paradigm divides application logic into three roles: ► Controller ► View ► Model • When the application includes EJB components • When there are no EJB components Developing Applications for Java EE Platform 91 Ω Omega Ω 12 Web Component Model 3 Using Web-Tier Design Patterns ► The web-tier developer should work with specific design patterns that realize the broad, architectural paradigms of MVC and Model 2 architecture. ► Of the many available patterns, consider these three specific patterns, which can be used in combination: • Service-to-Worker • Dispatcher View • Business Delegate Developing Applications for Java EE Platform 92 Ω Omega Ω Web Component Model 3 Service-to-Worker and Dispatcher View Patterns Developing Applications for Java EE Platform 93 Ω Omega Ω 13 Web Component Model 3 Web-Tier Design Framework Construction The Service-to-Worker and Dispatcher View patterns provide generic functionality that can be extracted to form a design framework. ► Each request is handled in the same generic way, with variations for the specific needs of the application. ► The controller element is the most generic, allowing reuse in different applications. ► The controller’s functionality can be expressed declaratively, rather than in code. Developing Applications for Java EE Platform 94 Ω Omega Ω Web Component Model 3 Some Available Web-Tier Frameworks Commonly used frameworks include: ► Struts ► JavaServer™ Faces technology Developing Applications for Java EE Platform 95 Ω Omega Ω 14 Web Component Model 3 Business Delegate Pattern Developing Applications for Java EE Platform 96 Ω Omega Ω 15