Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

obsoletePages.xml

00001 <pages title="Obsolete Pages">
00002   <page id="Demo" name="Demonstration" requiresRole="none"
00003     title="Jwaa Demo Application" >
00004 
00005   <p><img align="right"
00006        src="/jwaa/pix/states.gif"
00007        width="167"
00008        height="177" />The six pages that make up the demo
00009        application are shown in this figure as ovals with the 
00010        hotlinks that connect them as arrows.</p>
00011 
00012   <p>Each of the pages in this figure are generated by objects,
00013   called <i>Page Objects</i>. These are defined by classes that you define
00014   while developing your application. Browsers issue page requests
00015   with urls of the form 
00016   <pre> http://hostname:port/contextName/servletName/pageName...</pre>.
00017   If a servlet engine (for example, #link("Jetty") is configured to
00018   listen to that port on that host, it routes the request to the
00019   servlet configured to respond to the specified <code>context</code>and 
00020   <code>servlet</code>and the servlet dispatches the request to the
00021   desired page based on the url. You define the servlet and several 
00022   other glue classes as will be described later.</p>
00023 
00024   <p>Every page is defined by a Page class, each of which defines a
00025   MetaPage, a no-args constructor, and a run method as shown here:</p>
00026 
00027   <p>The PageMapping provides a unique name for each page. It must
00028   be consistent with the &lt;servlet-mapping&gt; entry of your
00029   web.xml file. For example, if &lt;servlet-name&gt; is /foo,
00030   &lt;servlet-mapping&gt; is /bar/*, and the unique name of this
00031   page is gag, the PageMapping argument is /bar/gag. The servlet 
00032   name is automatically added as required.</p>
00033 
00034   <p>The servlet calls the no-args constructor to create a page
00035   instance, initializes the instance with information it needs to
00036   respond to browser requests, and calls the instance's run method
00037   to handle the request. The MetaPage instance bundles static
00038   information about the page's relationship to other pages and will 
00039   be described later.</p>
00040 
00041   <p>The run method is where all the action happens. For
00042   non-interactive pages (pages that don't process forms), the run
00043   method simply sends html text to the browser (via the 
00044   <code>page.send(...) call outlined above</code>). Interactive
00045   pages can be considerably more elaborate since they must decide 
00046   what to send based on form parameter inputs.</p>
00047 
00048   <h2>Templates</h2>
00049 
00050   <p>Multi-page web applications must do something to ensure that
00051   the site's standard look and feel is preserved as the user moves
00052   from page to page. In Jwaa, the solution is to define a 
00053 
00054   <code>Template</code>class with static methods that the run
00055   methods call to compute the standard html boilerplate at the
00056   beginning and end of each page. For example, run methods are 
00057   typically coded like this:</p>
00058 
00059   <pre>
00060  public final void
00061 run() throws Fault {
00062  page.send(...);
00063 } 
00064 </pre>
00065 
00066   <p>JWAA's only requirement is that a run method exist to send the
00067     response to the browser. It does not constrain <i>how</i> run methods 
00068     compute that response. One common approach, <i>separation of concerns</i>, 
00069     reads the response text from files, often with the aid of a 
00070     template engine like #link("Velocity"). The other, 
00071     <i>integration of concerns</i>, maintains text as strings 
00072     within the application.  This will take the latter approach 
00073     because this is the approach used in the Demo applications.</p>
00074 
00075   <h2>Multi-line Strings</h2>send html boilerplate at the
00076   beginning/end of each page and the page.send(...) call sends the
00077   page content. It would be tedious to write long (multiline) text
00078   as Java Strings. #link("JavaPlus") simply provides a syntax that
00079   makes long strings much easier to manage than plain Java strings.
00080 
00081   <p>For example, #link("INNER") shows how everything we've
00082   discussed so far works in practice. Notice that the page's
00083   content is a single long string enclosed in Java+ string
00084   delimiters (
00085 
00086   <code>{{...}}</code>)</p>
00087 
00088   <h2>Executable Inclusions</h2>
00089 
00090   <p>Notice that the long string contains java code enclosed in
00091     embedded <code>{{...}}</code>delimiters (for example at the 
00092     beginning of the second paragraph). These are <i>executable inclusions</i>; 
00093   Java expressions embedded within strings to be evaluated and 
00094   spliced into the surrounding string at run-time.</p>
00095 
00096   <p>Executable inclusions are a generally useful feature. One
00097   common use is supporting compile-time checking of cross-page
00098   references, or hotlinks. The first paragraph shows the
00099   conventional technique in which pages reference other pages (or
00100   in this case, a graphic) by hard-coding &lt;a href=""&gt; (or in 
00101   this case, an &lt;img...&gt; command) into the html.</p>
00102 
00103   <p>The JWAA alternative is shown in the second paragraph. An
00104   executable inclusion is used to splice the output of the 
00105   <code>link</code>command into the surrounding string. The link
00106   command's argument, EntryPage.meta, identifies the target page
00107   such that if the page is ever renamed or moved, a compile-time
00108   error will report the error rather than letting the user discover 
00109   the problem at run-time as in the first approach.</p>
00110 
00111   <p>Another improvement is less obvious. Java servlets provide two
00112   techniques for tracking session information: cookies and
00113   url-rewriting. When a user logs in successfully, their identity
00114   is typically recorded as a cookie as they move from page to page.
00115   However some browsers don't support cookies, and some users turn
00116   them off, so the fallback technique, url rewriting, is needed for
00117   the system to remember who they are. But url rewriting only works
00118   when each and every url is generated on-the-fly, and is of no use
00119   when urls are hard-coded inside html text. By habitually adopting
00120   the JWAA technique, user sessions are automatically preserved
00121   even when the system can't count on cookies for maintaining the 
00122   user's identity.</p>
00123 
00124   <h2>Meta Pages</h2>
00125 
00126   <p>Notice that the link command's argument ends in .meta. This is
00127   the first, and simplest use of the MetaPage object that was
00128   defined without explanation in the ExamplePage example. By
00129   convention, every page class defines a public final static
00130   variable, named meta. Other pages rely on this variable for 
00131   generating hotlinks to that page as demonstrated above.</p>
00132 
00133   <p>Also notice that the link command in this example contains a
00134   second argument, "Click here". If this argument were omitted, the
00135   anchor text of the emitted url would have been as defined in the
00136   MetaPage constructor argument, "Entry" in this case. But since we
00137   wanted the anchor to read "Click here" in this case, we overrode 
00138   the default by providing the additional argument.</p>
00139 
00140   <p>This is only the simplest example of how the MetaPage is used.  
00141   #link("MetaPageDox" "Click here") for a complete discussion.</p>
00142 
00143   <h2>Rewriting here</h2>
00144 
00145   <p>Each page is a subclass of #link("GenericPage"), and they
00146   reference other pages by calling the <code>link, form, redirect 
00147   or forward</code>methods that they inherit with the target page 
00148   as an argument. Since Java checks all object references at compile 
00149   time, if the target page doesn't 
00150   exist, the error will be reported at compile time.</p>
00151 
00152   <p>JwaaPage defines several abstract methods such that each
00153   subclass must implement the following methods. In addition, pages
00154   can and often do define additional methods to support the 
00155   mandatory ones listed here</p>
00156 
00157   <dl>
00158 
00159     <dt>public PageName()</dt>
00160 
00161     <dd>Each page class must be public and must define a public
00162     no-args constructor. The application's servlet uses the
00163     constructor to create an instance of your Page class to handle
00164     the request. It then calls the instance's public final void
00165     init() populate the Page's four instance variables with the
00166     request-specific information that the page needs to handle the
00167     request: 
00168 
00169     <ol>
00170 
00171       <li>The servlet that dispatched the request</li>
00172 
00173       <li>The HttpServletRequest instance</li>
00174 
00175       <li>The HttpServletResponse instance</li>
00176 
00177     </ol></dd>
00178 
00179     <dt>public final static MetaPage meta = new MetaPage(...)</dt>
00180 
00181     <dd>Every page defines a global handle, by convention named
00182     "meta", that other pages will use to reference the page in
00183     hotlinks, form references, redirects and forward commands. This
00184     structure would be the page's meta class in other
00185     object-oriented languages. Think of it as the page's factory
00186     object because it provides a way to create instances of the
00187     page and maintains information that must be known even when no
00188 
00189     instances of the page are available.</dd>
00190 
00191     <dt>public void run() throws Fault</dt>
00192 
00193     <dd>The page's run method sends the page's html to the browser
00194     and processes form parameters that the browser returns as its
00195 
00196     response.</dd>
00197 
00198   </dl>
00199 
00200   <h3>MetaPage</h3>
00201 
00202   <p>#link("LOGOUT") is the simplest page in the demo, but it
00203   contains all of the elements that are required of all pages.
00204   Notice the MetaPage constructor, which takes the following 
00205   parameters:</p>
00206 
00207   <ul>
00208 
00209     <li>The first is the page's class. The servlet uses this to
00210     construct an instance of the page to service each page 
00211     request.</li>
00212 
00213     <li>The second is the page's dispatch key. This is a unique
00214     name for each page. The form and link commands will use this to 
00215     compose the URL that browsers use to refer to the page.</li>
00216 
00217     <li>The third is a brief string (ideally one word) that the
00218     link method will use as the anchor text in navigation bars and
00219     hotlinks if an explicit anchor is not provided as an 
00220     argument.</li>
00221 
00222     <li>The fourth is a longer string that the link method will use
00223     as the default title string in navigation bars, page titles and
00224     hotlinks, if an explicit title is not provided as an 
00225     argument.</li>
00226 
00227     <li>The fifth determines who can access the page. It must be an
00228     an object that abides by JWAA's #link("GenericRole") interface,
00229     such as #link("Role"). This class defines three capability
00230     levels, Visitor for those who have not registered and logged
00231     in, Registered for those who have, and Admin for specially
00232     authorized administrative accounts. The capability parameter
00233     determines whether the navigation bar will offer links to that
00234     page and whether the servlet will dispatch requests for that
00235     visitor. The servlet will redirect unauthorized requests to the 
00236     page specified by the servlet's getRefusePage() method.</li>
00237 
00238     <li>The sixth defines this page's contribution to the page
00239     hierarchy as an MetaPage[] array of sub-pages. This parameter
00240     is optional because the notion of "hierarchy" is not intrinsic,
00241     but is often added to help users understand and navigate the 
00242     site.</li>
00243 
00244   </ul>
00245 
00246   <p>The Meta structures must abide by the following rules, which
00247   are checked when the servlet engine is starting up. Violations
00248   are not fatal but are logged to the log file to warn you to fix
00249 
00250   them.</p>
00251 
00252   <ul>
00253 
00254     <li>Each page's dispatch key must be globally unique since
00255 
00256     external browsers use this to uniquely identify the page.</li>
00257 
00258     <li>Each page may not be a child of multiple parents. The page
00259     hierarchy is a tree, not a graph, and is managed as a doubly
00260 
00261     linked parent-child tree.</li>
00262 
00263     <li>Pages that are in the hierarchy will be automatically
00264     loaded when the servlet engine is starting up. The trigger is
00265     that #link("JwaaServlet") is imported (by the
00266     #link("GenericServlet") interface), which references the root
00267     of the page hierarchy #link("JwaaPage"). If a page does not
00268     appear in the hierarchy, the class loader won't load it. Pages
00269     that aren't in the hierarchy must be explicitly referenced
00270     elsewhere or they will not be registered in the dispatch table
00271 
00272     so dispatches will fail.</li>
00273 
00274   </ul>
00275 
00276   <h3>Run Method</h3>
00277 
00278   <p>A page's meta object specifies its static nature and its
00279   dynamic nature is determined by its run method.
00280   #link("LOGOUT") has the simplest run method, because it sends
00281   nothing at all to the browser. It simply logs the visitor out by
00282   calling setViewer(null) to erase the viewer's account information
00283 
00284   from the session and forwards control elsewhere.</p>
00285 
00286   <p>#link("INNER") is more typical. Its run method uses the
00287   send method (that all pages inherit from #link("JwaaPage")) to
00288   send a long batch of HTML text to the browser. The problem here
00289   is how to write the HTML, since Java's String syntax is oriented 
00290   to short uni-line strings. $fixme()</p>
00291 
00292   <p>A final refinement is to eliminate the leading and trailing
00293   html that define the site's look and feel by moving it into
00294   static methods of an application-specific JwaaPage.template
00295   class. With these two refinements, the run method of a simple
00296 
00297   "Hello World" application would be written like this:</p>
00298 
00299   <pre>
00300  send({{&lt;h1&gt;Hello world&lt;/h1&gt;}});
00301 
00302 
00303 </pre>
00304 
00305   <p>See #link("INNER") and #link("JwaaPage") for fully
00306 
00307   elaborated examples.</p>
00308 
00309   <h3>Cross-page References</h3>
00310 
00311   <p>Although you can always write &lt;a href&gt; and &lt;form&gt;
00312   commands as hard-coded strings within the html, JWAA provides a
00313   new technique that does exactly the same thing but allows linkage 
00314   errors to be detected at compile-time.</p>
00315 
00316   <p>All pages inherit link, form, forward, and redirect methods
00317   from #link("JwaaPage") which all require a MetaPage instance as
00318   their first argument. The link and form commands return the
00319   appropriate &lt;a href&gt; or &lt;form&gt; command as a String
00320   while the forward and redirect commands actually redirect control
00321   to the specified page. Java's strong type-checking automatically
00322   determines if the MetaPage structure is not accessible. For 
00323   example, the Hello World example might be extended like this:</p>
00324 
00325   <pre> 
00326  send({{
00327 &lt;h1&gt;Hello world&lt;/h1&gt; {{link(SomeOtherPage.meta, "Click
00328 here")}} for more information.  }}); 
00329 </pre>
00330 
00331   <p>See #link("INNER") for a longer example.</p>
00332 
00333   <h3>Interactive Pages</h3>
00334 
00335   <p>Non interactive pages (pages that don't process form
00336   parameters) typically handle everything in their run() method.
00337   Interactive pages, such as the #link("EDITOR") which supports
00338   editing of an account's registration information, confine the run
00339   method to specifying the page's logic and handle presentation
00340   elsewhere, either in a private method of the same class
00341   (integration of concerns) or in a separate file (separation of
00342 
00343   concerns) as described #link("INTERACTION" "here").</p>
00344 
00345   <p>The #link("PORTAL"), #link("EDITOR"),
00346   #link("RegisterPage"), and #link("ADMIN") pages provide fully
00347   developed examples of the integration of concerns approach.
00348   Notice that the control logic invariably follows this simple
00349 
00350   pattern:</p>
00351 
00352   <ol>
00353 
00354     <li>Gather the form parameters by using getField to build
00355 
00356     #link("Field") instances.</li>
00357 
00358     <li>If all field instances are valid, process them.</li>
00359 
00360     <li>Otherwise prompt the user to fix them.</li>
00361 
00362   </ol>
00363 
00364   <p>The else clause automatically handles the initial presentation
00365   of the page, when all fields contain default values (typically
00366   empty ones), and subsequent presentations in which one or more
00367   fields contain errors that the user must fix to proceed. Notice
00368   that the run method boils down to a few initialization statements 
00369   and an if statement. It can't get any simpler than that!</p>
00370 
00371   <h2><a name="mortar" id="mortar">Support Classes: the Mortar</a></h2>
00372 
00373   <p>In addition to the Page classes described above, every
00374   application needs a number of supporting classes to glue the
00375   separate pages into a functioning application. The remaining
00376   sections describe the supporting classes for the demo 
00377   application.</p>
00378   
00379   </page>
00380 
00381 <page id="OLD_DOCUMENTATION" name="Documentation" requiresRole="none"
00382 title="Pages are atoms, applications the molecules" >
00383 <logic />
00384 
00385 <child idref="METAPAGES"></child> 
00386 <child idref="ROLES"></child> 
00387 <child idref="SERVLETS"></child> 
00388 <child idref="MODELS"></child> 
00389 <child idref="FIELDS"></child>
00390 
00391 
00392 <p><img align="right" src="/jwaa/pix/states.gif" width="167" height="177" />
00393 The six pages that make up the demo application are shown in this figure 
00394 as ovals with the hotlinks that connect them as arrows.</p>
00395 
00396 <p>Each of the pages in this figure are generated by objects,
00397 called <i>Page Objects</i>. These are defined by classes that you define
00398 while developing your application. Browsers issue page requests
00399 with urls of the form 
00400 
00401 <code> http://hostname:port/contextName/servletName/pageName...</code>.
00402 
00403 If a servlet engine is configured to listen to that port on that
00404 host, it routes the request to the servlet configured to respond
00405 to the specified <code>context</code>and <code>servlet</code>.
00406 The servlet dispatches the request to the desired page based on 
00407 the url. You define the servlet and other glue classes as will be 
00408 described later.</p>
00409 
00410 <p>Every page is defined by a Page class, each of which defines a
00411 MetaPage, a no-args constructor, and a run method as shown here:</p>
00412 
00413 <pre>
00414  public class ExamplePage extends GenericPage { public final static
00415 MetaPage meta = new MetaPage(
00416  ExamplePage.class, "PageMapping", "AnchorText", "TitleText",
00417  ...capability, new MetaPage[] { subPages.meta, }
00418 ); public ExamplePage() {} public final void run() throws Fault {
00419  page.send(...);
00420 } }
00421 
00422 
00423 </pre>The PageMapping argument must be a unique name for this page.
00424 It must be consistent with the &lt;servlet-mapping&gt; defined in
00425 your web.xml file. For example If servlet-name is foo,
00426 servlet-mapping is bar, and the name of this page is gag the
00427 PageMapping value must be /bar/gag. The servlet-name is
00428 automatically added as required. 
00429 
00430 <p>The servlet calls the no-args constructor to create a page
00431 instance, initializes the instance with information it needs to
00432 respond to browser requests, and calls the instance's run method
00433 to handle the request. The MetaPage instance bundles static
00434 information about the page's relationship to other pages and will
00435 
00436 be describe later.</p>
00437 
00438 <p>The run method is where all the action happens. For
00439 non-interactive pages (pages that don't process forms), the run
00440 method simply sends html text to the browser (via the 
00441 
00442 <code>page.send(...) call outlined above</code>). Interactive
00443 pages can be considerably more elaborate since they must decide
00444 
00445 what to send based on form parameter inputs.</p>
00446 
00447 <h2>Templates</h2>
00448 
00449 <p>Multi-page web applications must do something to ensure that
00450 the site's standard look and feel is preserved as the user moves
00451 from page to page. In Jwaa, the solution is to define a 
00452 
00453 <code>Template</code>class with static methods that the run
00454 methods call to compute the standard html boilerplate at the
00455 beginning and end of each page. For example, run methods are
00456 
00457 typically coded like this:</p>
00458 
00459 <pre>
00460  public final void
00461 run() throws Fault {
00462  page.send(...);
00463 }
00464 
00465 
00466 </pre>
00467 
00468 <p>Note that JWAA's only requirement is that a run method sends a
00469 response to the browser. It does not constrain 
00470 
00471 <i>how</i>run methods compute that response. One approach,
00472 emphasizing 
00473 
00474 <i>separation of concerns</i>, is to read the response text from
00475 a file, typically with the aid of a template engine such as
00476 Velocity. The separation of concerns approach is described 
00477 separately, see #link("INTERACTION" "Dynamic Content").</p>
00478 
00479 <p>A different approach, emphasizing 
00480 <i>integration of concerns</i>, is to maintain the response text
00481 as strings within the application. I will emphasize the latter
00482 approach here because this is the approach used in the Demo
00483 
00484 applications.</p>
00485 
00486 <h2>Multi-line Strings</h2>send html boilerplate at the
00487 beginning/end of each page, and send(...) sends the page content.
00488 It would be tedious to express long (multiline) text as Java
00489 Strings, so #link("Java+") was developed to support syntax for
00490 long strings that isn't as tedious as Java string syntax. 
00491 
00492 <p>#link("INNER") demonstrates everything we've discussed
00493 so far. Notice that the page's content is a single long string
00494 enclosed in Java+ string delimiters (
00495 
00496 <code>{{...}}</code>)</p>
00497 
00498 <h2>Executable Inclusions</h2>
00499 
00500 <p>Notice that the long string contains java code enclosed in
00501 embedded 
00502 
00503 <code>{{...}}</code>delimiters (for example at the beginning of
00504 the second paragraph). These are 
00505 
00506 <i>executable inclusions</i>; Java expressions embedded within
00507 strings to be evaluated and spliced into the surrounding string
00508 
00509 at run-time.</p>
00510 
00511 <p>Executable inclusions are a generally useful feature. One
00512 common use is supporting compile-time checking of cross-page
00513 references, or hotlinks. The first paragraph shows the
00514 conventional technique in which pages reference other pages (or
00515 in this case, a graphic) by hard-coding &lt;a href=""&gt; (or in
00516 
00517 this case, an &lt;img...&gt; command) into the html.</p>
00518 
00519 <p>The JWAA alternative is shown in the second paragraph. An
00520 executable inclusion is used to splice the output of the 
00521 
00522 <code>link</code>command into the surrounding string. The link
00523 command's argument, EntryPage.meta, identifies the target page
00524 such that if the page is ever renamed or moved, a compile-time
00525 error will report the error rather than letting the user discover
00526 
00527 the problem at run-time as in the first approach.</p>
00528 
00529 <p>Another improvement is less obvious. Java servlets provide two
00530 techniques for tracking session information: cookies and
00531 url-rewriting. When a user logs in successfully, their identity
00532 is typically recorded as a cookie as they move from page to page.
00533 However some browsers don't support cookies, and some users turn
00534 them off, so the fallback technique, url rewriting, is needed for
00535 the system to remember who they are. But url rewriting only works
00536 when each and every url is generated on-the-fly, and is of no use
00537 when urls are hard-coded inside html text. By habitually adopting
00538 the JWAA technique, user sessions are automatically preserved
00539 even when the system can't count on cookies for maintaining the
00540 
00541 user's identity.</p>
00542 
00543 <h2>Meta Pages</h2>
00544 
00545 <p>Notice that the link command's argument ends in .meta. This is
00546 the first, and simplest use of the MetaPage object that was
00547 defined without explanation in the ExamplePage example. By
00548 convention, every page class defines a public final static
00549 variable, named meta. Other pages rely on this variable for
00550 
00551 generating hotlinks to that page as demonstrated above.</p>
00552 
00553 <p>Also notice that the link command in this example contains a
00554 second argument, "Click here". If this argument were omitted, the
00555 anchor text of the emitted url would have been as defined in the
00556 MetaPage constructor argument, "Entry" in this case. But since we
00557 wanted the anchor to read "Click here" in this case, we overrode
00558 
00559 the default by providing the additional argument.</p>
00560 
00561 <p>This is only the simplest example of how the MetaPage is used.
00562 
00563 See #link("MetaPageDox") for more information.</p></page>
00564 
00565 <page id="ROLES" name="Roles" requiresRole="none"
00566 title="Roles determine who can see what pages" >
00567 
00568 <logic />
00569 
00570 <p>Since each application is likely to have unique requirements
00571 for determining who is allowed to access each page, JWAA
00572 capability system is extendible. The demonstration uses the
00573 
00574 simple three-level role system defined in #link("Role").</p>
00575 
00576 <ul>
00577 
00578 <li>Visitor: for visitors whose identity is unknown because
00579 
00580 they've not registered and logged on.</li>
00581 
00582 <li>Registered: for visitors that have registered and logged
00583 
00584 on.</li>
00585 
00586 <li>Admin: a privileged account permitted to access
00587 
00588 administrative functions.</li>
00589 
00590 </ul>
00591 
00592 <p>In practice, the pages of a web application can be thought
00593 of as an outer lobby with the pages that should be accessible
00594 to everyone, and an inner sanctum with the pages that can only
00595 be accessed by those who have logged in. In the demo
00596 application, the inner sanctum consists of only three pages.
00597 #link("EDITOR") supports editing an account's registration
00598 information, #link("LOGOUT"), supports logging out, and
00599 #link("AdminPage"), is available only to administrative
00600 
00601 users.</p>
00602 
00603 <p>#link("PORTAL") is the doorway between the two regions.
00604 Until a visitor has registered and logged in, their account is
00605 null, signifying a special account reserved for unknown 
00606 visitors.</p>
00607 
00608 <p>When the visitor identifies themselves to the login
00609 procedure (in the demo, by providing a previously registered
00610 email address, but more typically, by also providing the
00611 corresponding password), #link("PORTAL")loads the account's
00612 persistent information from the database as an instance of
00613 #link("AccountAbstraction") and stores a reference to this
00614 instance as a specially named session attribute. The session
00615 attribute persists as the user browses from page to page. It
00616 will only be erased by the LogoutPage, if the session times 
00617 out, or if the server itself is restarted.</p>
00618 
00619 <p>Notice that #link("DemoAccount") implements the
00620 #link("AccountAbstraction") interface, which requires that all
00621 subclasses provide a isCapable(Capability c) method. The system
00622 calls this method to determine if a given account has the 
00623 capability level in the method's argument.</p>
00624 
00625 <ul>
00626 
00627 <li>#link("LookAndFeelDox") will refuse to show links to that
00628 page in the navigation bar. This lets the page hierarchy,
00629 which governs the navigation bar, be specified without
00630 concern for account capabilities, since the navigation bar
00631 will offer only the links that the viewer is allowed to
00632 
00633 access.</li>
00634 
00635 <li>#link("GenericServlet") redirects requests from viewers
00636 with inadequate capabilities by redirecting them to the page
00637 specified by the servlet's getRefusePage() method. This is a
00638 second line of defense to ensure that unauthorized visitors
00639 
00640 can't bypass the protection by editing requests by hand.</li>
00641 
00642 </ul>
00643 
00644 </page>
00645 
00646 <page id="SERVLETS" name="Servlets" requiresRole="none"
00647 title="Servlets are the top-level control point for an application" >
00648 
00649 <p>Each web application defines an instance of
00650 #link("GenericServlet") to handle all requests by that
00651 application. Every application's servlet simply overrides the
00652 following methods. For example, see the demo's servlet class, 
00653 #link("JwaaServlet").</p>
00654 
00655 <dl>
00656 
00657 <dt>public void init(ServletConfig servletConfig) throws
00658 
00659 ServletException</dt>
00660 
00661 <dd>All servlets override this method. Nothing special
00662 
00663 here.</dd>
00664 
00665 <dt>public final MetaPage getDefaultPage() { return
00666 
00667 DemoHomePage.meta; }</dt>
00668 
00669 <dd>The page to be displayed if the servlet can't locate the
00670 page named in the browser request. This might happen if the
00671 user composes the request manually. Another possibility is
00672 that the page is not registered in the page dispatch table
00673 because it was not loaded by the class loader, as might
00674 happen if the page is not listed as a child of some other
00675 
00676 page in the page hierarchy.</dd>
00677 
00678 <dt>public final MetaPage getRefusePage() { return
00679 
00680 EntryPage.meta; }</dt>
00681 
00682 <dd>This is the page the servlet should dispatch to if the
00683 user requests a page for which they don't have the required
00684 capability. The demo handles this by silently forwarding them
00685 to the default entrance page (EntryPage). Other applications
00686 
00687 might forward to an explicit "permission denied" page.</dd>
00688 
00689 <dt>private final static MetaPage rootPage = JwaaRoot.meta;
00690 
00691 <br />public final MetaPage getRootPage() { return rootPage;
00692 
00693 }</dt>
00694 
00695 <dd>This method exists as a reminder that the servlet must
00696 reference the root of the page hierarchy so that the class
00697 loader will automatically load all pages in the hierarchy as
00698 the servlet starts up. If you have pages that aren't listed
00699 in the hierarchy, the servlet will be unable to dispatch them
00700 since their name won't be defined in its dispatch table.
00701 Eliminating the static variable above will not work; the root
00702 
00703 reference must occur at load time, not run time.</dd>
00704 
00705 </dl>
00706 
00707 </page>
00708 
00709 <page id="FIELDS" name="Fields" requiresRole="none"
00710 title="Fields are the atoms; models the molecules" >
00711 
00712 <logic></logic>
00713 
00714 <p>Models are made of Fields not Strings. Strings are avoided
00715 
00716 because:</p>
00717 
00718 <ul>
00719 
00720 <li>Fields are uniquely reusable units of reuse because they
00721 are so concrete, immediately tangible and visible to everyone
00722 
00723 in the application's user interface</li>
00724 
00725 <li>Fields encapsulate the rules for determining whether
00726 user-provided inputs comply with the syntactical validity
00727 constraints of that field. Centralizing this logic in
00728 reusable field classes makes applications much simpler than
00729 the traditional approach of distributing this logic
00730 
00731 throughout the application.</li>
00732 
00733 <li>Each field can enforce DBMS-specific constraints, such as
00734 
00735 the maximum length of fixed-length fields.</li>
00736 
00737 </ul>
00738 
00739 <p>The JWAA package provides a #link("Validatable") interface,
00740 which defines the protocol that all field implementations
00741 adhere to. It also provides an abstract #link("Field") that
00742 
00743 defines methods for subclasses to inherit.</p>
00744 
00745 <p>The full list of Field classes is available in the
00746 #link("SOURCE"). The ones used by the demo application are
00747 #link("City"), #link("Country"), #link("Email"), #link("ID"),
00748 #link("Identifier"), #link("Name"), #link("Op"), #link("Pass"),
00749 
00750 #link("Phone"), #link("Street"), and #link("Zipcode").</p>
00751 
00752 <p>Typically you'd define your database to use the field
00753 lengths specified by these classes. If not you should edit the
00754 classes to match. Only U.S. address and phone numbers are
00755 provided. Revisions to support international usage would be
00756 
00757 gratefully accepted.</p>
00758 
00759 </page>
00760 
00761 <page id="METAPAGES" name="MetaPages" requiresRole="none"
00762 title="MetaPages collect pages into hierarchies" >
00763 
00764 <logic />
00765 
00766 <p>The developer sees each page as just one of many
00767 interconnected objects, the end-user wants to see them
00768 organized into simple hierarchies, with a home page at the top
00769 and clear navigational hints for moving from one to another.
00770 JWAA supports both of these views. Each page is an object as
00771 defined on the previous page. If pages are organized into
00772 hierarchies via the MetaPage structure to be described here,
00773 JWAA will generate navigational bars, such as those at the top
00774 
00775 of this page, automatically.</p>
00776 
00777 <p>Pages are instances of #link("GenericPage") subclasses that
00778 are created by the servlet to handle each request and discarded
00779 immediately thereafter. MetaPages, by contrast, are instances
00780 of #link("MetaPage") that are created at load time by each page
00781 class to initialize a 
00782 
00783 <code>public final static</code>variable, named by convention, 
00784 
00785 <code>meta</code>. The MetaPage maintains meta information
00786 about Page instances, such as their url, the page's default
00787 anchor and title strings, the accounts that permitted to access
00788 that page (the 
00789 
00790 <i>Capability</i>), and the page's position in the site's page
00791 
00792 hierarchy.</p>
00793 
00794 <p>JwaaPage defines several abstract methods such that each
00795 subclass must implement the following methods. In addition,
00796 pages can and often do define additional methods to support the
00797 
00798 mandatory ones listed here</p>
00799 
00800 <dl>
00801 
00802 <dt>public PageName()</dt>
00803 
00804 <dd>Each page class must be public and must define a public
00805 no-args constructor. The application's servlet uses the
00806 constructor to create an instance of your Page class to
00807 handle the request. It then calls the instance's public final
00808 void init() populate the Page's four instance variables with
00809 the request-specific information that the page needs to
00810 handle the request: 
00811 
00812 <ol>
00813 
00814 <li>The servlet that dispatched the request</li>
00815 
00816 <li>The HttpServletRequest instance</li>
00817 
00818 <li>The HttpServletResponse instance</li>
00819 
00820 </ol></dd>
00821 
00822 <dt>public final static MetaPage meta = new
00823 
00824 MetaPage(...)</dt>
00825 
00826 <dd>Every page defines a global handle, by convention named
00827 "meta", that other pages will use to reference the page in
00828 hotlinks, form references, redirects and forward commands.
00829 This structure would be the page's meta class in other
00830 object-oriented languages. Think of it as the page's factory
00831 object because it provides a way to create instances of the
00832 page and maintains information that must be known even when
00833 
00834 no instances of the page are available.</dd>
00835 
00836 <dt>public void run() throws Fault</dt>
00837 
00838 <dd>The page's run method sends the page's html to the
00839 browser and processes form parameters that the browser
00840 
00841 returns as its response.</dd>
00842 
00843 </dl>
00844 
00845 <h3>MetaPage</h3>
00846 
00847 <p>#link("LOGOUT") is the simplest page in the demo, but it
00848 contains all of the elements that are required of all pages.
00849 Notice the MetaPage constructor, which takes the following
00850 
00851 parameters:</p>
00852 
00853 <ul>
00854 
00855 <li>The first is the page's class. The servlet uses this to
00856 construct an instance of the page to service each page
00857 
00858 request.</li>
00859 
00860 <li>The second is the page's dispatch key. This is a unique
00861 name for each page. The form and link commands will use this
00862 to compose the URL that browsers use to refer to the
00863 
00864 page.</li>
00865 
00866 <li>The third is a brief string (ideally one word) that the
00867 link method will use as the anchor text in navigation bars
00868 and hotlinks if an explicit anchor is not provided as an
00869 
00870 argument.</li>
00871 
00872 <li>The fourth is a longer string that the link method will
00873 use as the default title string in navigation bars, page
00874 titles and hotlinks, if an explicit title is not provided as
00875 
00876 an argument.</li>
00877 
00878 <li>The fifth determines who can access the page. It must be
00879 an an object that abides by JWAA's #link("GenericRole")
00880 interface, such as #link("Role"). This class defines three
00881 capability levels, Visitor for those who have not registered
00882 and logged in, Registered for those who have, and Admin for
00883 specially authorized administrative accounts. The capability
00884 parameter determines whether the navigation bar will offer
00885 links to that page and whether the servlet will dispatch
00886 requests for that visitor. The servlet will redirect
00887 unauthorized requests to the page specified by the servlet's
00888 
00889 getRefusePage() method.</li>
00890 
00891 <li>The sixth defines this page's contribution to the page
00892 hierarchy as an MetaPage[] array of sub-pages. This parameter
00893 is optional because the notion of "hierarchy" is not
00894 intrinsic, but is often added to help users understand and
00895 
00896 navigate the site.</li>
00897 
00898 </ul>
00899 
00900 <p>The Meta structures must abide by the following rules, which
00901 are checked when the servlet engine is starting up. Violations
00902 are not fatal but are logged to the log file to warn you to fix
00903 
00904 them.</p>
00905 
00906 <ul>
00907 
00908 <li>Each page's dispatch key must be globally unique since
00909 external browsers use this to uniquely identify the
00910 
00911 page.</li>
00912 
00913 <li>Each page may not be a child of multiple parents. The
00914 page hierarchy is a tree, not a graph, and is managed as a
00915 
00916 doubly linked parent-child tree.</li>
00917 
00918 <li>Pages that are in the hierarchy will be automatically
00919 loaded when the servlet engine is starting up. The trigger is
00920 that #link("JwaaServlet") is imported (by the
00921 #link("GenericServlet") interface), which references the root
00922 of the page hierarchy #link("JwaaPage"). If a page does not
00923 appear in the hierarchy, the class loader won't load it.
00924 Pages that aren't in the hierarchy must be explicitly
00925 referenced elsewhere or they will not be registered in the
00926 
00927 dispatch table so dispatches will fail.</li>
00928 
00929 </ul>
00930 
00931 <h3>Run Method</h3>
00932 
00933 <p>A page's meta object specifies its static nature and its
00934 dynamic nature is determined by its run method.
00935 #link("LOGOUT") has the simplest run method, because it
00936 sends nothing at all to the browser. It simply logs the visitor
00937 out by calling setViewer(null) to erase the viewer's account
00938 information from the session and forwards control
00939 
00940 elsewhere.</p>
00941 
00942 <p>#link("INNER") is more typical. Its run method uses
00943 the send method (that all pages inherit from #link("JwaaPage"))
00944 to send a long batch of HTML text to the browser. The problem
00945 here is how to write the HTML, since Java's String syntax is
00946 oriented to short uni-line strings. JWAA relies on the
00947 
00948 $fixme()</p>
00949 
00950 <p>A final refinement is to eliminate the leading and trailing
00951 html that define the site's look and feel by moving it into
00952 static methods of an application-specific JwaaPage.template
00953 class. With these two refinements, the run method of a simple
00954 
00955 "Hello World" application would be written like this:</p>
00956 
00957 <pre>
00958 send({{&lt;h1&gt;Hello world&lt;/h1&gt;}}); 
00959 </pre>
00960 
00961 <p>See #link("INNER") and #link("JwaaPage") for
00962 
00963 examples.</p>
00964 
00965 <h3>Cross-page References</h3>
00966 
00967 <p>Although you can always write &lt;a href&gt; and
00968 &lt;form&gt; commands as hard-coded strings within the html,
00969 JWAA provides a new technique that does exactly the same thing 
00970 but allows linkage errors to be detected at compile-time.</p>
00971 
00972 <p>All pages inherit link, form, forward, and redirect methods
00973 from #link("JwaaPage") which all require a MetaPage instance as
00974 their first argument. The link and form commands return the
00975 appropriate &lt;a href&gt; or &lt;form&gt; command as a String
00976 while the forward and redirect commands actually redirect
00977 control to the specified page. Java's strong type-checking
00978 automatically determines if the MetaPage structure is not
00979 accessible. For example, the Hello World example might be 
00980 extended like this:</p>
00981 
00982 <pre>
00983  send({{
00984 &lt;h1&gt;Hello world&lt;/h1&gt; {{link(SomeOtherPage.meta, "Click
00985 here")}} for more information.  }}); 
00986 </pre>
00987 
00988 <p>See #link("INNER") for a longer example.</p>
00989 
00990 <h3>Interactive Pages</h3>
00991 
00992 <p>Non interactive pages (pages that don't process form
00993 parameters) typically handle everything in their run() method.
00994 Interactive pages, such as the #link("EDITOR") which
00995 supports editing of an account's registration information,
00996 confine the run method to specifying the page's logic and
00997 handle presentation elsewhere, either in a private method of
00998 the same class (integration of concerns) or in a separate file
00999 (separation of concerns) as described #link("INTERACTION" 
01000 "here").</p>
01001 
01002 <p>The #link("PORTAL"), #link("EDITOR"), #link("RegisterPage"), 
01003 and #link("AdminPage") pages demonstrate the integration of 
01004 concerns approach. Notice that the control logic invariably 
01005 follows this simple pattern:</p>
01006 
01007 <ol>
01008 
01009 <li>Gather the form parameters by using getField to build 
01010 #link("Field") instances.</li>
01011 
01012 <li>If all fields are valid, process them.</li>
01013 
01014 <li>Else prompt the user to fix them.</li>
01015 
01016 </ol>
01017 
01018 <p>The else clause above automatically handles the initial
01019 presentation of the page, when all fields contain default
01020 values (typically empty ones). Subsequent presentations handle
01021 the case where one or more fields contain errors that the user 
01022 must fix to proceed. Notice that the run method boils down to 
01023 a few initialization statements and an if statement. It can't 
01024 get any simpler than that!</p>
01025 
01026 <h2> <a name="mortar" id="mortar">Support Classes: the Mortar</a> </h2>
01027 
01028 <p>In addition to the Page classes described above, every
01029 application needs a number of supporting classes to glue the
01030 separate pages into a functioning application. The remaining
01031 sections describe the supporting classes for the demo
01032 application: #link("JwaaPage"), #link("JwaaServlet") and
01033 
01034 #link("Role").</p>
01035 
01036 <h3>JwaaPage.template Class</h3>
01037 
01038 <p>How a page emits its html is entirely up to the page's run
01039 method. But the recommended style is to move common look and
01040 feel issues into static methods of an application-specific
01041 JwaaPage.template class so that look and feel can be shared by
01042 
01043 all pages.</p>
01044 
01045 <p>The demonstration application's JwaaPage.template class is
01046 #link("JwaaPage"). It uses CSS (Cascading Style Sheets)
01047 commands extensively. It also uses the MetaPage page hierarchy
01048 to add navigational menus at the top of each page. To build
01049 your own application, copy this class to an
01050 
01051 application-specific new name and modify to suit.</p>
01052 
01053 <h3>Servlet Class</h3>
01054 
01055 <p>Each web application defines an instance of
01056 #link("GenericServlet") to handle all requests by that
01057 application. Every application's servlet simply overrides the
01058 following methods. For example, see the demo's servlet class, 
01059 #link("JwaaServlet").</p>
01060 
01061 <dl>
01062 
01063 <dt>public void init(ServletConfig servletConfig) throws
01064 
01065 ServletException</dt>
01066 
01067 <dd>All servlets override this method. Nothing special
01068 
01069 here.</dd>
01070 
01071 <dt>public final MetaPage getDefaultPage() { return
01072 
01073 DemoHomePage.meta; }</dt>
01074 
01075 <dd>The page to be displayed if the servlet can't locate the
01076 page named in the browser request. This might happen if the
01077 user composes the request manually. Another possibility is
01078 that the page is not registered in the page dispatch table
01079 because it was not loaded by the class loader, as might
01080 happen if the page is not listed as a child of some other
01081 
01082 page in the page hierarchy.</dd>
01083 
01084 <dt>public final MetaPage getRefusePage() { return
01085 
01086 EntryPage.meta; }</dt>
01087 
01088 <dd>This is the page the servlet should dispatch to if the
01089 user requests a page for which they don't have the required
01090 capability. The demo handles this by silently forwarding them
01091 to the default entrance page (EntryPage). Other applications
01092 
01093 might forward to an explicit "permission denied" page.</dd>
01094 
01095 <dt>private final static MetaPage rootPage = JwaaRoot.meta;
01096 
01097 <br />public final MetaPage getRootPage() { return rootPage;
01098 
01099 }</dt>
01100 
01101 <dd>This method exists as a reminder that the servlet must
01102 reference the root of the page hierarchy so that the class
01103 loader will automatically load all pages in the hierarchy as
01104 the servlet starts up. If you have pages that aren't listed
01105 in the hierarchy, the servlet will be unable to dispatch them
01106 since their name won't be defined in its dispatch table.
01107 Eliminating the static variable above will not work; the root
01108 
01109 reference must occur at load time, not run time.</dd>
01110 
01111 </dl>
01112 
01113 <h3>Capability Class</h3>
01114 
01115 <p>Since each application is likely to have unique requirements
01116 for determining who is allowed to access each page, JWAA
01117 capability system is extendible. The demonstration uses the
01118 simple three-level capability system defined in
01119 
01120 #link("Role").</p>
01121 
01122 <ul>
01123 
01124 <li>Visitor: for visitors whose identity is unknown because
01125 
01126 they've not registered and logged on.</li>
01127 
01128 <li>Registered: for visitors that have registered and logged
01129 
01130 on.</li>
01131 
01132 <li>Admin: a privileged account permitted to access
01133 
01134 administrative functions.</li>
01135 
01136 </ul>
01137 
01138 <p>In practice, the pages of a web application can be thought
01139 of as an outer lobby with the pages that should be accessible
01140 to everyone, and an inner sanctum with the pages that can only
01141 be accessed by those who have logged in. In the demo
01142 application, the inner sanctum consists of only three pages.
01143 #link("EDITOR") supports editing an account's registration
01144 information, #link("LOGOUT"), supports logging out, and
01145 #link("AdminPage"), is available only to administrative
01146 
01147 users.</p>
01148 
01149 <p>#link("PORTAL") is the doorway between the two regions.
01150 Until a visitor has registered and logged in, their account is
01151 null, signifying a special account reserved for unknown
01152 
01153 visitors.</p>
01154 
01155 <p>When the visitor identifies themselves to the login
01156 procedure (in the demo, by providing a previously registered
01157 email address, but more typically, by also providing the
01158 corresponding password), #link("PORTAL")loads the account's
01159 persistent information from the database as an instance of
01160 #link("DemoAccount") and stores a reference to this instance as
01161 a specially named session attribute. The session attribute
01162 persists as the user browses from page to page. It will only be
01163 erased by the LogoutPage, if the session times out, or if the 
01164 server itself is restarted.</p>
01165 
01166 <p>Notice that #link("DemoAccount") implements the
01167 #link("AccountAbstraction") interface, which requires that all
01168 subclasses provide a isCapable(Capability c) method. The system
01169 calls this method to determine if a given account has the 
01170 capability level in the method's argument.</p>
01171 
01172 <ul>
01173 
01174 <li>#link("JwaaPage") will refuse to show links to that page
01175 in the navigation bar. This lets the page hierarchy, which
01176 governs the navigation bar, be specified without concern for
01177 account capabilities, since the navigation bar will offer
01178 
01179 only the links that the viewer is allowed to access.</li>
01180 
01181 <li>#link("GenericServlet") redirects requests from viewers
01182 with inadequate capabilities by redirecting them to the page
01183 specified by the servlet's getRefusePage() method. This is a
01184 second line of defense to ensure that unauthorized visitors
01185 
01186 can't bypass the protection by editing requests by hand.</li>
01187 
01188 </ul>
01189 
01190 <h3>Model Classes</h3>
01191 
01192 <p>In addition to an application's user-interface components
01193 (Views/Controllers) every application needs one or more Model
01194 classes to manage the application's data as persistent
01195 
01196 objects.</p>
01197 
01198 <p>The demo's model class is #link("DemoAccount"), which
01199 demonstrates the principle that <i>Fields are to models as atoms 
01200 are to molecules.</i> Models are not defined in terms of basic 
01201 Java types such as Strings. Rather the models' instance variables and
01202 method arguments are uniformly declared in terms of objects
01203 that abide by the #link("Validatable") interface. Since these
01204 objects are typically implemented as subclasses of
01205 #link("Field"), these higher-level classes are called Field
01206 
01207 Classes.</p>
01208 
01209 <h3>Field Classes</h3>
01210 
01211 <p>Models are defined in terms of #link("Field")s, not Strings,
01212 
01213 because:</p>
01214 
01215 <ul>
01216 
01217 <li>Fields are uniquely reusable units of reuse because they
01218 are so concrete, immediately tangible and visible to everyone
01219 
01220 in the application's user interface</li>
01221 
01222 <li>Fields encapsulate the rules for determining whether
01223 user-provided inputs comply with the syntactical validity
01224 constraints of that field. Centralizing this logic in
01225 reusable field classes makes applications much simpler than
01226 the traditional approach of distributing this logic
01227 
01228 throughout the application.</li>
01229 
01230 <li>Each field can enforce DBMS-specific constraints, such as
01231 
01232 the maximum length of fixed-length fields.</li>
01233 
01234 </ul>
01235 
01236 <p>The JWAA package provides a #link("Validatable") interface,
01237 which defines the protocol that all field implementations
01238 adhere to. It also provides an abstract #link("Field") that 
01239 defines methods for subclasses to inherit.</p>
01240 
01241 <p>Typically you'd define your database to use the field
01242 lengths specified by these classes. If not you should edit the
01243 classes to match. Only U.S. address and phone numbers are
01244 provided. International contributions would be gratefully 
01245 accepted.</p>
01246 
01247 </page>
01248 
01249 <page id="MODELS" name="Models" requiresRole="none"
01250 title="Models are an application's persistent memory" >
01251 
01252 <logic />
01253 
01254 <p>In addition to an application's user-interface components
01255 (Views/Controllers) every application needs one or more Model
01256 classes to manage the application's data as persistent 
01257 objects.</p>
01258 
01259 <p>The demo's model class is #link("DemoAccount").  <i>Fields 
01260 are to models as atoms are to molecules.</i>means that
01261 models are not defined in terms of raw Java types such as
01262 Strings. Rather the models' instance variables and method
01263 arguments are declared in terms of objects that abide by the
01264 #link("Validatable") interface. Since Validatable objects are
01265 typically implemented as subclasses of #link("Field"), they are 
01266 called Field Classes.</p>
01267 
01268 </page>
01269 
01270 <page id="PAGES" title="Defining Pages" requiresRole="none"> 
01271 <logic />
01272 
01273 <p>If the course is the skeleton and tasks are the meat, then
01274 pages are the skin. They wrap the gory internals into an tidy
01275 package that students use to learn about the course without 
01276 being exposed to the gory details.</p>
01277 
01278 <p>Those who are familiar with HTML markup should be aware that
01279 XHTML is a markup language exactly like HTML except that strict
01280 syntactic requirements are enforced. See the XHTML tutorial in
01281 
01282 the reference section for details.</p>
01283 
01284 <p>Right-click the Browsing link in the navigation bar to open
01285 a browser window on these files so that you continue reading
01286 
01287 about what they contain.</p>
01288 
01289 <dl>
01290 
01291 <dt>Step 1: Revise the home page</dt>
01292 
01293 <dd>The page with the id="OUTER" attribute defines the
01294 course's home page. Open a new browser window (continue
01295 reading these instructions in your current window) and type
01296 http://localhost/ale/root/YourCourseName in the location bar.
01297 You'll see the contents of the home element displayed there.
01298 Open YourCourseName/outerPages.xml in a text editor, make a
01299 few changes in the content, save them, and click your
01300 browser's reload button. Notice that the changes are
01301 
01302 reflected in your browser.</dd>
01303 
01304 <dd>You're might make errors when you do this so see the XML
01305 link (top row in the navigation bar) for how to deal with
01306 
01307 them.</dd>
01308 
01309 <dt>Step 2: Page Hierarchies versus Page Sequences</dt>
01310 
01311 <dd>JWAA provides two automated tools to support navigating
01312 from page to page without having to hand-code navigational
01313 links into each and every page. The navigation bar (the row
01314 of hotlinks at the top of this page) is used to support
01315 non-sequential navigation, which is usually what you want for
01316 ordinary pages. The other, a menu of Prev, Save, Next
01317 buttons, supports sequential navigation which is most
01318 appropriate when you want to constrain navigation to 
01319 sequential movement in structured tasks.</dd>
01320 
01321 <dd>The two kinds of navigational aids are governed by the
01322 child and logic elements at the beginning of each page. The
01323 child elements specify which other pages (defined elsewhere
01324 in this file) should be presented in the navigation bar as
01325 children of the current page. For example Tasks, Pages,
01326 LookAndFeel, and Java are children of the Reference Manual 
01327 page.</dd>
01328 
01329 <dd>Notice that each of these pages contain a &lt;logic/&gt;
01330 element. This is a command to turn 
01331 
01332 <b>off</b>the automatic logic that would otherwise add a row
01333 of Prev/Save/Next buttons to each page. These are
01334 inappropriate for hierarchical pages so the logic command
01335 turns them off. Try deleting one temporarily and observe the
01336 
01337 result.</dd>
01338 
01339 <dt>Step 3: Complete your Outer Pages</dt>
01340 
01341 <dd>Revise the home page and its child pages until they
01342 reflect what you want for your course. Be careful not to
01343 delete anything except what is specifically instructed. Many
01344 of these pages, particularly those near the end, are required
01345 to exist, and must have exactly the identifiers they have now
01346 to be recognized by the system. Always be ready to undo any
01347 
01348 changes that the system won't accept.</dd>
01349 
01350 </dl>
01351 
01352 <h2>References</h2>
01353 
01354 <dl>
01355 
01356 <dt>
01357 
01358 <a href="http://www.w3schools.com/xhtml/">XHTML
01359 
01360 Tutorial</a>
01361 
01362 </dt>
01363 
01364 <dd>An XHTML tutorial by W3Schools.</dd>
01365 
01366 </dl>
01367 
01368 </page>
01369 
01370 <page id="BROADCAST" name="Broadcast" requiresRole="none"
01371 title="Defining Simple Broadcast-style Pages"
01372 >
01373 
01374 <logic />
01375 
01376 <p>Task pages that simply broadcast information in a
01377 sequential, page by page manner, are built exactly as described 
01378 in the Pages section. There are only two differences:</p>
01379 
01380 <ol>
01381 
01382 <li>Omit the &lt;logic/&gt; element from the page. This tells
01383 JWAA to automatically present page flipper buttons
01384 (Prev/Save/Next) at the top and bottom of the page. JWAA will
01385 
01386 take it from there.</li>
01387 
01388 <li>Omit the page's requiresRole="none" attribute. This tells
01389 JWAA that to require that the student be logged in to access
01390 
01391 this page.</li>
01392 
01393 </ol>
01394 
01395 <p>Being logged in is important. That is how JWAA knows how to
01396 record that student's information in the database. Although
01397 simple broadcast pages don't collect much information, not as
01398 Interactive Pages do, the Talk to Me feature (the closing page
01399 of each task) certainly does. It would be disconcerting to
01400 reach the last page and be required to login to proceed.
01401 
01402 Catching this early is important.</p>
01403 
01404 </page>
01405 
01406 <page id="INTERACTIVE" name="Interactive Pages" requiresRole="none"
01407 title="Defining Interactive (Question/Answer) Pages"
01408 >
01409 
01410 <logic />
01411 
01412 <p>Interactive task pages are exactly the same as ordinary
01413 broadcast pages except that they contain special markup
01414 commands to position question fields within the page
01415 
01416 content.</p>
01417 
01418 <p>Each field has an id="..." attribute whose sole
01419 purpose is to uniquely identify the question within that page.
01420 They are used as keys to identify student answers in the
01421 database, so it is very important to not change them task after
01422 the database has accumulated student work. Since they do take
01423 up space in the database, I recommend using very short strings.
01424 I generally use q0, q1, q2, etc. To insert new questions, use
01425 q0a, q0b, etc. Anything convention will do so long as each
01426 question on a page is given a unique id that
01427 permanently identifies that question within that task and that
01428 page. I'll number these examples according to this convention.
01429 The text of the question is written as "mixed" content in the
01430 body of the question element as shown in the following
01431 
01432 examples.</p>
01433 
01434 <dl>
01435 
01436 <dt>essayField</dt>
01437 
01438 <dd>These are used to present essay-style questions. The
01439 question itself should be stated inside the body of the
01440 essayField, like this 
01441 
01442 <pre>
01443 &lt;essayField id="q1"&gt; What is your opinion of that?
01444 &lt;/essayField&gt;
01445 
01446 </pre>which looks like this when the student encounters this page: 
01447 
01448 <essayField id="q1">What is your opinion of
01449 
01450 that?</essayField></dd>
01451 
01452 <dt>textField, emailField, urlField, phoneField</dt>
01453 
01454 <dd>These look exactly the same in the student's browser. The
01455 only difference is that textFields are used for unstructured
01456 information; the other three provide validation logic to
01457 prevent the student from proceeding until the information is
01458 syntactically valid. For example: 
01459 
01460 <pre>
01461 &lt;emailField id="q2"&gt; Type your email address here.
01462 &lt;/emailField&gt;
01463 
01464 </pre>Looks like this in the browser: 
01465 
01466 <emailField id="q2">Type your email address
01467 
01468 here.</emailField></dd>
01469 
01470 <dt>choiceField</dt>
01471 
01472 <dd>These (along with menuFields) are used for picking a
01473 single correct answer from a list. They're used like this: 
01474 
01475 <pre>
01476  &lt;choiceField
01477 id="q3"&gt; A menu for choosing answers from a list of
01478 alternatives &lt;choice id="1"&gt;Yes&lt;/choice&gt;
01479 &lt;choice id="2"&gt;No&lt;/choice&gt; &lt;choice
01480 id="3"&gt;Maybe&lt;/choice&gt; &lt;/choiceField&gt; 
01481 
01482 </pre>Chose the correct answer from this list. 
01483 
01484 <choiceField id="q3">
01485 
01486 <choice id="1">Yes</choice>
01487 
01488 <choice id="2">No</choice>
01489 
01490 <choice id="3">Maybe</choice>
01491 
01492 </choiceField></dd>
01493 
01494 <dt>multiChoiceField</dt>
01495 
01496 <dd>These are used for multiple choice questions, like this: 
01497 
01498 <pre>
01499  &lt;multiChoiceField id="q3"&gt; Chose
01500 as many answers as apply: &lt;choice
01501 id="1"&gt;Yes&lt;/choice&gt;
01502 &lt;choice id="2"&gt;No&lt;/choice&gt; &lt;choice
01503 id="3"&gt;Maybe&lt;/choice&gt; &lt;/multiChoiceField&gt; 
01504 
01505 </pre>Chose as many answers as apply: 
01506 
01507 <multiChoiceField id="q3">
01508 
01509 <choice id="1">Yes</choice>
01510 
01511 <choice id="2">No</choice>
01512 
01513 <choice id="3">Maybe</choice>
01514 
01515 </multiChoiceField></dd>
01516 
01517 <dt>menuField</dt>
01518 
01519 <dd>These (or choiceFields) are for picking a single correct
01520 answer from a list. They're used like this: 
01521 
01522 <pre>
01523 &lt;menuField id="q3"&gt; A menu for choosing answers from
01524 a
01525 list of alternatives &lt;choice
01526 id="1"&gt;Yes&lt;/choice&gt;
01527 &lt;choice id="2"&gt;No&lt;/choice&gt; &lt;choice
01528 id="3"&gt;Maybe&lt;/choice&gt; &lt;/menuField&gt; 
01529 
01530 </pre>Chose the correct answer from this list. 
01531 
01532 <menuField id="q3">
01533 
01534 <choice id="1">Yes</choice>
01535 
01536 <choice id="2">No</choice>
01537 
01538 <choice id="3">Maybe</choice>
01539 
01540 </menuField></dd>
01541 
01542 <dt>submitField</dt>
01543 
01544 <dd>These are rarely used in ordinary task pages, but are
01545 indispensable when the usual page by page sequencing of the
01546 Prev/Save/Next buttons must be circumvented, in the login
01547 page for example. Browsers use the &lt;input type="submit
01548 ...&gt; form that this element emits to submit form contents
01549 to the server. JWAA automatically recognizes the
01550 Prev/Save/Next submit button names as commands to save all
01551 form contents on this page in the database and advance to the
01552 next page. Pages that contain submitFields should provide a
01553 &lt;logic;&gt; element that provides code to handle all this
01554 explicitly. This will be described in the next section,
01555 
01556 Structured Pages.</dd>
01557 
01558 </dl>
01559 
01560 <p>All of these features contain provisions for annotating
01561 student answers with comments the instructor may provide on
01562 each question during the review process. By default, instructor
01563 comments are presented in a contrasting (red) font immediately
01564 below the student's answer answer. A separate box is available
01565 
01566 for commenting on the task as a whole.</p>
01567 
01568 </page>
01569 
01570 <page id="PROGRAMMABLE" name="ProgrammablePages" requiresRole="none"
01571 title="Defining Programmable Pages" >
01572 
01573 <logic />
01574 
01575 <p>JWAA automatically allows pages to contain programmable
01576 statements written in the Velocity Template Language (see the
01577 
01578 References section for details).</p>
01579 
01580 <p>The key to understanding the JWAA programming model is to
01581 think of each page element is an active 
01582 
01583 <b>program</b>, not just static data as with HTML pages. JWAA
01584 provides programming logic automatically that is sufficient for
01585 most pages. Programmable Pages differ only in that the
01586 automatically provided logic is insufficient for some reason
01587 
01588 and must therefore be provided manually.</p>
01589 
01590 <p>The program that handles each page must deal with 
01591 
01592 <b>two</b>entirely different events: sending the page's
01593 contents to the browser and handling the information the
01594 browser sends back when the student clicks a submit button.
01595 Pages that need to override the logic that JWAA automatically
01596 provides must somehow deal with the difference, which is where
01597 
01598 the page's &lt;logic&gt; steps in.</p>
01599 
01600 <p>Actually there's a step that proceeds both of these, the
01601 step of parsing the XML file that actually defines each page
01602 and getting it ready for execution. We'll start with that step,
01603 then describe the remaining two halves of the process,
01604 beginning with the process of displaying a page followed by the
01605 
01606 process of responding to page submissions.</p>
01607 
01608 <h2>Loading/Parsing the XML</h2>
01609 
01610 <p>Recall that JWAA handles precisely two kinds of XML files:
01611 course.xml, which defines the structure and schedule of the
01612 course as a whole while pointing to additional xml files that
01613 
01614 each define any number of pages.</p>
01615 
01616 <p>XML is rather time-consuming to parse so pages are parsed
01617 only as they are needed. So JWAA examines the modification time
01618 on all XML files each time the contents of that file is
01619 accessed, and converts it to an in-memory representation at
01620 that time. Error conditions, such as XML errors, are noticed at
01621 this time and reported as the page is being loaded. From then
01622 on the XML format is irrelevant: the XML has been converted to
01623 
01624 a network of Java objects in the server's memory.</p>
01625 
01626 <p>The XHTML content of each &lt;page&gt; element is converted
01627 at load time to a simple string much like an ordinary XHTML
01628 
01629 file, with these differences:</p>
01630 
01631 <ol>
01632 
01633 <li>The velocity macros, \#header() and \#footer(), are
01634 automatically inserted at the beginning and end of each
01635 string of XHTML code. These macros will be interpreted when
01636 the page is displayed to add XHTML boilerplate, navigation
01637 bars, and the like to each page. These macros are described 
01638 in more detail on the LookAndFeel page.</li>
01639 
01640 <li>The content of each page is automatically rewritten to
01641 replace JWAA's special markup language for each question
01642 fields with the corresponding Velocity macro. For example,
01643 &lt;textField&gt; is replaced with a string that calls the
01644 
01645 velocity macro, \#textField(...).</li>
01646 
01647 <li>If the page provides a &lt;logic&gt; element, it is
01648 attached verbatim to the front of the text that the
01649 Controller will pass to velocity to interpret. If not a call
01650 to the velocity macro, #defaultLogic(), is placed there
01651 instead. Notice that the processing logic is at the very
01652 beginning of the information that velocity will interpret,
01653 ahead of the automatically inserted \#header() and \#footer()
01654 
01655 macro calls.</li>
01656 
01657 </ol>
01658 
01659 <p>If the XML parsing and validation is successful, the result
01660 is an instance of the Java class, Page, that has two internal
01661 variables set to ordinary Java strings, one containing logic
01662 for processing the page when it is submitted and the other for
01663 
01664 displaying the page itself.</p>
01665 
01666 <h2>Page Processing Overview</h2>
01667 
01668 <p>The controller's response to a page request involves the
01669 
01670 following steps:</p>
01671 
01672 <ol>
01673 
01674 <li>The servlet engine creates a Controller instance,
01675 initialized with a database connection (automatically
01676 retrieved from a connection pool and automatically returned
01677 there at the end), the httpServletRequest and
01678 httpServletResponse objects (which the controller needs to
01679 service the request), and the servlet that initiated the
01680 request. Then it calls the Controller's run method. The
01681 Controller is an instance of JWAA's GenericController class,
01682 
01683 which is described in more detail in the reference list.</li>
01684 
01685 <li>The Controller's run method decodes the the pathInfo
01686 string to determine which page to display. Each Course object
01687 manages an independent page lookup table which is why all
01688 
01689 page identifiers for a given course must be unique.</li>
01690 
01691 <li>The Controller retrieves the Person object of the
01692 currently logged in user from the servlet's session object
01693 and uses that to determine the user's Role (unknown because
01694 they're not logged in, guest, student, faculty,
01695 administrator, etc). The Role object is then used to
01696 determine if the requested page should be provided and
01697 
01698 redirects the request to a RequestDenied page if not.</li>
01699 
01700 <li>The Controller then builds the Context object that all
01701 Velocity code uses to communicate with the rest of the
01702 system. The context object contains a reference to the
01703 controller instance (named $self) a reference to the current
01704 user (named $person), and the values of any form
01705 
01706 parameters.</li>
01707 
01708 <li>Finally, the controller retrieves the text of the page
01709 from the Page object and passes that plus the context object
01710 
01711 to the velocity engine for processing.</li>
01712 
01713 <li>The velocity statements in the page's logic and body
01714 sections can make arbitrary changes to the text. For example,
01715 if statements may omit sections of the text, foreach
01716 statements can loop over arbitrarily complex data structures,
01717 macro calls can insert new text, and
01718 $self.gotoPage(someOtherPage) can break the control flow by
01719 
01720 sending control to some other page in midstream.</li>
01721 
01722 <li>The controller sends whatever string the velocity engine
01723 returns to the browser (and it may not return if the velocity
01724 code calls gotoPage()), thus determining what is actually
01725 
01726 displayed by the browser.</li>
01727 
01728 </ol>
01729 
01730 <p>In summary, the XML code for each page is converted at XML
01731 load time into a single long string that is filtered at run
01732 time by the velocity engine. The velocity engine interprets
01733 velocity statements embedded in this text, returning a new
01734 string that the Controller sends to the browser. We'll examine
01735 
01736 how the embedded velocity statements work next.</p>
01737 
01738 <h2>Velocity Statements</h2>
01739 
01740 <p>Velocity reserves two characters from the usual HTML
01741 character set, \# and \$, and uses them to trigger special
01742 processing. The \# character is used to designate a macro call
01743 or various other built-in velocity commands (\#if, \#foreach,
01744 \#set), and the \$ character is reserved to indicate a
01745 reference to something in the velocity context object. These
01746 are described in detail in the velocity reference material,
01747 along with how to escape these characters if you need them in
01748 
01749 your XHTML document.</p>
01750 
01751 <p>Open YourCourseName/innerPages.xml in a text editor and find
01752 the page that defines the course locker (id="locker").
01753 Notice the line a few lines down from the top that reads
01754 &lt;h2&gt;Welcome \$person&lt;h2&gt;. Velocity just replaces
01755 the string, \$person, with the string value of the context 
01756 variable, person.</p>
01757 
01758 <p>A few lines below that is a more complex example. This line
01759 generates the person's mailing address by accessing address
01760 information from inside that person's Person object; i.e.
01761 \$person.name, \$person.street, \$person.city, \$person.state, 
01762 \$person.zipcode, etc.</p>
01763 
01764 <p>Such expressions can be arbitrarily complex. A few lines
01765 below that is the expression,
01766 \$self.emitLink(\$self.findPage("edit"), "Edit") that calls the
01767 emitLink method of the "self" object (self is always the name
01768 of the Controller for the current page), the first argument
01769 being the result of calling the findPage() method of the
01770 controller with the id of the desired page. See the
01771 velocity reference manual for more information on how Velocity 
01772 code interacts with Java code.</p>
01773 
01774 <p>Finally, notice the \#foreach statements a bit further down.
01775 These build the locker by looping through each lesson in the
01776 course (\$self.course.lessons) and then looping through each 
01777 task in the lesson (\$lesson.tasks).</p>
01778 
01779 <h2>References</h2>
01780 
01781 <ul>
01782 
01783 <li>
01784 
01785 <a href="http://jakarta.apache.org/velocity/user-guide.html">
01786 
01787 Velocity User's Guide</a>is the most relevant.</li>
01788 
01789 <li>
01790 
01791 <a href="/jwaa">JWAA:</a>JWAA is baed on the Java Web
01792 Application Architecture, which provides form validation,
01793 page dispatching, and other support facilities that JWAA's
01794 
01795 Java code relies on.</li>
01796 
01797 <li>
01798 
01799 <a href="http://jakarta.apache.org/velocity">
01800 
01801 Other</a>Velocity references may be useful, particularly for
01802 
01803 configuring.</li>
01804 
01805 </ul>
01806 
01807 </page>
01808 
01809 <page id="DATABASE" name="Database" requiresRole="none"
01810 title="Accessing the Database" >
01811 
01812 <logic />
01813 
01814 <p>Under construction</p>
01815 
01816 </page>
01817 
01818 <page id="CONFIGURING" name="Configuring" requiresRole="none"
01819 title="Configuring JWAA" >
01820 
01821 <logic />
01822 
01823 <p>The simplest way to run JWAA involves only MySQL and a Jetty
01824 (or Tomcat) servlet engine. A better but more complex way is to
01825 install the servlet engine so that static data (graphics and
01826 static web pages) are served by a web server such as Apache,
01827 which being written in C, is faster that the servlet engine's
01828 Java code. We will assume the latter because the former is
01829 
01830 adequately covered by each servlet engine's instructions.</p>
01831 
01832 <p>This means that each web page request is handled
01833 (dispatched) by four different pieces of code: the web server
01834 (we'll call that Apache; others are similar), the servlet
01835 engine (we'll call that Jetty; others are similar), the JWAA
01836 servlet, and the JWAA page controller/dispatcher. Each of these
01837 must be configured to know which requests to dispatch
01838 elsewhere, and configuring them all properly is by far the most
01839 troublesome part of the installation process. This overview may
01840 
01841 help understand what's involved:</p>
01842 
01843 <ol>
01844 
01845 <li>The web server (apache) breaks the URL down into parts
01846 and routes the request according to what it finds there.
01847 Somewhat oversimplified, the URL
01848 virtualschool.edu:80/ale/root/course/page?foo=bar, is broken
01849 down into virtualschool.edu:80 (the hostname and port
01850 number), the context name (/ale), the context mapping
01851 (/root), the pathInfo (/ale/root/ale), and the request
01852 arguments (?foo=bar). The context name and mapping strings
01853 determine which requests will be handled by directly to
01854 apache or routed to one of the several servlet engines that
01855 might be available on that server. This is described 
01856 under #link("CONFIGURING).</li>
01857 
01858 <li>If the context name and mapping correspond to a servlet
01859 engine apache knows about, sends the pathInfo string and
01860 arguments there, else it will process the request 
01861 internally.</li>
01862 
01863 <li>The servlet engine strips the context name and mapping
01864 (/ale/root) from the request, and attempts to find a course
01865 whose id matches the first part of whats left
01866 (course). If it succeeds, it uses the remainder to search for
01867 one of that course's pages and proceeds as described below.
01868 Otherwise it issues a "can't find" response and gives 
01869 up.</li>
01870 
01871 <li>We now know the course and a specific page within that
01872 course. The servlet engine builds an object for controlling
01873 processing of that page (the "Controller"). It temporarily
01874 allocates a database connection from a pool and initializes
01875 that objects variables with the database connection, the
01876 servlet, the httpRequest and httpResponse that it received
01877 from the servlet engine. These are mainly useful to the
01878 Controller, but it is occasionally useful for page logic to
01879 know that this information is available there. The controller
01880 also initializes one of its variables with the page being
01881 
01882 served.</li>
01883 
01884 <li>The controller then constructs the context object with
01885 two variables via which velocity code will communicate with
01886 the rest of the system. The context variable, $self, contains
01887 a reference to the controller object. $person contains a
01888 reference to the object that represents the person that is
01889 
01890 currently logged in (if any).</li>
01891 
01892 </ol>
01893 
01894 </page>
01895 
01896 <page id="CONCEPTS" name="Concepts" requiresRole="none"
01897   title="Conceptual Overview" >
01898 
01899 <logic />
01900 
01901 <p>JWAA supports two ways to package the logic (control) and 
01902 presentation (HTML) parts of a web page.</p>
01903 
01904 <dl>
01905 
01906 <dt>Integration of concerns for stiffness</dt>
01907 
01908 <dd>A stiff application is harder to break (or to get wrong in
01909 the first place) because all interfaces are strongly
01910 type-checked by the compiler. Integration of concerns handles
01911 presentation in separate (typically private) methods of the
01912 same file that specifies the logic. This style is best when a
01913 single person is responsible for the page because everything is
01914 specified in the same file. This reduces the surface area to
01915 the barest minimum, and since Java can rigorously type check
01916 all interfaces, there is less chance for things to break as the
01917 application evolves. The disadvantage is that the application
01918 must be recompiled and reloaded to see changes in the
01919 
01920 browser.</dd>
01921 
01922 <dt>Separation of concerns for flexibility</dt>
01923 
01924 <dd>A flexible application is easier to change on the fly
01925 because some changes don't require recompiling and restarting
01926 the server. This philosophy is best in large shops in which
01927 programmers are responsible for the application's logic and
01928 user interface designers are responsible for the html. Although
01929 this doubles the number of files, it lets HTML designers change
01930 
01931 the HTML without recompiling and restarting the server.</dd>
01932 
01933 </dl>
01934 
01935 <p>The demo applications emphasize the integration of concerns
01936 philosophy because the reduced surface area and type checking
01937 advantages were compelling in my shop. This page provides
01938 everything you need to use JWAA according to the integration of
01939 concerns approach. The separation of concerns approach is
01940 described under #link("INTERACTION" "Integrating JWAA with 
01941 Velocity"), but relies on concepts described on this page.</p>
01942 
01943 <ul>
01944 
01945 <li>
01946 
01947 <a href="#concepts">Key Concepts</a>
01948 
01949 </li>
01950 
01951 <li>
01952 
01953 <a href="#bricks">Page classes as the bricks</a>
01954 
01955 </li>
01956 
01957 <li>
01958 
01959 <a href="#mortar">Support classes as the mortar</a>.</li>
01960 
01961 </ul>
01962 
01963 <p>Each section provides #link("SOURCE") hotlinks to
01964 the source code of the #link("DemoHomePage" "online demo") to
01965 
01966 illustrate each point.</p>
01967 
01968 <a name="concepts"
01969 
01970  id="concepts"></a>
01971 
01972 <h2>Key Concepts</h2>
01973 
01974 <p>At the highest level, a web application is a number of 
01975 <a href="#bricks">page objects</a>, one for each page in your
01976 application. A user's browser session begins at a "home" page
01977 (specified by a configuration parameter) and advances to other
01978 pages when the user clicks hotlinks or submit buttons. These page
01979 objects are the bricks that make up the application. They are
01980 joined by support classes to form the application as a whole.</p>
01981 
01982 <h3>Pages are Objects not Files</h3>
01983 
01984 <p>Most web application environments view web pages as files that
01985 reference each other via &lt;a href&gt; and &lt;form&gt; commands
01986 hard coded into each file. However web applications are not
01987 browsers and need not abide by this convention. HTML is to a web 
01988 application as ASCII is to Hello World.</p>
01989 
01990 <p>So JWAA applications are composed of dynamic objects, called
01991 pages, rather than static HTML files. When a page (the ovals in
01992 the figure) references another page (the arrows in the figure),
01993 it specifies the object by name rather than by naming the file
01994 that contains its HTML. This simple change makes all the
01995 difference. Invalid object names are automatically detected and
01996 reported when the application is compiled, rather than going 
01997 undetected until the link is clicked by some user.</p>
01998 
01999 <h3>Model-View-Controller Paradigm + Fields</h3>
02000 
02001 <p>
02002 
02003 <img align="right" src="/jwaa/pix/mvc.gif" alt="" width="117" height="114" />
02004 Trygvee Reenskaug introduced the
02005  Model-View-Controller (MVC) paradigm within Smalltalk during
02006  a summer visit to Xerox Parc. It spread from there to other
02007 
02008  languages.</p>
02009 
02010 <dl>
02011 
02012 <dt>Models</dt>
02013 
02014 <dd>Models are objects that manage the application's persistent
02015 state, usually by cooperating with a database of some sort. For
02016 example, #link("AccountAbstraction") maintains account-holder
02017 information (name, address, phone number, etc) for the demo
02018 application by loading the account-holder's information from
02019 the database when the user logs in, saving it thereafter as
02020 
02021 necessary.</dd>
02022 
02023 <dt>Views</dt>
02024 
02025 <dd>Views present the model to the user by sending HTML to a
02026 browser in our case. In the integration of concerns approach,
02027 views are private methods of the class that implements each
02028 page and are called as needed by the controller method. See
02029 #link("INTERACTION" "Integrating JWAA with Velocity") for the 
02030 separation of concerns approach.</dd>
02031 
02032 <dt>Controllers</dt>
02033 
02034 <dd>Controllers provide the logic for controlling the
02035 interaction as a whole. In JWAA, each web page corresponds to a
02036 subclass of Page. The dispatcher allocates instances of this
02037 class to service each request. This instance's run method is
02038 
02039 the page's controller.</dd>
02040 
02041 <dt>Fields</dt>
02042 
02043 <dd>In the original MVC paradigm, models were built from
02044 primitive data types such as Strings. JWAA introduces
02045 higher-level container objects, called Fields, for the same
02046 effect. Fields also encapsulate the logic for validating user
02047 inputs (does the field contain acceptable values?) and for
02048 communicating errors to the user via the user interface. Fields
02049 are a uniquely powerful unit of reuse that simplifies user
02050 
02051 interface design and implementation.</dd>
02052 
02053 </dl>
02054 
02055 <a name="bricks"
02056 
02057  id="bricks"></a>
02058 
02059 <h2>Pages are the Bricks</h2>
02060 
02061 <p>
02062 
02063 <img align="right"
02064  src="/jwaa/pix/states.gif"
02065  width="167"
02066  height="177" />The six pages that make up the demo
02067  application are shown in this figure as ovals with the
02068  hotlinks that connect them as arrows. Each page is a
02069  subclass of the abstract page controller class,
02070  #link("GenericPage"), and they reference other pages by
02071  calling the 
02072 
02073 <code>link, form, redirect or forward</code>methods that they
02074 inherit from JwaaPage with the target page as an argument. Since
02075 Java checks all object references at compile time, if the target
02076 page doesn't exist, the error will be reported at compile
02077 
02078 time.</p>
02079 
02080 <p>JwaaPage defines several abstract methods such that each
02081 subclass must implement the following methods. In addition, pages
02082 can and often do define additional methods to support the
02083 
02084 mandatory ones listed here</p>
02085 
02086 <dl>
02087 
02088 <dt>public PageName()</dt>
02089 
02090 <dd>Each page class must be public and must define a public
02091 no-args constructor. The application's servlet uses the
02092 constructor to create an instance of your Page class to handle
02093 the request. It then calls the instance's public final void
02094 init() populate the Page's four instance variables with the
02095 request-specific information that the page needs to handle the
02096 request: 
02097 
02098 <ol>
02099 
02100 <li>The servlet that dispatched the request</li>
02101 
02102 <li>The HttpServletRequest instance</li>
02103 
02104 <li>The HttpServletResponse instance</li>
02105 
02106 </ol></dd>
02107 
02108 <dt>public final static MetaPage meta = new MetaPage(...)</dt>
02109 
02110 <dd>Every page defines a global handle, by convention named
02111 "meta", that other pages will use to reference the page in
02112 hotlinks, form references, redirects and forward commands. This
02113 structure would be the page's meta class in other
02114 object-oriented languages. Think of it as the page's factory
02115 object because it provides a way to create instances of the
02116 page and maintains information that must be known even when no
02117 
02118 instances of the page are available.</dd>
02119 
02120 <dt>public void run() throws Fault</dt>
02121 
02122 <dd>The page's run method sends the page's html to the browser
02123 and processes form parameters that the browser returns as its
02124 
02125 response.</dd>
02126 
02127 </dl>
02128 
02129 <h3>MetaPage</h3>
02130 
02131 <p>#link("LOGOUT") is the simplest page in the demo, but it
02132 contains all of the elements that are required of all pages.
02133 Notice the MetaPage constructor, which takes the following
02134 
02135 parameters:</p>
02136 
02137 <ul>
02138 
02139 <li>The first is the page's class. The servlet uses this to
02140 construct an instance of the page to service each page
02141 
02142 request.</li>
02143 
02144 <li>The second is the page's dispatch key. This is a unique
02145 name for each page. The form and link commands will use this to
02146 
02147 compose the URL that browsers use to refer to the page.</li>
02148 
02149 <li>The third is a brief string (ideally one word) that the
02150 link method will use as the anchor text in navigation bars and
02151 hotlinks if an explicit anchor is not provided as an
02152 
02153 argument.</li>
02154 
02155 <li>The fourth is a longer string that the link method will use
02156 as the default title string in navigation bars, page titles and
02157 hotlinks, if an explicit title is not provided as an
02158 
02159 argument.</li>
02160 
02161 <li>The fifth determines who can access the page. It must be an
02162 an object that abides by JWAA's #link("GenericRole") interface,
02163 such as #link("Role"). This class defines three capability
02164 levels, Visitor for those who have not registered and logged
02165 in, Registered for those who have, and Admin for specially
02166 authorized administrative accounts. The capability parameter
02167 determines whether the navigation bar will offer links to that
02168 page and whether the servlet will dispatch requests for that
02169 visitor. The servlet will redirect unauthorized requests to the
02170 
02171 page specified by the servlet's getRefusePage() method.</li>
02172 
02173 <li>The sixth defines this page's contribution to the page
02174 hierarchy as an MetaPage[] array of sub-pages. This parameter
02175 is optional because the notion of "hierarchy" is not intrinsic,
02176 but is often added to help users understand and navigate the
02177 
02178 site.</li>
02179 
02180 </ul>
02181 
02182 <p>The Meta structures must abide by the following rules, which
02183 are checked when the servlet engine is starting up. Violations
02184 are not fatal but are logged to the log file to warn you to fix
02185 
02186 them.</p>
02187 
02188 <ul>
02189 
02190 <li>Each page's dispatch key must be globally unique since
02191 
02192 external browsers use this to uniquely identify the page.</li>
02193 
02194 <li>Each page may not be a child of multiple parents. The page
02195 hierarchy is a tree, not a graph, and is managed as a doubly
02196 
02197 linked parent-child tree.</li>
02198 
02199 <li>Pages that are in the hierarchy will be automatically
02200 loaded when the servlet engine is starting up. The trigger is
02201 that #link("JwaaServlet") is imported (by the
02202 #link("GenericServlet") interface), which references the root
02203 of the page hierarchy #link("JwaaPage"). If a page does not
02204 appear in the hierarchy, the class loader won't load it. Pages
02205 that aren't in the hierarchy must be explicitly referenced
02206 elsewhere or they will not be registered in the dispatch table
02207 
02208 so dispatches will fail.</li>
02209 
02210 </ul>
02211 
02212 <h3>Run Method</h3>
02213 
02214 <p>A page's meta object specifies its static nature and its
02215 dynamic nature is determined by its run method.
02216 #link("LOGOUT") has the simplest run method, because it sends
02217 nothing at all to the browser. It simply logs the visitor out by
02218 calling setViewer(null) to erase the viewer's account information
02219 
02220 from the session and forwards control elsewhere.</p>
02221 
02222 <p>#link("INNER") is more typical. Its run method uses the
02223 send method (that all pages inherit from #link("JwaaPage")) to
02224 send a long batch of HTML text to the browser. The problem here
02225 is how to write the HTML, since Java's String syntax is oriented
02226 
02227 to short uni-line $fixme()</p>
02228 
02229 <p>A final refinement is to eliminate the leading and trailing
02230 html that define the site's look and feel by moving it into
02231 static methods of an application-specific JwaaPage.template
02232 class. With these two refinements, the run method of a simple
02233 
02234 "Hello World" application would be written like this:</p>
02235 
02236 <pre>
02237  send({{&lt;h1&gt;Hello world&lt;/h1&gt;}});
02238 
02239 
02240 </pre>
02241 
02242 <p>See #link("INNER") and #link("LookAndFeelDox") for fully
02243 
02244 elaborated examples.</p>
02245 
02246 <h3>Cross-page References</h3>
02247 
02248 <p>Although you can always write &lt;a href&gt; and &lt;form&gt;
02249 commands as hard-coded strings within the html, JWAA provides a
02250 new technique that does exactly the same thing but allows linkage
02251 
02252 errors to be detected at compile-time.</p>
02253 
02254 <p>All pages inherit link, form, forward, and redirect methods
02255 from #link("JwaaPage") which all require a MetaPage instance as
02256 their first argument. The link and form commands return the
02257 appropriate &lt;a href&gt; or &lt;form&gt; command as a String
02258 while the forward and redirect commands actually redirect control
02259 to the specified page. Java's strong type-checking automatically
02260 determines if the MetaPage structure is not accessible. For
02261 example, the Hello World example might be extended like
02262 
02263 this:</p>send({{ &lt;h1&gt;Hello world&lt;/h1&gt;
02264 {{link(SomeOtherPage.meta, "Click here")}} for more information.
02265 }}); 
02266 
02267 <p>See #link("INNER") for a longer example.</p>
02268 
02269 <h3>Interactive Pages</h3>
02270 
02271 <p>Non interactive pages (pages that don't process form
02272 parameters) typically handle everything in their run() method.
02273 Interactive pages, such as the #link("EDITOR") which supports
02274 editing of an account's registration information, confine the run
02275 method to specifying the page's logic and handle presentation
02276 elsewhere, either in a private method of the same class
02277 (integration of concerns) or in a separate file (separation of 
02278 concerns) as described #link("INTERACTION" "here").</p>
02279 
02280 <p>The #link("PORTAL"), #link("EDITOR"),
02281 #link("RegisterPage"), and #link("AdminPage") pages demonstrate
02282 the integration of concerns approach. Notice that the control
02283 
02284 logic invariably follows this simple pattern:</p>
02285 
02286 <ol>
02287 
02288 <li>Gather the form parameters by using getField to build
02289 
02290 #link("Field") instances.</li>
02291 
02292 <li>If all field instances are valid, process them.</li>
02293 
02294 <li>Otherwise prompt the user to fix them.</li>
02295 
02296 </ol>
02297 
02298 <p>The else clause automatically handles the initial presentation
02299 of the page, when all fields contain default values (typically
02300 empty ones), and subsequent presentations in which one or more
02301 fields contain errors that the user must fix to proceed. Notice
02302 that the run method boils down to a few initialization statements
02303 
02304 and an if statement. It can't get any simpler than that!</p>
02305 
02306 <h2>
02307 
02308 <a name="mortar"
02309 
02310  id="mortar">Support Classes are the Mortar</a>
02311 
02312 </h2>
02313 
02314 <p>In addition to the Page classes described above, every
02315 application needs a number of supporting classes to glue the
02316 separate pages into a functioning application. The remaining
02317 sections describe the supporting classes for the demo
02318 application: #link("LookAndFeelDox"), #link("JwaaServlet") and
02319 
02320 #link("Role").</p>
02321 
02322 <h3>JwaaPage.template Class</h3>
02323 
02324 <p>How a page emits its html is entirely up to the page's run
02325 method. But the recommended style is to move common look and feel
02326 issues into static methods of an application-specific
02327 JwaaPage.template class so that look and feel can be shared by
02328 
02329 all pages.</p>
02330 
02331 <p>The demonstration application's JwaaPage.template class is
02332 #link("LookAndFeelDox"). It uses CSS (Cascading Style Sheets)
02333 commands extensively. It also uses the MetaPage page hierarchy to
02334 add navigational menus at the top of each page. To build your own
02335 application, copy this class to an application-specific new name
02336 
02337 and modify to suit.</p>
02338 
02339 <h3>Servlet Class</h3>
02340 
02341 <p>Each web application defines an instance of
02342 #link("GenericServlet") to handle all requests by that
02343 application. Every application's servlet simply overrides the
02344 following methods. For example, see the demo's servlet class,
02345 
02346 #link("JwaaServlet").</p>
02347 
02348 <dl>
02349 
02350 <dt>public void init(ServletConfig servletConfig) throws
02351 
02352 ServletException</dt>
02353 
02354 <dd>All servlets override this method. Nothing special
02355 
02356 here.</dd>
02357 
02358 <dt>public final MetaPage getDefaultPage() { return
02359 
02360 WelcomePagePage.meta; }</dt>
02361 
02362 <dd>The page to be displayed if the servlet can't locate the
02363 page named in the browser request. This might happen if the
02364 user composes the request manually. Another possibility is that
02365 the page is not registered in the page dispatch table because
02366 it was not loaded by the class loader, as might happen if the
02367 page is not listed as a child of some other page in the page
02368 
02369 hierarchy.</dd>
02370 
02371 <dt>public final MetaPage getRefusePage() { return
02372 
02373 EntryPage.meta; }</dt>
02374 
02375 <dd>This is the page the servlet should dispatch to if the user
02376 requests a page for which they don't have the required
02377 capability. The demo handles this by silently forwarding them
02378 to the default entrance page (EntryPage). Other applications
02379 
02380 might forward to an explicit "permission denied" page.</dd>
02381 
02382 <dt>private final static MetaPage rootPage = JwaaRoot.meta;
02383 
02384 <br />public final MetaPage getRootPage() { return rootPage;
02385 
02386 }</dt>
02387 
02388 <dd>This method exists as a reminder that the servlet must
02389 reference the root of the page hierarchy so that the class
02390 loader will automatically load all pages in the hierarchy as
02391 the servlet starts up. If you have pages that aren't listed in
02392 the hierarchy, the servlet will be unable to dispatch them
02393 since their name won't be defined in its dispatch table.
02394 Eliminating the static variable above will not work; the root
02395 
02396 reference must occur at load time, not run time.</dd>
02397 
02398 <dt>public final DBPool getConnectionPool() { return pool;
02399 
02400 }</dt>
02401 
02402 <dd>This method returns the connection pool that the servlet
02403 should use to obtain database connections. The servlet
02404 automatically checks out a new connection from the pool for
02405 each dispatch, stores it as an instance variable in the page
02406 instance that will handle that dispatch, calls the instance's
02407 run() method, and automatically returns the connection to the
02408 
02409 pool when the run method returns.</dd>
02410 
02411 </dl>
02412 
02413 <h3>Capability Class</h3>
02414 
02415 <p>Since each application is likely to have unique requirements
02416 for determining who is allowed to access each page, JWAA
02417 capability system is extendible. The demonstration uses the
02418 simple three-level capability system defined in
02419 
02420 #link("Role").</p>
02421 
02422 <ul>
02423 
02424 <li>Visitor: for visitors whose identity is unknown because
02425 
02426 they've not registered and logged on.</li>
02427 
02428 <li>Registered: for visitors that have registered and logged
02429 
02430 on.</li>
02431 
02432 <li>Admin: a privileged account permitted to access
02433 
02434 administrative functions.</li>
02435 
02436 </ul>
02437 
02438 <p>In practice, the pages of a web application can be thought of
02439 as an outer lobby with the pages that should be accessible to
02440 everyone, and an inner sanctum with the pages that can only be
02441 accessed by those who have logged in. In the demo application,
02442 the inner sanctum consists of only three pages.
02443 #link("EDITOR") supports editing an account's registration
02444 information, #link("LOGOUT") supports logging out, and 
02445 #link("AdminPage"), is for administrative users.</p>
02446 
02447 <p>#link("PORTAL") is the doorway between the two regions.
02448 Until a visitor has registered and logged in, their account is
02449 null, signifying a special account reserved for unknown
02450 
02451 visitors.</p>
02452 
02453 <p>When the visitor identifies themselves to the login procedure
02454 (in the demo, by providing a previously registered email address,
02455 but more typically, by also providing the corresponding
02456 password), #link("PORTAL")loads the account's persistent
02457 information from the database as an instance of
02458 #link("AccountAbstraction") and stores a reference to this
02459 instance as a specially named session attribute. The session
02460 attribute persists as the user browses from page to page. It will
02461 only be erased by the LogoutPage, if the session times out, or if
02462 
02463 the server itself is restarted.</p>
02464 
02465 <p>Notice that #link("DemoAccount") implements the
02466 #link("AccountAbstraction") interface, which requires that all
02467 subclasses provide a isCapable(Capability c) method. The system
02468 calls this method to determine if a given account has the
02469 
02470 capability level in the method's argument.</p>
02471 
02472 <ul>
02473 
02474 <li>#link("LookAndFeelDox") will refuse to show links to that
02475 page in the navigation bar. This lets the page hierarchy, which
02476 governs the navigation bar, be specified without concern for
02477 account capabilities, since the navigation bar will offer only
02478 
02479 the links that the viewer is allowed to access.</li>
02480 
02481 <li>#link("GenericServlet") redirects requests from viewers
02482 with inadequate capabilities by redirecting them to the page
02483 specified by the servlet's getRefusePage() method. This is a
02484 second line of defense to ensure that unauthorized visitors
02485 
02486 can't bypass the protection by editing requests by hand.</li>
02487 
02488 </ul>
02489 
02490 <h3>Model Classes</h3>
02491 
02492 <p>In addition to an application's user-interface components
02493 (Views/Controllers) every application needs one or more Model
02494 classes to manage the application's data as persistent
02495 
02496 objects.</p>
02497 
02498 <p>The demo's model class is #link("DemoAccount"), which
02499 demonstrates the principle that 
02500 
02501 <i>Fields are to models as atoms are to molecules.</i>This means
02502 that models are not defined in terms of basic Java types such as
02503 Strings. Rather the models' instance variables and method
02504 arguments are uniformly declared in terms of objects that abide
02505 by the #link("Validatable") interface. Since these objects are
02506 typically implemented as subclasses of #link("Field"), these
02507 
02508 higher-level classes are called Field Classes.</p>
02509 
02510 <h3>Field Classes</h3>
02511 
02512 <p>Models are defined in terms of #link("Field")s, not Strings,
02513 
02514 because:</p>
02515 
02516 <ul>
02517 
02518 <li>Fields are uniquely reusable units of reuse because they
02519 are so concrete, immediately tangible and visible to everyone
02520 
02521 in the application's user interface</li>
02522 
02523 <li>Fields encapsulate the rules for determining whether
02524 user-provided inputs comply with the syntactical validity
02525 constraints of that field. Centralizing this logic in reusable
02526 field classes makes applications much simpler than the
02527 traditional approach of distributing this logic throughout the
02528 
02529 application.</li>
02530 
02531 <li>Each field can enforce DBMS-specific constraints, such as
02532 
02533 the maximum length of fixed-length fields.</li>
02534 
02535 </ul>
02536 
02537 <p>The JWAA package provides a #link("Validatable") interface,
02538 which defines the protocol that all field implementations adhere
02539 to. It also provides an abstract #link("Field") that defines
02540 
02541 methods for subclasses to inherit.</p>
02542 
02543 <p>The full list of Field classes is available in
02544 
02545 #link("SOURCE").</p>
02546 
02547 <p>Typically you'd define your database to use the field lengths
02548 specified by these classes. If not you should edit the classes to
02549 match. Only U.S. address and phone numbers are provided.
02550 International contributions would be gratefully accepted.</p>
02551 
02552 </page>
02553 
02554 
02555 </pages>