Show TOC

Background documentationUsage & Scopes Locate this document in the navigation structure

 

The bean concept - reusable components that can be used in more than one software package - plays an important role in the portal component development. Portal components can use the beans to store and retrieve data. The JSP as well as the servlet have read and write access to the bean so the bean can be used to transfer data between the JSP and the servlet. In the portal a servlet can be a DynPage or an Abstract Portal Component.

Declaration of a bean in the Servlet

In the servlet the bean is usually defined with a name, a constructor and set and get methods to handle the data. Some HTMLB-Controls need specific models. In the following example we take the IListModel which is used by the listBox, dropdownListBox and the breadCrumb control (other controls using models would be the dateNavigator, chart, tableView and tree). To have access to the model in the JSP, the get and set method in the servlet has to follow the naming convention:

get+MethodName

The MethodName must start with a capital (upper case) letter.

Example

Declaration of the bean with a model

Syntax Syntax

  1.     public class ListBoxBean {
            private IListModel ListBoxmodel;
            // get model method
            public IListModel getMyListBoxmodel() {
                return this.ListBoxmodel;
            }
            .
            .
        }
    
End of the code.

Using the bean in the JSP with a listBox

Syntax Syntax

  1.   .
      .
      <%-- Declare Bean --%>
      <jsp:useBean id="myBean" scope="application" class="bean.ListBoxBean" />
      <%-- Use Bean in model attribute. We use the id myBean (from the  
           useBean statement above) and the name of the get method without 
           the get --%>
    
      <hbj:listBox
          id="LB_Pick"
          width="100"
          size="5"
          selection="1"
          model="myBean.myListBoxmodel"
      />
      .
      .
    
End of the code.
Declaration of a Bean in the JSP

The bean has to be declared at the beginning of the JSP.

Syntax Syntax

  1.   <%-- Declare Bean --%>
      <jsp:useBean id="myBean" scope="application" class="bean.ListBoxBean" />
    
End of the code.
Attributes

Attribute

M

Values

Description

Usage

class

*

String (cs)

Defines the class name of the bean.

class="bean.myBean"

id

*

String (cs)

Id of the bean in the JSP. The id references the bean in the JSP.

id = "myBean"

scope

*

APPLICATION

SESSION

REQUEST

PAGE

Defines the scope in which the bean can be accessed.

scope = "application"

Scope in detail

Except the scope option APPLICATION all the other scope options follow the JSP specifications from Sun Microsystems. The option APPLICATION had to be modified to meet the requirements for a portal. The standard recommendation of APPLICATION would allow access to a the bean through out the whole portal (it would be located in the "Web Application" shell if you look at the following overview chart). In the portal the sphere for APPLICATION is defined as the portal component. This gives the portal component control over the bean but the bean cannot be accessed by other users or other applications of the same user.

Overview

This graphic is explained in the accompanying text.

The overview chart shows the location of the bean in the portal according to the scope attribute. The scope attribute also controls the access of servlet and JSP to the bean.

Scope = Application

This graphic is explained in the accompanying text.

The bean is "inside" the portal component. JSP and servlet have read and write access to the bean.

Accessing the bean

Syntax Syntax

  1. Get a value:
       Object value = 
            request.getComponentContext().getProfile().getValue(String key);
    Put/set a value:
       request.getComponentContext().getProfile().putValue
                                                 (String key, Object value);
    
End of the code.
Scope = Session

The bean is "outside" the portal component. JSP and servlet have read and write access to the bean.

Accessing the bean

Syntax Syntax

  1. HTTPServlet:
    Get a value:
       Object value = request.getSession().getValue(String key);
    Put/set a value:
       request.getSession().putValue(String key, Object value);
    
    Portal:
    Get a value:
       Object value = componentRequest.getComponentSession().getValue
                                                           (String key);
    Put/set a value:
       componentRequest.getComponentSession().putValue
                                               (String key, Object value);
    
End of the code.
Scope = Request

This graphic is explained in the accompanying text.

The bean is "outside" the portal component. JSP and servlet have read and write access to the bean. Because of the location of the bean other portal components in the same request can access the bean as well.

Caution Caution

Be aware that even Portal Components on the same page are rendered in EP in different requests, because they are rendered in separate iFrames on the page! So you can't use in general the Servlet Request to transfer data between Portal Components! To use the ServletRequest makes only sense, if you combine two portal components in one request using the portal object model.

End of the caution.

This scope requires a careful selection of the bean name.

Example Example

Portal Component 1: Uses bean "myBean" with getName and setName methods.

Portal Component 2: Uses bean "myBean" with getNumber and setNumber methods.

Portal Component 1 is loaded first, than Portal Component 2 is loaded.

Result:

This would cause access error message, when Portal Component 2 uses the getNumber or setNumber; the bean "myBean" with getName and setName is in charge, because Portal Component 1 has been loaded first.

End of the example.

Accessing the bean

Syntax Syntax

  1. HTTPServlet:
    Get a value:
       Object value = request.getAttribute (String key);
    Put/set a value:
       request.setAttribute (String key, Object value);
    
    Portal:
    Get a value:
       Object value = componentRequest.getServletRequest().getAttribute
                                                           (String key);
    Put/set a value:
       componentRequest.getServletRequest().setAttribute
                                               (String key, Object value);
    
End of the code.
Scope = Page

This graphic is explained in the accompanying text.

The bean is "inside" the portal component. Only the JSP has read and write access to the bean.

Accessing the bean

Syntax Syntax

  1. Get a value:
       Object value = pagecontext.getValue (String key);
    Put/set a value:
       pagecontext.putValue (String key, Object value);
    
End of the code.