Home > Articles

WebObjects Components

  • Print
  • + Share This
Like this article? We recommend

Using Images

So, what good is a Web page without some images? I say the key to a good looking Web site is to go heavy with the tables, and then pile on the KBs of images. (But then, I haven't won any awards for Web site design, so you probably shouldn't take that advice...)

There are a few ways to add images to your components. One way is to make the images part of your application and display them using a WOImage dynamic element. For most situations, this is appropriate method.

Another way is to bind the URL for the image to one of the WOImage bindings. This is useful if you are reusing images that already exist on a HTTP server.

It's fairly easy to add images to your application. The first thing you need to do is add the images to your Web Server Resources suitcase. This is done by either double-clicking the suitcase or by dragging the images from the file viewer and dropping them in the suitcase (see Figure 3.22). Next, you can drag the image from Project Builder to WebObjects Builder.

Figure 3.22 Drag an image to your component from Project Builder to WebObjects Builder to create a WOImage dynamic element.

Dragging or dropping the image from Project Builder to WebObjects Builder will create a WOImage dynamic element, with the filename bound to the name of the image. Table 3.6 lists the bindings for a WOImage.

Table 3.6 WOImage Attributes




This should be bound to an instance of NSData, which is a class that stores a blob of data. This binding is used when the images are either retrieved from disk or database or are created dynamically. If this is bound, the mimeType binding must also be specified.


Specifies a filename for a file in the Web Server Resources suitcase or in the framework specified by the framework binding.


If the image is in a WebObjects framework, it is the name of the framework. Frameworks will be discussed in Chapter 11, "Creating Reusable Components."


When using the data binding, the WebObjects Resource Manager will cache the data according to the key specified in this binding. If this is used, the application will be able to use the cached data rather than reload it.


The mime type of the image; that is, image/gif, image/jpg, and so on. This is required if the data tag is specified.


What is displayed if the browser cannot display the image.


The URL of the image. This is useful if the images for your application are stored on an existing HTTP server.

In general, there are three ways to use a WOImage: binding the data, binding the src, or binding the filename. If two of those bindings are set for a WOImage, it will be invalid.

WOImage data Binding Example

One of the ways to use a WOImage is to bind the data attribute to an instance of an NSData. NSData is a class in the Foundation kit that stores a blob of data as an array of bytes. This gives you a lot of flexibility: You can store and retrieve image data from the database, you can manipulate image data, or you can generate images on-the-fly, similar to the way the Street Mapping Web sites work.

NSData has only a few methods (see Table 3.7), mainly because the purpose if the class is to provide an object-oriented interface to an array of bytes.

Table 3.7 Common NSData Methods



NSData( java.net.URL )

Creates an NSData object using the contents of the URL provided.

NSData( byte [] )

Creates an NSData object using the array of bytes.

NSData( java.io.File )

Opens the file and creates an NSData object using the contents of the file.

public byte[] bytes()

Returns the contents of the NSData object as an array of bytes.

public int length()

Returns the length, in bytes, of the contents of the object.

An example of using the NSData class is modifying the Random Numbers example to display a random image. Assume that there are six images in a dice directory. The images are shown in Table 3.8. These images are available at the book's Web site: http://www.wowack.com.

Table 3.8 The Dice Images









We need to create a method that will return an NSData object that represents one of the random images every time. The easiest way to do this is to create an array of filenames, and randomly choose a file to load and display. The code for this component is given in Listing 3.4.

Listing 3.4

The Main Component for the RandomDice Application

import com.apple.yellow.foundation.*;
import com.apple.yellow.webobjects.*;
import com.apple.yellow.eocontrol.*;
import com.apple.yellow.eoaccess.*;
import java.io.*;

public class Main
  extends WOComponent
  String [] filenames={ "one.jpg", "two.jpg",
  String basePath="d:\\WOProjects\\dice\\";

  public int randomNumber()
    return (int)(Math.random()*6.0);

  public NSData randomImage()
    String filename = basePath+filenames[this.randomNumber()];
    File file = new File( filename );
    return new NSData(file);

The filenames instance variable stores the filenames, and the basePath instance variable stores the location where the files are stored. Replace this with the actual location of the files. The double backslashes are used because the filename is a string, and the backslash is an escape character. Therefore, \\ represents one backslash. If you're specifying a path on the Mac, you simply need to use the forward slash. For example, if the dice directory is in your home directory under the images, the basePath instance variable would be set as follows:

String basePath="~/images/dice/";

For complete cross-platform compatibility, get the file.separator Java system property using:


Another change in Listing 3.4 is the randomNumbers method. It now returns a random number between 0 and 5.

The biggest change is the addition of the randomImage method. This method returns an NSData object that represents one of the filenames specified in the instance variable array. The complete filename is calculated based on the basePath and filenames variables, and then the File object is created. This can be used to construct the NSData object that is returned. After we have this code, displaying the data will be easy.

Add a WOImage to the Main component in WebObjects Builder, and bind the values according to Table 3.9. Save the component and the code, and build your application.

Table 3.9 WOImage Bindings for the Dice Example







Where Are the Web Server Resources?

WebObjects does a good job of handling the resources such as images and text files for you. This is extremely useful most of the time because you are freed from the mundane task of keeping track of files and directories. However, it can get in the way sometimes, such as when you want to retrieve the image for a different reason, or if you want to find the relative path to another resource.

Resources Versus Web Server Resources

Resources that are not going to be sent to the client can be installed anywhere on the server. Examples of this type of resource are the model file, lookup files, configuration files, and so on. Resources that are to be served to the clients must be installed in a location within the Web Server's document root directory. When your application is built, it is placed in a .woa directory. Two directories are inside that directory: Resources and WebServerResources. The application executable is placed inside the .woa directory. Figure 3.23 displays the directory structure for the RandomDice application.

Figure 3.23 The directory structure for the RandomDice application.

When the application is installed, the contents of the WebServerResources directory are copied into a directory within the document root of the Web server. This is set in the WebServerConfig.plist, which is found in the \Apple\Library\WebObjects\Configuration directory on Windows and the /System/Library/WebObjects/Configuration directory on Macs. An example of this file is given in Listing 3.5.

Listing 3.5

The WebServerConfig.plist

  //** This configuration file is used by WOF
  //** to compute startup parameters.
  DocumentRoot = "d:/inetpub/wwwroot/";
  WOAdaptorURL = "http://localhost/scripts/WebObjects.exe";

Programmatic Access to Resources

It's common to want to have access to the resources within the project. There's a need to access these resources through the file system and another need to access resources by providing URLs. The WOResourceManager is used to retrieve the files either way.

To retrieve the WOResourceManager, use the WOApplication's resourceManager() method:

public WOResourceManager resourceManager()

The WOResourceManager contains two methods that are used to return file paths: urlForResourceNamed() and pathForResourceNamed().


The declaration for the urlForResourceNamed() method is as follows:

public String urlForResourceNamed(
    String fileName,
    String frameworkName,
    NSArray laguageList,
    WORequest request )

The urlForResourceNamed() returns a path on the Web server to the resource given in the filename argument. The resource name is the name as it appears in the suitcase in Project Builder. If the resource is contained in a framework, give the framework name in the second argument; otherwise use null. You can also specify a language list for multilingual applications, or null for a single-language application. And, you will need to pass in the current request so the URL can be generated correctly. The URL returned can then be used in a hyperlink.


The declaration for the pathForResourceNamed() method is as follows:

public String pathForResourceNamed(
    String fileName,
    String frameworkName,
    NSArray laguageList )

This method returns the absolute file path to the resource given the name of the resource. Similar to urlForResourceNamed(), this method can compute the path given a framework and list of languages. Unlike urlForResourceNamed(), a WORequest is not required.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020