Welcome!

Machine Learning Authors: Elizabeth White, Leon Adato, Jack Jachner, Pat Romanski, Progress Blog

Related Topics: Machine Learning , Java IoT, Agile Computing

Machine Learning : Article

Creating AJAX and Rich Internet Components with JSF

Introducing a new open source project

This article is based on, and contains excerpts from, the book Pro JSF: Building Rich Internet Components by Jonas Jacobi and John Fallows, published by Apress. Book is available on fine bookstores and Amazon.

In our previous article - "Rich Internet Components with JavaServer Faces" (JDJ, Vol. 10, issue 11) - we discussed how JavaServer Faces can fulfill new presentation requirements without sacrificing application developer productivity building Rich Internet Applications (RIA). We discussed how JSF component writers can utilize technologies, such as AJAX and Mozilla XUL, to provide application developers with rich, interactive, and reusable components.

 To use AJAX and Mozilla XUL with JSF, component writers have to make sure to provide any resource files needed by these technologies, such as images, style sheets, or scripts. The standard approach to providing resource files for a JSF component library is to serve them directly out of the Web application root file system. These resources are usually packaged in an archive (such as a ZIP file) and shipped separately from the JSF component library.

This article introduces a new open source project - Weblets - which can be found on the java.net Website (http://weblets.dev.java.net). The goal of this open source project is to provide JSF component writers with a facility that can serve resource files out of a Java archive (JAR), rather than serving them from the Web application root file system. Unlike traditional Web applications, which have statically configured URL mappings defined in web.xml, there is a need for dynamic configuration of URL mappings, based on the presence of a component library JAR. In essence, Weblets provide developers with an easy way to package Web application resources in the same Java archive (JAR) that their implementation code resides in.

Resource Loading
Let's assume that we have a JSF component that needs to have a JavaScript file, myScript.js, served to the client. This JavaScript file is used by the component to provide some level of richness when interacted with by the end user. This JavaScript file is traditionally served by the Web application via a relative path that is hard coded into the actual Renderer code for the JSF component. This requires the application developer to deploy additional resources that are delivered and packaged in a separate archive file, e.g., ZIP, often referred to as "installables."

It is important to note that the JavaServer Faces HTML basic RenderKit does not have any images, styles, or scripts, so there is no standard solution to the Faces resource packaging problem.

The following sample Renderer code illustrates the installables approach to serving a JavaScript file, /myresources/myScript.js, from the Web application root file system.

ViewHandler handler = context.getApplication().getViewHandler();
String resourceURL = handler.getResourceURL(context,"/myresources/myScript.js");
out.startElement("script", null);
out.writeAttribute("type", "text/javascript", null);
out.writeAttribute("src", resourceURL, null);
out.endElement("script");

Although the installables approach is convenient for the JSF component author, it does increase the installation burden on the application developer, who must remember to extract the installables archive each time the component library is upgraded to a new version. Therefore, we need a way to package our additional resources into the same JAR file containing the Renderer classes, simplifying deployment for application developers using our component library.

Using Weblets
The open source Weblets project aims to solve the resource-packaging problem in a generic and extensible way so that it can be leveraged by all JavaServer Faces component writers, while placing only a minimal installation burden on the application developer.

A Weblet acts as a mediator that intercepts requests from the client and uses short Web URLs to serves resources from a JAR file. Unlike the Servlet or Filter approach, a Weblet can be registered and configured inside a JAR, so the component library Renderers, their resource files, and the Weblet configuration file (weblets-config.xml) can all be packaged together in the same JAR. The Weblet Container can be registered just once in the Web application configuration file - web.xml - for all component libraries. There is no need to separately deploy additional installables when the component libraries are upgraded to new versions.

It is important to note that all resources served up by Weblets are internal resources, used only by the Renderer. Any resources, like images, that are provided by the application, are supplied as component attribute values and loaded from the context root as external resources.

Weblet Architecture
Although Weblets were designed to be used by any Web client, the Weblets implementation has been integrated with JavaServer Faces using a custom ViewHandler, WebletsViewHandler, as shown in Figure 1. During rendering of the main JavaServer Faces page, the WebletsViewHandler is responsible for converting Weblet-specific resource URLs into the actual URLs used by the browser to request Weblet-managed resources.

After receiving the rendered markup for the main page, the browser downloads each additional resource using a separate request. Each request for a Weblet-managed resource is intercepted by the WebletsPhaseListener, which then asks the WebletContainer to stream the Weblet-managed resource file out of the component library JAR.

The WebletContainer is designed to leverage the browser cache where possible. This improves overall rendering performance by minimizing the total number of requests made for Weblet-managed resource files.

To ensure flexibility and optimization, and avoid collisions with existing Web application resources, Weblets can be configured by application developers to override any default settings provided by the component author.

Using Weblets in a Component library
Weblets are configured using a weblets-config.xml file, which must be stored in the /META-INF directory of the component library JAR. Configuring a Weblet is similar to configuring a Servlet or a Filter. Each Weblet entry in the weblets-config.xml file has a Weblet name, implementation class, and initialization parameters (see Listing 1). The Weblet mapping associates a particular URL pattern with a specific Weblet name, e.g., org.myapp.html. The Weblet name and default URL pattern define the public API for the Weblet-managed resources and should not be modified between releases of your component library, in order to maintain backward compatibility.

Our component library packages resources in the org.myapp.faces.renderer.html.resources Java package and makes them available to the browser using the default URL mapping of /myresources/*.

The PackagedWeblet is a built-in Weblet implementation that can read from a particular Java package using the ClassLoader and stream the result back to the browser. The package initialization parameter tells the PackagedWeblet which Java package to use as a root when resolving Weblet-managed resource requests.

Weblet Versioning
Weblets also has built-in support for versioning of the component library. This is used to allow the browser to cache packaged resources such as myScript.js when possible, preventing unnecessary round-trips to the Web server.

Each time the browser renders a page, it will ensure that all resources used by that page are available. During the initial rendering of the page, the browser populates its cache with the contents of each resource URL by downloading a fresh copy from the Web server. As it does so, the browser records the Last-Modified and Expires timestamps from the response headers. The cached content is said to have expired if the current time is later than the expiration timestamp, or if no expiration timestamp information exists.

On the next render of the same page, the browser checks to see if the locally cached resource has expired. The locally cached copy is reused if it has not expired. Otherwise, a new request is made to the Web server, including the last modified information in the If-Modified-Since request header. The Web server responds by either indicating that the browser cache is still up-to-date, or by streaming the new resource contents back to the browser with updated Last-Modified and Expires timestamps in the response headers.

Weblets use versioning to leverage the browser cache behavior so that packaged resources can be downloaded and cached as efficiently as possible. The browser only needs to check for new updates when the cache has been emptied or when the component library has been upgraded at the Web server.

Listing 2 illustrates the Weblets versioning feature by adding a 1.0 version to our org.myapp.html Weblet.

By specifying a Weblet version, you indicate that the packaged resource is not going to change until the version number changes. Therefore, the version number is included as part of the resource URL determined at runtime by the WebletsViewHandler, e.g., /myresources$1.0/myScript.js. When the WebletContainer services this request, it extracts the version number from the URL and determines that the resource should be cached and never expire. As soon as a new version of the component library is deployed to the Web application, the resource URL created at runtime by the WebletsViewHandler changes, e.g., /myresources$2.0/myScript.js, thus the browser's cached copy of myScript.js for version 1.0 is no longer valid because the URL is different.

During development, the contents of packaged resources can change frequently, so it is important for the browser to keep checking back with the Web server to detect the latest resource URL contents. This check happens by default every time the main Web page is rendered if the Weblet version is omitted from weblets-config.xml.

Alternatively the Weblet configuration allows component authors to append -SNAPSHOT to the version number. For example, 1.0-SNAPSHOT, as shown in the following code, indicates that this file is under development and should behave as though the version number has been omitted.

<?xml version="1.0" encoding="UTF-8" ?>
<weblets-config xmlns="http://weblets.dev.java.net/config" >
   <weblet>
     <weblet-name>org.myapp.html</weblet-name>
     <weblet-class>net.java.dev.weblets.packaged.PackagedWeblet      </weblet-class>
     <weblet-version>1.0-SNAPSHOT</weblet-version>
     ...
   </weblet>
   ...
</weblets-config>

More Stories By Kaazing Blog

Kaazing is helping define the future of the event-driven enterprise by accelerating the Web for the Internet of Things.

More Stories By John Fallows

John brings to Kaazing his 17 years’ experience in technology development and software design, and is considered a pioneer in the field of rich and highly interactive user interfaces. As CTO he formulates Kaazing Corporation’s vision of enabling mobile users, marketplaces and machines to connect and communicate in real-time, more reliably and at unprecedented scale. He defines the architecture of the Kaazing product suite and oversees its development. Prior to co-founding Kaazing, John served as Architect for Brane Corporation, a startup company based in Redwood City, California. Before joining Brane, he was a Consulting Member of Technical Staff for Server Technologies at Oracle Corporation. During his last five years at Oracle, he focused on designing, developing, and evolving Oracle ADF Faces to fully integrate Ajax technologies. Originally from Northern Ireland, he received his MA in Computer Science from Cambridge University in the United Kingdom and has written several articles for leading IT magazines such as Java Developer’s Journal, AjaxWorld Magazine, and JavaMagazine (DE), and is a popular speaker at international conferences. He is co-author of the bestselling book Pro JSF and Ajax: Building Rich Internet Components (Apress).

Comments (7) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
AJAX News Desk 04/20/06 08:31:14 AM EDT

In our previous article - 'Rich Internet Components with JavaServer Faces' (JDJ, Vol. 10, issue 11) - we discussed how JavaServer Faces can fulfill new presentation requirements without sacrificing application developer productivity building Rich Internet Applications (RIA). We discussed how JSF component writers can utilize technologies, such as AJAX and Mozilla XUL, to provide application developers with rich, interactive, and reusable components.

SYS-CON India News Desk 04/12/06 10:29:46 AM EDT

In our previous article - 'Rich Internet Components with JavaServer Faces' (JDJ, Vol. 10, issue 11) - we discussed how JavaServer Faces can fulfill new presentation requirements without sacrificing application developer productivity building Rich Internet Applications (RIA). We discussed how JSF component writers can utilize technologies, such as AJAX and Mozilla XUL, to provide application developers with rich, interactive, and reusable components.

AJAX News Desk 04/10/06 07:30:09 PM EDT

In our previous article - 'Rich Internet Components with JavaServer Faces' (JDJ, Vol. 10, issue 11) - we discussed how JavaServer Faces can fulfill new presentation requirements without sacrificing application developer productivity building Rich Internet Applications (RIA). We discussed how JSF component writers can utilize technologies, such as AJAX and Mozilla XUL, to provide application developers with rich, interactive, and reusable components.

SYS-CON Australia News Desk 04/10/06 05:53:10 PM EDT

In our previous article - 'Rich Internet Components with JavaServer Faces' (JDJ, Vol. 10, issue 11) - we discussed how JavaServer Faces can fulfill new presentation requirements without sacrificing application developer productivity building Rich Internet Applications (RIA). We discussed how JSF component writers can utilize technologies, such as AJAX and Mozilla XUL, to provide application developers with rich, interactive, and reusable components.

Anil Sharma 02/26/06 02:35:43 PM EST

How does JSF approach compare with the following approach?

1. Get data as XML over HTTP(S).
2. Render everything in the broweser space.

What are the pros and cons? In my experience the above approach is far simpler than using any server side presentation technology (assuming that you use a framework and not start from scratch). It is a much more simpler model. Therefore, why JSF if I am starting a new project? It will help to get your opinion on it.

SYS-CON Australia News Desk 02/25/06 02:33:57 PM EST

In our previous article - 'Rich Internet Components with JavaServer Faces' (JDJ, Vol. 10, issue 11) - we discussed how JavaServer Faces can fulfill new presentation requirements without sacrificing application developer productivity building Rich Internet Applications (RIA). We discussed how JSF component writers can utilize technologies, such as AJAX and Mozilla XUL, to provide application developers with rich, interactive, and reusable components.

SYS-CON Italy News Desk 02/08/06 02:56:48 PM EST

In our previous article - 'Rich Internet Components with JavaServer Faces' (JDJ, Vol. 10, issue 11) - we discussed how JavaServer Faces can fulfill new presentation requirements without sacrificing application developer productivity building Rich Internet Applications (RIA). We discussed how JSF component writers can utilize technologies, such as AJAX and Mozilla XUL, to provide application developers with rich, interactive, and reusable components.

@CloudExpo Stories
From 2013, NTT Communications has been providing cPaaS service, SkyWay. Its customer’s expectations for leveraging WebRTC technology are not only typical real-time communication use cases such as Web conference, remote education, but also IoT use cases such as remote camera monitoring, smart-glass, and robotic. Because of this, NTT Communications has numerous IoT business use-cases that its customers are developing on top of PaaS. WebRTC will lead IoT businesses to be more innovative and address...
Blockchain is a shared, secure record of exchange that establishes trust, accountability and transparency across business networks. Supported by the Linux Foundation's open source, open-standards based Hyperledger Project, Blockchain has the potential to improve regulatory compliance, reduce cost as well as advance trade. Are you curious about how Blockchain is built for business? In her session at 21st Cloud Expo, René Bostic, Technical VP of the IBM Cloud Unit in North America, will discuss th...
While some vendors scramble to create and sell you a fancy solution for monitoring your spanking new Amazon Lambdas, hear how you can do it on the cheap using just built-in Java APIs yourself. By exploiting a little-known fact that Lambdas aren’t exactly single-threaded, you can effectively identify hot spots in your serverless code. In his session at @DevOpsSummit at 21st Cloud Expo, Dave Martin, Product owner at CA Technologies, will give a live demonstration and code walkthrough, showing how ...
SYS-CON Events announced today that CA Technologies has been named “Platinum Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business – from apparel to energy – is being rewritten by software. From planning to development to management to security, CA creates software that fuels transformation for companies in the applic...
Cloud adoption is often driven by a desire to increase efficiency, boost agility and save money. All too often, however, the reality involves unpredictable cost spikes and lack of oversight due to resource limitations. In his session at 20th Cloud Expo, Joe Kinsella, CTO and Founder of CloudHealth Technologies, tackled the question: “How do you build a fully optimized cloud?” He will examine: Why TCO is critical to achieving cloud success – and why attendees should be thinking holistically ab...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
As more and more companies are making the shift from on-premises to public cloud, the standard approach to DevOps is evolving. From encryption, compliance and regulations like GDPR, security in the cloud has become a hot topic. Many DevOps-focused companies have hired dedicated staff to fulfill these requirements, often creating further siloes, complexity and cost. This session aims to highlight existing DevOps cultural approaches, tooling and how security can be wrapped in every facet of the bu...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, will introduce two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a...
yperConvergence came to market with the objective of being simple, flexible and to help drive down operating expenses. It reduced the footprint by bundling the compute/storage/network into one box. This brought a new set of challenges as the HyperConverged vendors are very focused on their own proprietary building blocks. If you want to scale in a certain way, let’s say you identified a need for more storage and want to add a device that is not sold by the HyperConverged vendor, forget about it....
SYS-CON Events announced today that Calligo has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Calligo is an innovative cloud service provider offering mid-sized companies the highest levels of data privacy. Calligo offers unparalleled application performance guarantees, commercial flexibility and a personalized support service from its globally located cloud platform...
As DevOps methodologies expand their reach across the enterprise, organizations face the daunting challenge of adapting related cloud strategies to ensure optimal alignment, from managing complexity to ensuring proper governance. How can culture, automation, legacy apps and even budget be reexamined to enable this ongoing shift within the modern software factory?
SYS-CON Events announced today that Elastifile will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Elastifile Cloud File System (ECFS) is software-defined data infrastructure designed for seamless and efficient management of dynamic workloads across heterogeneous environments. Elastifile provides the architecture needed to optimize your hybrid cloud environment, by facilitating efficient...
SYS-CON Events announced today that Cloudistics, an on-premises cloud computing company, has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Launched in 2016, Cloudistics helps anyone bring the power of the cloud to the data center in an easy-to-use, on- premises cloud platform that automatically provides high performance resources for all types of applications: Docke...
With Cloud Foundry you can easily deploy and use apps utilizing websocket technology, but not everybody realizes that scaling them out is not that trivial. In his session at 21st Cloud Expo, Roman Swoszowski, CTO and VP, Cloud Foundry Services, at Grape Up, will show you an example of how to deal with this issue. He will demonstrate a cloud-native Spring Boot app running in Cloud Foundry and communicating with clients over websocket protocol that can be easily scaled horizontally and coordinate...
@DevOpsSummit at Cloud Expo taking place Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center, Santa Clara, CA, is co-located with the 21st International Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is ...
SYS-CON Events announced today that Golden Gate University will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Since 1901, non-profit Golden Gate University (GGU) has been helping adults achieve their professional goals by providing high quality, practice-based undergraduate and graduate educational programs in law, taxation, business and related professions. Many of its courses are taug...
SYS-CON Events announced today that Golden Gate University will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Since 1901, non-profit Golden Gate University (GGU) has been helping adults achieve their professional goals by providing high quality, practice-based undergraduate and graduate educational programs in law, taxation, business and related professions. Many of its courses are taug...
DevOps at Cloud Expo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to w...
SYS-CON Events announced today that Secure Channels, a cybersecurity firm, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Secure Channels, Inc. offers several products and solutions to its many clients, helping them protect critical data from being compromised and access to computer networks from the unauthorized. The company develops comprehensive data encryption security strategie...
Vulnerability management is vital for large companies that need to secure containers across thousands of hosts, but many struggle to understand how exposed they are when they discover a new high security vulnerability. In his session at 21st Cloud Expo, John Morello, CTO of Twistlock, will address this pressing concern by introducing the concept of the “Vulnerability Risk Tree API,” which brings all the data together in a simple REST endpoint, allowing companies to easily grasp the severity of t...