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.
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.
Declaration of the bean with a model
public class ListBoxBean { |
Using the bean in the JSP with a listBox
. |
The bean has to be declared at the beginning of the JSP.
<%-- Declare Bean --%> |
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 |
Defines the scope in which the bean can be accessed. |
scope = "application" |
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.
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.
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); |
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); |
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.
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.
This scope requires a careful selection of the bean name.
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.
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); |
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); |