Web 2.0 is the term coined for the growing Internet trend characterized by online collaboration and sharing among users. Web 2.0 is changing the way people interact with Web applications, leading nine out of ten Web sites to review their implementation of the request/wait/response paradigm. Technologies like asynchronous JavaScript and XML (AJAX) enhance the user experience of accessing the Web, meeting standards that Web application developers have sought for many years. The success of AJAX occurred when advanced Web applications such as Google Maps and Gmail became generally available. Google offers a framework -- the Google Web Toolkit (GWT) -- to facilitate the development of advanced AJAX applications. This paper tells how to build AJAX applications, including the use of personalized components that extend the application program interface (API) of GWT. To demonstrate the benefits of this technology, we include examples of creating a paged table with editable fields and a personalized dialog box. We discuss the management of events and service calls for data access, as well as peculiar aspects of the API and tips about its use.
Trang 1Greg Murray February 2006
Web 2.0 and AJAX with Java
Trang 4Web 2.0
• Web as a Platform
• Collection Intelligence
> Folksonomy – Collaborative Categorization
• Data is key and should be shared
• Software is in constantly evolving
> Software release cycles dead?
• Lightweight Programming Models
> SOAP/REST
• The Network is the computer
• Rich User Experience
Trang 5Conventional Rich Web Applications
Trang 6Conventional Interaction Model
Trang 7High Level AJAX Interaction Model
Trang 8Asynchronous JavaScript + XML
AJAX is using JavaScript, namely the XmlHttpRequest object, to communicate asynchronously with a server-side
component and dynamically update the source of an HTML page based on the
resulting XML/Text response.
Trang 9Anatomy of an AJAX Interaction
Trang 10HTML Page Event
<form name="autofillform" action="autocomplete" method="get">
<table border="0" cellpadding="5" cellspacing="0">
Trang 11JavaScript Event Handler
Trang 12public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
sb.append( "<id>" + e.getId() + "</id>" );
sb.append( "<firstName>" + e.getFirstName() + "</firstName>" );
sb.append( "<lastName>" + e.getLastName() + "</lastName>" );
response.getWriter().write( "<employees>" + sb.toString() + "</employees>" );
} else response.setStatus(HttpServletResponse.SC_NO_CONTENT);
Trang 13JavaScript Client Callback
for (loop = 0; loop < employees.childNodes.length; loop++) {
var employee = employees.childNodes[loop];
appendEmployee(firstName.childNodes[0].nodeValue,
lastName.childNodes[0].nodeValue, employeeId.childNodes[0].nodeValue); }
Trang 14AJAX Demo
Trang 18Recommendation: Consider the meaning of each and weigh
the benefits when designing your application
Trang 19XMLHttpRequest (XHR)
• HTTP Method
> GET - When the result of N > 0 requests is the same
> POST - When operation has “side-effects” and changes
the state on the server.
• Concurrent Requests
> Max is 2 (IE) Consider - Pooling
> JavaScript Closures – Inline functions
Recommendation: Take care using the XHR Use Closures to track the requests/callbacks Consider using a library.
Trang 20Internationalization (I18n)
• Page Content Type
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
• Use JavaScript encodeURI when building URLs or
sending localizable content.
• Call request.setCharacterEncoding("UTF-8") before retrieving any parameters from Java EE.
• Call response.setContentType(“text/xml;charset=UTF-8”)
Recommendation: Use UTF-8 since it supports the widest number of languages and browsers
Trang 21AJAX Design
• Add Around the Edges
> Small components (autocomplete, tree, partial submit)
• Page is the Application
> Client and Server split MVC responsibilities
Recommendation: Consider designing initial AJAX applications around the edges as you gain experience Don't go overboard.
Trang 22Recommendation: If you already have a server centric architecture
consider adding some client centric components When using a client centric architecture consider using an existing library.
Trang 23Response Content Type
> Post processing on client
> Inject directly into the page
• JavaScript
> Evaluated in JavaScript using eval()
> JavaScript object representations of data(JSON)
Recommendation: Use XML for structured portable data Use plain text for when injecting content into the HTML Use JavaScript to return
object representations data.
Trang 24• Server Side Validation
• Value List Handler
Recommendation: Use AJAX to Enhance the user experience.
Trang 25• Sandboxed
> Cross Domain XMLHttpRequest restricted
> Access to file system restricted
• HTTPS – Requires a page refresh
• JavaScript Libraries for Encryption Exist
• JavaScript code visible to the world
Recommendation: Use HTTPS when you want to secure AJAX
communication Don't put compromising code in your JavaScript
Trang 26• http://www.macrumors.com/events/mwsf2006-stats.php
“We peaked at approximately 103,000 simultaneous web visitors and
6,000 IRC viewers during the Keynote speech and transmited over 32
GB of data in a three hour period If not for the efficiency of the
MacRumorsLive AJAX update system, the same webcast would have required approximately twice as many servers and would have had to transfer almost 6 times as much data (196 GB).”
• Patterns- Value List Handler/ Master Details
• JavaScript - Compression
Recommendation: AJAX Performs! Use patterns and try to reduce the size of your JavaScript files Consider dynamic loading of script
Trang 28JSF Component Approach
• Control Content Rendering
• Control of Server Side Logic
• All in one component
• Reusable
• Usable in a tool
• Hide AJAX complexity from page developers
Benefits Include:
Trang 29Anatomy of an AJAX enabled JSF
Component
Trang 30Page Developer's View of JSF
Trang 31Server Side Logic for JSF
Component
public String[] completeName() {
ArrayList results = new ArrayList();
}
}
return (String[])results.toArray();
Trang 32AJAX BluePrints
• BluePrints Solutions Catalog Entries on AJAX
> NetBeans
> Command Line
> Written for Glassfish http://glassfish.dev.java.net
• Java Petstore Demo
Trang 33• AJAX Pet Store
• More Java BluePrints Solutions Catalog entries
• More Java BluePrints AJAX components
• Better tool support
• Web 2.0/AJAX focus for JavaOne 2006
Trang 34• Java provides the ideal platform for AJAX and Web
2.0 style applications
• Use AJAX where it makes sense.
• Follow the guidelines
• The BluePrints team and BluePrints Solutions
Catalog is a great AJAX resources
Trang 36Q & A