Usage & Scopes

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

public class  ListBoxBean{ private  IListModelListBoxmodel; //getmodelmethod public  IListModelgetMyListBoxmodel(){ return this .ListBoxmodel;}..}

Using the bean in the JSP with a listBox

.. <%--DeclareBean--%><jsp:useBeanid= "myBean" scope= "application" class= "bean.ListBoxBean" /><%--UseBeaninmodelattribute.WeusetheidmyBean(fromtheuseBean statementabove)andthenameofthegetmethodwithouttheget--%><hbj:listBoxid= "LB_Pick" width= "100" size= "5" selection= "1" model= "myBean.myListBoxmodel" /> ..

Declaration of a Bean in the JSP

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

<%--DeclareBean--%><jsp:useBeanid= "myBean" scope= "application" class= "bean.ListBoxBean" />

Attributes

Attribute

M

Values

Description

Usage

class

*

String ( c s)

Defines the class name of the bean.

class="bean.myBean"

id

*

String ( c s)

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

id = " myBean "

scope

*

APPLICATIONSESSIONREQUESTPAGE

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



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



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

Accessing the bean

Get a value:

Object value =

request.getComponentContext().getProfile().getValue(String key);

Put/set a value:

request.getComponentContext().getProfile().putValue

(String key, Object value);

Scope = Session

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

Accessing the bean

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);

Scope = Request



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.

This scope requires a careful selection of the bean name.

Accessing the bean

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);

Scope = Page



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

Accessing the bean

Get a value:

Object value = pagecontext.getValue (String key);

Put/set a value:

pagecontext.putValue (String key, Object value);