PATH Documentation > WebObjects

 

Passing Data From a JSP Page to a Component

In this section, you'll expand the JSP_Example project to include

The FavoriteFood component contains two attributes: visitorName and favoriteFood. When the DiningWell workhorse servlet receives a request, it passes two strings to the FavoriteFood component. The FavoriteFood component then uses those string to render its HTML code.

  1. Using a text editor, create a file with the following contents:
    <%-- DiningWell.jsp --%>
    
    <%@ taglib uri="/WOtaglib" prefix="wo" %>
    
    <HTML>
    
    <HEAD>
        <TITLE>Using Two Components</TITLE>
    </HEAD>
    
    <BODY>
        <wo:component className="Hello">
        </wo:component>
        <P><P>
        <wo:component className="FavoriteFood" bodyContentOnly="true">
            <wo:binding key="visitorName" value='<%= "Worf" %>' />
            <wo:binding key="favoriteFood" value='<%= "gagh" %>' />
        </wo:component>
    </BODY>
    
    </HTML> 

    Note that in this case the bodyContentOnly attribute of the <wo:component> tag is set to true (this is the default, so you don't need to specify a value for it). This allows you to define the FavoriteFood component as "Full document" (the default setting in WebObjects Builder) instead of "Partial document." This way, the component can be viewed as a Web page on its own and as a component within a JSP page.

    For faster processing, you can set the bodyContentOnly attribute to false if you are certain that the component only includes the <BODY> tag and not the <HTML> tag.

  2. Save the file as DiningWell.jsp in the JSP_Example/Servlet Resources/jsp directory.
  3. In Project Builder, create a component called FavoriteFood (make sure you assign it to the Application Server target).
  4. Edit the component using WebObjects Builder so that it looks like Figure 3-4. Make sure to add accessor methods to the visitorName and favoriteFood String keys.

    Figure 3-4 The DiningWell component in WebObjects Builder

    [image: ../art/favorite_food_wo.gif]

    When you're done FavoriteFood.java should look like Listing 3-1.

    Listing 3-1 FavoriteFood.java

    import com.webobjects.foundation.*;
    import com.webobjects.appserver.*;
    import com.webobjects.eocontrol.*;
    import com.webobjects.eoaccess.*;
    
    public class FavoriteFood extends WOComponent {
        protected String visitorName;
        protected String favoriteFood;
    
        public FavoriteFood(WOContext context) {
            super(context);
        }
        
        public String visitorName() {
            return visitorName;
        }
        public void setVisitorName(String newVisitorName) {
            visitorName = newVisitorName;
        }
        
        public String favoriteFood() {
            return favoriteFood;
        }
        public void setFavoriteFood(String newFavoriteFood) {
            favoriteFood = newFavoriteFood;
        }
    }
  5. Make sure the FavoriteFood component is set to "Full document" (see "Creating a JSP-Based Application" for details).
  6. Build the project and, if you're using Tomcat 3.2.3, restart your servlet container.

If you're using Tomcat, you can view the new page in your browser with this URL

http://localhost:8080/JSP_Example/jsp/DiningWell.jsp

The Web page should look like Figure 3-5.


Figure 3-5 The output of DiningWell.jsp

[image: ../art/dining_well_jsp.gif]

This is the HTML code your Web browser receives:

<HTML>

<HEAD>
    <TITLE>What to eat?</TITLE>
</HEAD>

<BODY>
    Hello, World!
    <P><P>
    Worf's favorite food is gagh.
</BODY>

</HTML> 


 


© 2002 Apple Computer, Inc. (Last Updated January 3, 2002)