390 likes | 471 Views
Recommendations for Java-Based Web Application Architectures Session W16 – 04:30–05:50pm – 04/04/2001 Craig McClanahan (Sun Microsystems) craig.mcclanahan@eng.sun.com. Introduction. What are the high level issues we need to address? What technology choices do we have?
E N D
Recommendations for Java-Based Web Application Architectures Session W16 – 04:30–05:50pm – 04/04/2001 Craig McClanahan (Sun Microsystems) craig.mcclanahan@eng.sun.com
Introduction • What are the high level issues we need to address? • What technology choices do we have? • What high level architectural approaches should we consider?
The Issues • The HTTP Communications Protocol • Request / Response oriented • Stateless • HTML and XML-based Presentation Languages • "Standard HTML" ... not! • Dynamic HTML and JavaScript • Wireless devices vary in capabilities and language support
The Issues • Disparate Skill Sets Required • Presentation Layer – User interface design, visual appearance, optimization • Application Layer – Functional business logic to perform required actions • Persistence Layer – Databases, directory servers, messaging, transactions, Enterprise JavaBeans™ (EJB) • Application Deployment – Networks, firewalls, public key infrastructures, load balancing, failover
Application Data and Functionality The Model Layer
Sources of Application Data • Relational Databases • Accessed directly via JDBC driver • Accessed indirectly via JavaBeans abstraction layer • Accessed indirectly via Enterprise JavaBeans • Legacy Data Files • Accessed directly via File I/O • Accessed indirectly via JavaBeans abstraction layer • Accessed indirectly via HTTP
Sources of Application Data • Legacy Applications • Accessed via HTTP, RMI, and CORBA APIs • Accessed via proprietary application APIs • Accessed via the Java2 Enterprise Edition™ (J2EE) Connector Architecture
Internal Data Representation • JDBC Rows and Columns • Attractive for very simple applications • Attractive when using JavaServer Pages™ (JSP) custom tags that support direct SQL commands • Online/offline access via the javax.sql.RowSet abstraction • Ties your application to the underlying database representation
Internal Data Representation • JavaBeans Abstraction Layer • Insulates application logic from underlying storage formats • Enables transparent caching of frequently used beans • Integrates well with presentation (view) layer components • Can be tedious to create without development tool support • Should not be aware of use in a web application setting
Internal Data Representation • Enterprise JavaBeans (EJB) • Application data generally represented as Entity Beans • Supports scalable, reliable, transaction-aware applications • Can be used directly or represented by a JavaBeans abstraction layer • May cause performance issues if used directly in n-tier deployment environments
Internal Data Representation • Extensible Markup Language (XML) • Useful for dynamically generated data from XML-aware data sources • Useful for integrating data from external applications systems • Can be subjected to various filtering and transformation activities • Standard parsers and transformation engines available
Application Functionality • Embedded Logic in Servlets and JSP Pages • Sometimes useful in simple applications • Tends to cause presentation logic and business logic to be mixed • Tends to require developers with one skill set to do all updates • JavaBeans Processing Classes • Encapsulates individual processing functions • Insulates presentation layer from internal data representation
Application Functionality • Enterprise JavaBeans (EJB) • Application functionality generally represented as Session Beans • Multi-tier deployment transparently supported • Extensible Stylesheet Processing (XSP) • Especially useful in content publishing applications • XSP stylesheets can integrate processing instructions to initiate functional manipulations
Content Generation Options The View Layer
Content Generation Options • Servlets using writer.println()statements • Servlets using class libraries for HTML rendering • Templates with parametric replacement for dynamic content • JavaServer Pages™ (JSP) • JavaServer Pages with Custom Tag Libraries
The Desired Output <table> <tr> <th>Account#</th> <th>Customer Name</th> </tr> <tr> <td>123456</td> <td>Acme Motors</td> </tr> </table>
Servlet With Println() Calls • The method almost everyone tries on their first application • The full power of Java is available to create dynamic output • Presentation logic is buried in the midst of the Java code • Requires a Java developer for user interface remodels • http://java.sun.com/products/servlet/
Servlet With Println() Calls PrintWriter writer = response.getWriter(); ... render top of page and table heading ... Customer custs[] = getCustomers(); for (int i = 0; i < custs.length; i++) { writer.println("<tr>"); writer.println(" <td>" + custs[i].getId() + "</td>"); writer.println(" <td>" + custs[i].getName() + "</td>"); } ... render table footing and bottom of page ...
Use HTML Rendering Class Library • You can use an HTML (and XML) rendering library like the Element Construction Set (ECS) • Avoids some typographical mistakes (like unbalanced tags) • Other advantages and disadvantages similar to using println() statements directly • http://java.apache.org/ecs
Use HTML Rendering Class Library PrintWriter writer = response.getWriter(); ... render top of page and table heading ... Customer custs[] = getCustomers(); Html html = new Html(); for (int i = 0; i < custs.length; i++) { html.addElement((new TR()) .addElement(new TD(custs[i].getId())) .addElement(new TD(custs[i].getName()))); } writer.println(html.toString()); ... render table footing and bottom of page ...
Templates With Parametric Replacement • Do not waste time building and maintaining your own template language • Use existing packages like WebMacro or Velocity • Servlet developer makes objects available: WebMacro wm = new WebMacro(); Context ctx = wm.getContext(); Customer custs[] = getCustomers(); ctx.put("results", custs);
Templates With Parametric Replacement • The template writer uses these objects to create the presentation #foreach $result in $results { <tr> <td>$result.ID</td> <td>$result.Name</td> </tr> } • http://jakarta.apache.org/velocity
JavaServer Pages (JSP) • You can use JavaServer Pages to render dynamic output in a very similar manner • Same advantages and disadvantages of mixing business and presentation logic • http://java.sun.com/products/jsp/
JavaServer Pages (JSP) <table> <tr> <th>Account#</th> <th>Customer Name</th> </tr> <% Customer custs[] = SomeClass.getCustomers(); %> <% for (int i = 0; i < custs.length; i++) { %> <tr> <td><%= custs[i].getId() %></td> <td><%= custs[i].getName() %></td> </tr> <% } %> </table>
JSP With Custom Tags • JSP includes standard tags for JavaBean and property access • Built-in facilities for defining custom tag libraries • Ongoing effort to create a Standard Tag Library for commonly required features (JSR-052) • Example uses an iteration tag from the Struts Framework to loop over a collection
JSP With Custom Tags <jsp:useBean id="custs" scope="request" type="java.util.Collection"/> <logic:iterate id="cust" name="custs"> <tr> <td><jsp:getProperty name="cust" property="id"/></td> <td><jsp:getProperty name="cust" property="name"/> </td> </tr> </logic:iterate> • http://jakarta.apache.org/struts/
XSLT Transformations • Assume XML data content in this format: <customers> <customer id="123456" name="Acme Motors"/> <customer id="654321" name="Foobar Transport"/> </customer> • Can be transformed by an XML stylesheet <xsl-template match="customers/customer"> <tr> <td><xsl:value-of select="@id"/></td> <td><xsl:value-of select="@name"/></td> </tr> </xsl-template> • http://xml.apache.org/cocoon/
Application Logic Control The Controller Layer
The "Model 1" Approach • A Model 1 design is characterized by: • Form submits go to the servlet or JSP page that created that form • Intermixed presentation and business logic in the servlet or JSP page • Commonly used when developers of only one skill set (Java programming or web page development) are available • These designs work acceptably for simple applications, but are not easy to maintain or enhance
The "Model 2" Approach • A Model 2 design is characterized by: • Form submits go to a common controller component • Controller component dispatches to appropriate business logic based on the request's characteristics • Business logic interacts with the model layer to perform the required processing • Controller component forwards to appropriate presentation logic to construct the response
Struts Controller Features • Mapping of requests to action classes by logical name • Instantiate action classes on first use • Form bean support – property population and input validation • Configurable support for internationalization, application message resources, HTTP no-cache headers, and debugging messages
Struts Custom Tag Features • Extensive input form support • Interacts with form bean support in the controller servlet • Support tags for extracting bean properties, collections, iteration, and conditional logic • Template tags for organizing page layout • Interoperates with standard JSP tags and other tag libraries
(1) Separation of Concerns • Future changes are a given – count on having to deal with them • Use appropriate technologies for each component • Limit the technical skillsets required to develop and maintain each component • Design to minimize cross-component impacts when changes occur
(2) The Model Layer • Simple Applications – Access relational databases via JDBC connections • Complex Applications – Use entity Enterprise JavaBeans for scalable performance, security, and transactions • Specialized Data Access – Leverage Java APIs for access to remote servers (RMI, CORBA, XML data formats) and applications • Insulation – Use adapter JavaBeans to shield application business logic from persistent storage formats
(3) The View Layer • Avoid creating "Yet Another Template Language" (YATL) • Leverage JavaServer Pages (JSP) technology for creating presentation components • Utilize widely available custom tag libraries to accelerate page development • Create application-specific custom tag libraries for specific rendering needs
(4) The Controller Layer • Leverage existing Model-View-Controller application frameworks • Use adapter classes to decouple business logic from web application APIs (servlets) • Encapsulate business logic in JavaBeans or session Enterprise JavaBeans
Resources • Java Software / Sun Microsystems • Java2 Enterprise Edition - http://java.sun.com/j2ee/ • Java2 Blueprints - http://java.sun.com/j2ee/blueprints/ • JavaServer Pages - http://java.sun.com/products/jsp/ • Servlets - http://java.sun.com/products/servlet/