Step 3: Personalize a Web Application

So far, you have a Portal which surfaces two applications. But the Portal lacks any personalization features. The favorite color application has no way to know when a user is logged in and it has no way of remembering a user's favorite color after the user has logged out of the Portal.

In this step you will make the favorite color application capable of remembering the user's favorite color after the user has logged out. You will add a User Profile Control to the application, a control which can save information about individual Portal users. The User Profile Control saves the information in a database (a database entirely managed by the control) and accesses the information using a set of property/value pairs.

The tasks in this step are:

To Edit the Application to Require Login

In this step you will edit the application so that it is accessible only to logged in users. You will accomplish this by setting properties on the favorite color application. First, you will set the application's login-required property to true. This will cause the application to throw a NotLoggedInException anytime a non-logged in user tries to access the application. Second, you will edit the application's catch property. The catch property provides error handling instructions to the application. In this case the application will handle the exception by sending non-logged in users to a JSP page that instructs them that they must first log on to use the application.

  1. On the Application tab, double-click the file favoriteColorController.jpf to open the file.
  2. On the Property Editor tab, set the login required property to true.

  3. On the Property Editor tab, click the plus sign to the right of the catch List property.

  4. On the Property Editor tab, click the plus sign to the left of catch(1).

  5. In the type property, enter com.bea.wlw.netui.pageflow.NotLoggedInException and press the Enter key.

  6. In the path property, enter notLoggedIn.jsp and press the Enter key.

  7. Press Ctrl+S to save your work.

To Add a User Profile Control

In this task you will add a User Profile Control to the favorite color application. A User Profile Control provides access to a user's User Profile: a store of information that is maintained by the Portal. The User Profile stores information about the user as a set of property/values pairs. Most importantly the User Profile persists after a user has logged out of the Portal. In this case you will save the user's favorite color in the User Profile.

  1. In the main work area, click the Action View tab.

  2. On the Data Palette tab, to the right of the Controls heading, click Add.

  3. From the menu, select Portal Controls-->Profile.

  4. In the Insert Control - User Profile Control dialog, enter userProfileControl.

  5. Click Create.
  6. Press Ctrl+S to save your work.

The User Profile Control you have added contains methods for storing user information (setProperties and setProperty) and methods for retreiving user information (such as getProfile and getProfileForUser). You can use these methods to store and retrieve user preferences and other information over a history of visits to the Portal.

To Edit the Web Application to Use the Control

In this task you will edit the favorite color application code to take advantage of the User Profile Control. When the user selects a favorite color, it will be saved in the User Profile. When the user revisits the Portal, the favorite color will be retrieved from the User Profile.

  1. In the main work area, click the Source View tab.

  2. At the top of the file, click the plus sign to the left of the import... area.

  3. Edit the set of import statements so it looks like the following. Code to add is shown in red.
    import com.bea.wlw.netui.pageflow.FormData;
    import com.bea.wlw.netui.pageflow.Forward;
    import com.bea.wlw.netui.pageflow.PageFlowController;
    import com.bea.p13n.usermgmt.profile.ProfileWrapper;
    
  4. In the main work area, click the Action View tab.

  5. In the main work area, click the link text for the begin method.

  6. Edit the source code for the begin method so it appears as follows. Code to add appears in red:
        /**
         * @jpf:action 
         * @jpf:forward name="showColor" path="color.jsp"
         */
        protected Forward begin()
            throws Exception
        {
            // Get the current user's profile.
            ProfileWrapper pw = userProfileControl.getProfileFromRequest( getRequest() );
    
            // If the favorite color retrieved from the user profile is not null...
            if( pw.getPropertyAsString( "favorites", "favoriteColor" ) != null )
            {
                // ...load the favorite color into the member variable _fav
                _fav = pw.getPropertyAsString( "favorites", "favoriteColor" );        
            }        
            
            // Render the color.jsp page 
            return new Forward( "showColor" );
        }
  7. In the main work area, click the Action View tab.

  8. In the main work area, click the link text for the setFav method.

  9. Edit the source code for the setFav method so it appears as follows. Code to add appears in red:
        /**
         * @jpf:action
         * @jpf:forward name="showColor" path="color.jsp"
         */
        protected Forward setFav(SelectFavForm form)
            throws Exception
        {
            // Get the current user's profile.
            ProfileWrapper pw = userProfileControl.getProfileFromRequest( getRequest() );
    
            // Save the selected color in the user's profile.
            // This color will be read from the profile on the
            // user's next visit to the portal.
            pw.setProperty( "favorites", 
                            "favoriteColor", 
                            form.getFavoriteColor() );         
            
            // Load the selected color into the global variable _fav.
            // The JSP page color.jsp reads the display color from _fav.
            _fav = form.getFavoriteColor();
            
            // Render the JSP page color.jsp.
            return new Forward("showColor");
        }
  10. Press Ctrl+S to save your work.

To Test the Portal

As you test the Portal, notice it has two new capabilities:

  1. The favorite color application is accessible only to logged in users
  2. The favorite color application remembers a user's favorite color while the user is logged out
  1. On the Application tab, double-click my.portal to display the file in the main work area.
  2. Click the Start button, shown below.

  3. When the Workshop Test Browser launches, click the Page 2 tab.

    Notice that the user must be logged in to access the Favorite Color application.

  4. Click the Page 1 tab and click the login link.

  5. Login with the username/password combination weblogic/weblogic.

  6. Click the Page 2 tab.

    Notice that the Favorite Color Application is visible once the user is logged in.

  7. Select a favorite color and click Click.

  8. Click the Page 1 tab and click the logout link.

  9. Login again with the username/password combination weblogic/weblogic.

  10. Click the Page 2 tab.

    Notice that the Portal remembered the user's favorite color while the user was logged out.

Related Topics

Click one of the following arrows to navigate through the tutorial: