Welcome!

Java Authors: Liz McMillan, Walter H. Pinson, III, Maureen O'Gara, Yakov Werde, Tony Bishop

Related Topics: AJAX & REA, Java, Web 2.0

AJAX & REA: Article

Creating AJAX and Rich Internet Components with JSF

Introducing a new open source project

Security
When serving packaged resources from a JAR, extra care must be taken not to make Java class files or other sensitive information accessible by URL. In desktop Java applications, resource files are often stored in a sub-package called "resources" underneath the Java implementation classes that use the resource files. The same strategy is also appropriate for packaged resources in JavaServer Faces component libraries, and has the security benefit of ensuring that only the resource files are accessible by URL. All other contents of the JAR file, including Java implementation classes, are not URL accessible because no Java classes exist in either the "resources" package or in any sub-package of "resources."

Weblets Protocol
Having covered how to configure Weblets, it's time to look at how we can reference resources defined by the Weblet in our renderer. The syntax, defined by the Weblet contract, for returning a proper URL to the JSF page is as follows:

<prefix><weblet name><resource>

The prefix indicates that this is a Weblet-managed resource, and this is followed by the Weblet name and the resource requested.

Previously, in our Renderer class, we passed the URL /myresources/myScript.js as an argument to the ViewHandler's getResourceURL() method. In the code sample below, we amend this to use the Weblet protocol instead.

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

The Weblet protocol-like syntax is convenient and easy to understand. The syntax starts with weblet://, followed by the Weblet name, e.g., org.myapp.html, and finally the path info or resource file, e.g., /myScript.js. Notice that neither the URL mapping nor the version number are included in the Weblet resource syntax. The Weblet URL mapping and version number are used by the WebletsViewHandler to create a resource URL that the Weblet will service.

When the component writer is not using Weblets, he would not be using the weblet:// resource path syntax and would distribute a separate installables zip. When the component writer moves to Weblets, he would start using weblet:// resource path syntax in the Renderer, and include the resources in the JAR. There is no benefit to using a mixture of these approaches for resources in the same version of the same component library.

Using Weblets in a JSF Application
In order to simplify setup for the application developer, component writers should select a default URL mapping for their component libraries. There is no need for the application developer to add any Weblet-specific configuration to the web.xml file, since the WebletsPhaseListener will be invoked automatically to service incoming requests for Weblet-managed resources.

Summary
As a new open source project, Weblets has tremendous possibilities to provide a defacto generic and configurable resource loading facility for Web clients and the JSF component community. The key differentiators are simplified packaging of JSF components and their resources, and a minimized overhead of installing and setting up JSF component libraries for a particular Web application project.

This article has explored a new way of packaging resources with JSF components. You should now be able to leverage Weblets in your own component library by including a suitable weblets-config.xml file and using the weblet:// protocol-style syntax to reference Weblet-managed resources.

In our next article in this series of building "Rich Internet Components with JavaServer Faces," we are going to look at how we can design JSF components using AJAX and Weblets.

More Stories By Jonas Jacobi

Jonas Jacobi is co-founder and chief executive officer of Kaazing Corporation. A native of Sweden, Jacobi has worked in the software industry for more than 15 years with a mission to simplify application development. Prior to founding Kaazing, he worked for Oracle for eight years as a Java EE evangelist and product manager responsible for the product management of JavaServer Faces, Oracle ADF Faces, and Oracle ADF Faces Rich Client in the Oracle JDeveloper team. As co-founder and CEO of Kaazing, Jonas sets the company's business and product strategy and oversees all aspects of Kaazing's operations and mission to become the world-wide leader in real-time software. He is co-author of the best-selling book, "Pro JSF and Ajax: Building Rich Internet Components," (Apress).

More Stories By John Fallows

John Fallows, Co-Founder & CTO of Kaazing Corporation, is a pioneer in the field of rich and highly interactive user interfaces. In his role as chief technology officer, John formulates Kaazing's vision of creating the best real-time web framework based on the Java standard. He defines the architecture of the Kaazing product suite and oversees its development.

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.