Home / Articles / Adobe ColdFusion / Developing a ColdFusion/J2EE Solution

Developing a ColdFusion/J2EE Solution

Chapter Description

This article kicks off in the midst of fictional Sonic Systems' web project. It shows how the aim in creating a project blueprint was to eliminate the possibility for changes in the architecture during development, and to help development go as smoothly as possible.

Portlet Rendering

The first step was drawing an individual portlet. This would be done using the <CF_PORTLET> custom tag along with a registry component that would read relevant information from the PSML registry, such as the type of portlet being rendered (CFML, HTML, etc.) and the title of the portlet.

Building the Registry Component

First, the component needed to be developed to read important information about portlets from the registry. This component would have the following methods within it:

  • getEntry(). Returns a ColdFusion XML Document object of the entry—to be used mainly for debugging purposes.

  • getEntryIndex(). Returns the array location in the ColdFusion XML Document object for the specified registry (this is mainly used internally).

  • getEntryNames(). Returns all entries in the specified registry.

  • getParameter(). Returns the value the parameter specified for a particular entry.

  • getPortletParent(). Returns the parent type of the portlet—this is the type, such as HTML, JSP, CFML, or servlet.

  • getPortletTemplate(). Returns the actual template of an entry.

  • getTitle(). Returns the title of the entry.

  • getURL(). Returns the URL of an entry of the type, HTML.

When talking about components, the terms method and function are synonymous. The coding of this CFC was fairly straightforward, using the CFC constructs, as well as ColdFusion MX's new XML features. One of these, the XmlSearch() function, proved especially valuable. This function allows developers to search XML documents using XPath syntax.

The getEntryNames() method primarily used the XmlSearch() function to retrieve the entries:

   <CFARGUMENT NAME="Registry" DEFAULT="demo-portlets"
   <CFARGUMENT NAME="RegistryType" DEFAULT="portlet"

   <!--- read the appropriate file --->
   <CFSET RegistryLocation = Server.ColdFusion.RootDir
                          & "\wwwroot\WEB-INF\portal\conf\"
                          & ARGUMENTS.Registry &".xreg">
   <!--- parse the xml into DOM --->
   <CFSET Registry = XMLParse(XMLReg)>
   <!--- get only the child nodes necessary for this
   function --->
   <CFSET SelectedElements = XmlSearch(Registry,
             "/registry/" & ARGUMENTS.RegistryType & "-entry")>
   <!--- build array --->
   <CFSET ReturnArray = ArrayNew(1)>
       <CFSET ArrayAppend(ReturnArray,

   <CFRETURN ReturnArray>


After the initial function and arguments are declared using the <CFFUNCTION> and <CFARGUMENT> tags, the exact location of the registry is set using the relevant variables, and then retrieved using <CFFILE>. The XmlParse() function parses the XML into a ColdFusion document object. Then a variable called SelectedElements is set to the value returned by XmlSearch(). This function has two arguments: the first is the XML document object to be searched, and the second is a string containing the XPath search parameters.

One of the ways XPath can refer to XML documents is similar to a directory structure, which is what is used here. If the user were searching through the portlet directory, the value of ARGUMENTS.RegistryType would be "portlet," and the search syntax would be "/registry/portal-entry." In this type of XPath search, think of XML as a directory tree. The <registry> tag is the root of a registry document, and <portlet-entry> elements are its children. So this search would return an array including all of the <portal-entry> elements (and their XML contents) that are under the <registry> element.

This array is then looped through, and a new array is created containing only the value in the name attribute of each <portlet-entry> element. The array is what is returned to the calling template.

When this component was completed, Project Omega was able to use ColdFusion's introspective feature to list the methods within it and all

instant message

XPath offers a number of search methods and has a syntax all its own. This can be an extremely powerful tool for finding information within XML documents. For more information on XPath, please visit http://www.w3c.org/TR/xpath. relevant details. The details would include all of the methods, what arguments they took, and what they would return. In addition, whether they were available remotely and any other information would also be included. It would all display in an HTML page by calling on the CFC directly (see Figure I-3.4). The Project Omega team could refer to it to see what methods were available.

Figure 3-4 Figure I-3.4 The contents of a ColdFusion component can be easily viewed through a browser.

Once this component was completed, the <CF_PORTLET> tag would need to be developed. This tag would take the following attributes:

ID. The unique name of the portlet as listed in the registry.

DISPLAY. This could take the value of MAXIMIZED, MINIMIZED, NORMAL, or CLOSED.

WIDTH. This would represent the percentage width a portlet would take up in its window; the default is 95 percent.

In addition to these attributes, the SESSION.Registry variable needs to be set, holding the user's registry. The APPLICATION.PortletDirectory variable will also be required to know where to obtain the actual portlet templates.

This tag first checks for attributes and either exits or sets default values. Once this is completed, The DISPLAY attribute is checked; if it is CLOSED, then the tag exits using <CFEXIT>, as there is no need to render the portlet.


Next, the title is retrieved from the registry component using its getTitle() method using the <CFINVOKE> tag:

<CFINVOKE COMPONENT="portal.components.registry"

Because the components are in the /portal/components/ directory, and this is the registry.cfc component, the component is called on using the appropriate dot notation: portal.components.registry. This is the directory location, with folders separated by dots instead of forward slashes, and the component name without the .cfc ending. The method you want to call is specified in the METHOD attribute: getTitle. The return value will be set to a new variable called Title, as specified in the RETURNVARIABLE attribute. The remaining attributes are specific to the method called.

Following this, the actual design of the portlet is started by creating an HTML table containing the portlets title and the appropriate buttons to minimize, maximize, or close the portlet. Once the skins portion of the application is completed, this table will be updated to reflect the styles in a user's preferences. This table header is all that will be displayed if the port-let is minimized; otherwise, the main table cell contains the portlet itself.

Next, the getPortletParent() method of the registry component is called on to return the type of portlet that is to be displayed:

<CFINVOKE COMPONENT="portal.components.registry"

The variable returned by this is used to execute a <CFSWITCH> statement. Each case within this switch block will represent a supported value: HTML, CFML, JSP, and Servlet.


Embedding HTML Portlets

The HTML case uses <CFHTTP> to retrieve the appropriate URL:

     <!--- get HTML file for this portlet --->
     <CFINVOKE COMPONENT="portal.components.registry"
     <!--- check for local files - add server info --->
     <CFIF URLResource DOES NOT CONTAIN "http://">
       <CFSET Resource = "http://" & HTTP_HOST &
       <CFSET Resource = URLResource>
     <!--- get URL --->
     <CFHTTP URL="#Resource#"
                    <!--- display URL, but replace links to open new
                    window... --->
                   "<A ",
                   "<A TARGET=_BLANK ",
     This web page is currently unavailable.
     This web page is currently unavailable. 

Within this case, first the URL is obtained by calling on the getURL() method of the registry component. Next, a <CFTRY> block tries to retrieve the URL (adding http:// if necessary) using <CFHTTP>. In the retrieved page, all instances of "<A" are replaced with "<A TARGET=_BLANK ". This will open new windows for all links. A <CFCATCH> block catches any errors and outputs a friendly message.

Embedding JSP Portlets

JSP portlets work in a similar way:

     <!--- first, get template --->
     <CFINVOKE COMPONENT="portal.components.registry"
     <CFSET Resource = APPLICATION.PortletDirectory &
     <!--- include JSP template using getContext --->
     This application is currently unavailable.
     This application is currently unavailable.

The same <CFTRY> and <CFCATCH> method is used to gracefully control errors. The Project Omega team was surprised at the efficiency with which both JSP and Servlets could be absorbed into a ColdFusion page. The getPageContext() methods made this quite simple. To include a JSP page, the include(), method is called on and works just like a <CFINCLUDE> would with a ColdFusion page.

Embedding Servlet Portlets

instant message

ColdFusion MX is actually a giant J2EE application, and altering or removing any existing entries in the web.xml file can really screw up your ColdFusion server installation.

The Servlet code was literally identical to the JSP code. However, because Servlets were really just Java class files, their location had to be mapped into the Web application. Servlet mapping associates the appropriate Java class with a URL that can be called on by a browser or a ColdFusion template.

To map a Servlet, it's necessary to edit the web.xml file in the {ColdFusion_Root}/wwwroot/WEB-INF/ directory. This file controls the setup of a J2EE application when the application server is initialized.

To set up a server, a number of lines must be added to this file:

   <display-name>Snoop Servlet</display-name>
   <description>Outputs all variables in all


instant message

For more information on the web.xml file and its uses, please visit the Macromedia Tech Note Understanding web. xml Files at http://www.macromedia.com/v1/handlers/index.cfm?id=19399&method=full.

There are two primary elements that contain Servlet information: the <servlet> element, which contains primary information about a Servlet, and the <servlet-mapping> element, which maps the Servlet to a URL.

Within the <servlet> element are four primary child elements:

  • servlet-name> contains the name this Servlet will be referred to as.

  • servlet-class> contains the actual Java class file in dot notation.

  • Generally Servlets are placed within the {ColdFusion_Root}/wwwroot/ WEB-INF/classes/ directory.

  • <display-name> contains a user-friendly name for the Servlet. This is often used by J2EE application server administrative consoles.

  • <description> contains a description of the Servlet. This is also often used by J2EE application server administrative consoles.

    instant message

    The <servlet-mapping> element has only two child elements:

    The method getPageContext(). include() shows how Java stacks methods using the dot notation. Here, the include() method will be run on results of the getPageContext() method. This is different from the way ColdFusion has methods within methods, such as DollarFormat(Rand() ), which will run the DollarFormat() function on the results of the Rand() function.

  • <servlet-name>. This element links the mapping to the appropriate Servlet. This must match the <servlet-name> of a <servlet> element in the same web.xml file.

  • <url-pattern>. This element tells the application server what URL pattern to pass through the Servlet. This example refers to a specific URL (/servlets/Snoop), but other patterns may be used. For example, one could map all files with a .do ending using *.do as the URL pattern.

Once a Servlet is mapped, it can be called on using getPageContext() .include(ServletMapping). Which is precisely how Project Omega set up the Servlet type to work.

During this development phase, the Project Omega team discovered that another method, getPageContext().forward(), could perform server-side forwarding. This is different from <CFLOCATION>, which performs client-side redirects. By redirecting on the server side, an application can be more tightly controlled before information is sent out to the client.

Embedding CFML Portlets

Finally, the CFML portlet case was created. This was undoubtedly the simplest. The code was almost identical to the JSP and Servlet types, however, instead of the getPageContext().include() method, the <CFINCLUDE> tag is used to include the ColdFusion template.

Once this was developed and tested, the Project Omega team worked together to build some simple portlets. They created several simple portlets of every type that performed simple tasks, such as searching the registry, outputting variables using <CFDUMP>, and basic text output. These portlets were then added to the demo-portlets.xreg, and an anonymous user default.psml file was created following Jetspeed's user PSML format, with Victor's modifications. This dummy data could now be used to further develop the portal application.

8. PSML Reader | Next Section Previous Section