User Tools

Site Tools


at-m42:lecture13

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
at-m42:lecture13 [2009/04/21 18:15] eechrisat-m42:lecture13 [2011/01/14 12:45] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ~~SLIDESHOW~~ ~~SLIDESHOW~~
-====== Presentation Tier Services ======+====== Presentation-tier Services ======
  
   * Server services is the largest market sector in Java world((Mobile may well become more important, certainly in terms of installed systems, if not in terms of monetary value, we shall see)).   * Server services is the largest market sector in Java world((Mobile may well become more important, certainly in terms of installed systems, if not in terms of monetary value, we shall see)).
Line 19: Line 19:
   * Despite best efforts of some, server is not a single source platform (e.g. WinTel): much more heterogeneous than the typical client (Desktop).   * Despite best efforts of some, server is not a single source platform (e.g. WinTel): much more heterogeneous than the typical client (Desktop).
   * The Java platform offers platform-independent server environment with developing support that allow links to “legacy” applications.   * The Java platform offers platform-independent server environment with developing support that allow links to “legacy” applications.
-  * Possibly most important server application is ability to link to an SQL database: this is provided in Java by the JDBC package (see [[lecture15|Integration Tier Services]]).+  * Possibly most important server application is ability to link to an SQL database: this is provided in Java by the JDBC package (see [[lecture14|Integration Tier Services]]).
   * Next most important is web services providing (browser based) thin-client applications for e-commerce, in institutions via the intranet and to the general public via the Internet.   * Next most important is web services providing (browser based) thin-client applications for e-commerce, in institutions via the intranet and to the general public via the Internet.
  
Line 37: Line 37:
   * [[#Web Designers Want Templates Not Code!]]   * [[#Web Designers Want Templates Not Code!]]
   * [[#Groovy Server Pages]]   * [[#Groovy Server Pages]]
-  * [[#Architectural issues to be aware of!]]+  * [[#Architectural Issues: Patterns|Architectural issues to be aware of!]]
  
  
Line 318: Line 318:
   * [[#Web Designers Want Templates Not Code!]]   * [[#Web Designers Want Templates Not Code!]]
   * [[#Groovy Server Pages]]   * [[#Groovy Server Pages]]
-  * [[#Architectural issues to be aware of!]]+  * [[#Architectural Issues: Patterns|Architectural issues to be aware of!]]
  
 ===== Java Server Pages ===== ===== Java Server Pages =====
Line 446: Line 446:
   * **[[#Web Designers Want Templates Not Code!]]**   * **[[#Web Designers Want Templates Not Code!]]**
   * [[#Groovy Server Pages]]   * [[#Groovy Server Pages]]
-  * [[#Architectural issues to be aware of!]]+  * [[#Architectural Issues: Patterns|Architectural issues to be aware of!]]
  
 ===== Templates Not Code! ===== ===== Templates Not Code! =====
Line 477: Line 477:
   * Good compromise between code-only servlet and Java-rich JSP.   * Good compromise between code-only servlet and Java-rich JSP.
      
 +===== Another example =====
 +  * Example from //Groovy in Action//: [[http://localhost:8080/at-m42-examples/HighLow.groovy|HighLow.groovy]].
 +  * This is the //controller//:
 +
 +<code groovy 1|Example 14: Controller of HighLow game (at-m42/Examples/lecture13/web/HighLow.groovy)>
 +def session = request.session
 +def guess = params.guess
 +
 +guess = guess ? guess.toInteger() : null
 +if (params.restart) guess = null
 +
 +if (! session.goal || params.restart) {
 + session.goal = (Math.random() * 100).toInteger()
 +}
 +def goal = session.goal
 +
 +// ...
 +</code>
 +
 +===== Example (continued) =====
 +
 +<code groovy 1|Example 14: view for HighLow game (at-m42/Examples/lecture13/web/HighLow.groovy)>
 +// ...
 +
 +html.html { head { title 'Think of a Number' }
 +    body {
 +        h1 'Think of a Number'
 +        if (goal && guess) {
 +            div "Your guess is $guess "
 +            if (guess == goal) {
 +                div 'correct!'
 +            } else if (guess < goal) {
 +            div 'too low' 
 +            } else {
 +                div 'too high'
 +            }
 +        }
 +        p "What's your guess (0..100)?"
 +        form(action : 'HighLow.groovy') {
 +            input (type : 'text', name : 'guess', '')
 +            button (type : 'submit', 'Guess')
 +            button (type : 'submit', name : 'restart', value : 'true', 'New Game')
 +        }
 +    }
 +}
 +</code>
  
 ===== Adding GSP capabilities to Servlet container ===== ===== Adding GSP capabilities to Servlet container =====
  
 +<code xml 1>
 +<servlet>
 +    <servlet-name>Groovy Server Pages</servlet>
 +    <servlet-class>groovy.template.TemplateServlet</servlet-class>
 +</servlet>
 +
 +<servlet-mapping>
 +    <servlet-name>Groovy Server Pages</servlet-name>
 +    <url-pattern>*.gsp</url-pattern>
 +</servlet-mapping>
 +</code>
 +
 +===== A GSP View Page =====
 +
 +<code html l|Example 15(a): View for High-Low Game (at-m42/Examples/lecture13/web/HighLow.gsp)>
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture13/web/HighLow.gsp
 +</code>
 +
 +===== The Controller: Dispatches to View =====
 +
 +  * Revised controller Groovlet: [[http://localhost:8080/at-m42-examples/HighLow.groovy|HighLow2.groovy]].
 +<code groovy l|Example 15(b): Controller which dispatches to view (at-m42/Examples/lecture13/web/HighLow2.groovy)>
 +extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/lecture13/web/HighLow2.groovy
 +</code>
  
  
 ===== Other Examples of Template View implementations =====  ===== Other Examples of Template View implementations ===== 
-Not for discussion here … to be studied as part of your seminar project+  * Not for discussion here. 
-Java standard is to use custom tags” that talk to Java classes. +    Java standard is to use "custom tagsthat talk to Java classes. 
-Tags look like XML tags (with namespaces) in (X)HTML pages +    Tags look like XML tags (with namespaces) in (X)HTML pages. 
-JSTL – Java Standard Tag Library provides access to beans and provides simple programming logic tags. +    JSTL -- Java Standard Tag Library provides access to beans and provides simple programming logic tags. 
-Can eliminate <% … %> from JSP code! +    Can eliminate ''<% … %>'' from JSP code! 
-Several open source alternatives: +  Several open source alternatives: 
-Best regarded seems to be Velocity (another apache project) +     * Best regarded seems to be //[[http://velocity.apache.org/|Velocity]]// (another Apache project) 
-Very simple template language embedded in HTML +     * Very simple template language embedded in HTML -- similar to GSP 
-Parsed and replaced by computational results (by a special Servlet) at access time. +     * Parsed and replaced by computational results (by a special Servlet) at access time. 
-Transparent to web developers who just see HTML pages. +     * Transparent to web developers who just see HTML pages.
-Contents +
-Servlets +
-Java Server Pages (JSP) +
-Web Designers Want Templates Not Code! +
-Architectural issues to be aware of!+
  
-Architectural Issues: Patterns +===== Lecture Contents =====
-Web container gets its requests from a web client in HTTP and returns HTML views of the application’s business objects.  +
-Business objects are Java objects in the business tier. +
-Web Tier communicates with the Business Tier either using RMI or by directly via Java objects.  +
-Many presentation layer patterns are designed to simplify (or at least manage) the difficulties inherent with the impedance mismatch in this model. +
-Also allow web designers and Java programmers to do their things without stepping on each others toes. +
-Web Tier as “Model View Controller” +
-Web Tier is essentially a good example of the classic model-view-controller (MVC) pattern. +
-Principles: +
-Separation of presentation from model +
-Separation of controller from the view +
-In “Web Tier”  +
-View is HTML or JSP page. +
-Controller is a Servlet. +
-Model is a Java (business) object. +
-MVC Patterns in the Web Tier +
-Page Controller – an object that handles a request for a specific page or action on a web site. +
-Decode URL and extract any form data +
-Create and invoke any model objects to process the data. +
-Pass data to model (models don’t need to know about HTTP) +
-Determine which view should display the result and forward model info to view (JSP page). +
-MVC Patterns in the Web Tier +
-Front Controller handles all requests for a web site +
-Used when navigation and application logic is more complex than can be reasonably handled by a collection of page controllers +
-Component Model +
-Pattern is based on “components” and events+
  
-Patterns in the Web Tier +  * [[#Servlets and Groovlets]] 
-Implementing complex presentation logic in HTML is a complex task +  * [[#Java Server Pages]] 
-Most of the Java EE Core Patterns are concerned with the presentation tier. +  * [[#Web Designers Want Templates Not Code!]] 
-See full catalogue (online) at http://java.sun.com/blueprints/coreJava EEpatterns/Patterns/ +  * [[#Groovy Server Pages]] 
-Study some for the seminar+  * **[[#Architectural Issues: Patterns|Architectural issues to be aware of!]]**
-MVC Frameworks +
-Many common problems (as indicated by the large number of patterns for presentation tier) have been solved many times. +
-Many frameworks have been developed to simplify and standardise web tier development and implement the patterns.  +
-All based around Servlets. Some also use or extend JSP. +
-Some examples (study for seminar): +
-Front Controller:  +
-Apache Struts: http://struts.apache.org/ +
-Spring MVC: http://www.springframework.org/ +
-Component model: +
-Jakarta Tapestry: http://jakarta.apache.org/tapestry/ +
-Java EE JavaServer Faces (JSF): http://java.sun.com/Java EE/javaserverfaces/index.jsp+
  
 +===== Architectural Issues: Patterns =====
  
-Lecture Summary +  * Web container gets its requests from web client in HTTP and returns HTML views of the application's business objects.  
-Advantage of Java web container is that it is platform independent: will work on any machine that has network connection and a JVM. +  * Business objects are Java objects in the //business tier//
-We have introduced some of the Java APIs that can be used to create web-based presentation services with Java+  * //Web Tier// communicates with the //Business Tier// either using some form of distributed computing technique or directly via Java/Groovy objects.  
-Servlets provide a way to create applications that communicate to a client via a web browser+  * Many presentation layer patterns are designed to simplify (or at least manage) the difficulties inherent with the "//impedance mismatch//" in this model. 
-JSP allows the embedding of Java code inside an HTML page+  * Also allow web designers and programmers to do their things without stepping on each others toes.
-In the thin-client world you are not working alone! +
-Several core Java EE design patterns record best practice and have been implemented in open source and official Java solutions+
  
-Lab Exercises +===== Web Tier as “Model View Controller” ====== 
-Download, install and run Tomcat +
-Modify ServletsRule.java so that the count i is loaded when the servlet starts (override init()) and is saved to disk by an overriden destroy() method. +
-Experiment with JSP pages. +
-Read-up on any or all of the Java EE patterns concerned with the presentation tier. +
-Look into alternative presentation tier technologies: official Sun offerings: JSF, JSTL. Officially sanctioned MVC framework: Apache Struts. Alternatives to Struts and JSF: e.g. Tapestry, Spring MVC. +
  
 +{{  :at-m42:classic-mvc.png|Model-View-Controller pattern}}
  
 +  * Web Tier is essentially a good example of the classic [[wp>Model-view-controller|model-view-controller]] (MVC) pattern.
 +  * Principles:
 +    * Separation of presentation from model
 +    * Separation of controller from the view
 +  * In "Web Tier" 
 +    * View is HTML or [G/J]SP page.
 +    * Controller is a Servlet.
 +    * Model is a (business) object.
  
 +===== MVC Patterns in the Web Tier (1) =====
  
 +[[http://www.martinfowler.com/eaaCatalog/pageController.html|Page Controller]] -- //an object that handles a request for a specific page or action on a web site//.
  
 +{{:at-m42:page-controller.png  |Page controller}}  
 +
 +  * Decode URL and extract any form data
 +  * Create and invoke any model objects to process the data.
 +  * Pass data to model (models don’t need to know about HTTP)
 +  * Determine which view should display the result and forward model info to view (template page).
 +
 +===== MVC Patterns in the Web Tier (2) =====
 +
 +[[http://www.martinfowler.com/eaaCatalog/frontController.html|Front Controller]] -- //handles all requests for a web site//.
 +  * Used when navigation and application logic is more complex than can be reasonably handled by a collection of page controllers.
 +{{:at-m42:front-controller1.png  |Front controller (class diagram)}}
 +{{  :at-m42:front-controller2.png|Front controller (activity diagram)}}
 +
 +===== Component Model =====
 +
 +Pattern is based on "components" and events
 +{{:at-m42:component.png?654|Component model}}
 +
 +===== Patterns in the Web Tier =====
 +  * Implementing complex presentation logic in HTML is a complex task
 +  * Most of the Java EE Core Patterns are concerned with the presentation tier.
 +  * See full catalogue ([[http://java.sun.com/blueprints/corej2eepatterns/Patterns/|online]]). 
 +===== MVC Frameworks =====
 +
 +  * Many common problems (as indicated by the large number of patterns for presentation tier) have been solved many times.
 +  * Many frameworks have been developed to simplify and standardise web tier development and implement the patterns. 
 +  * All based around Servlets. Some also use or extend JSP.
 +
 +===== Some Example Frameworks =====
 +
 +//Front Controller//: 
 +  * Apache Struts: http://struts.apache.org/
 +  * Spring MVC: http://www.springframework.org/
 +//Component model//:
 +  * Apache Tapestry: http://tapestry.apache.org/
 +  * Java EE JavaServer Faces (JSF): http://java.sun.com/javaee/javaserverfaces/
 +  * Google Web Toolkit (GWT): http://code.google.com/webtoolkit/
 +
 +Many more [[http://java-source.net/open-source/web-frameworks|listed here]].
 +
 +===== Lecture Summary =====
 +
 +  * Advantage of Java web container is that it is platform independent: will work on any machine that has a network connection and a JVM.
 +  * We have introduced some of the Java APIs that can be used to create web-based presentation services with Java.
 +  * Servlets provide a way to create applications that communicate to a client via a web browser.
 +  * JSP allows the embedding of Java code inside an HTML page.
 +  * Groovy makes both Servlets (Groovlets) and Templates (GSP) easier.
 +  * In the thin-client world you are not working alone:
 +    * Several core Java EE design patterns record best practice and have been implemented in open source and official Java solutions. 
  
 ---- ----
  
 [[Home]] | [[lecture12|Previous Lecture]] | [[Lectures]] | [[lecture14|Next Lecture]]  [[Home]] | [[lecture12|Previous Lecture]] | [[Lectures]] | [[lecture14|Next Lecture]] 
- 
at-m42/lecture13.1240337732.txt.gz · Last modified: 2011/01/14 12:23 (external edit)