j2ee: Using JavaBeans in JSPs [17/20] Previous pageContentsNext page

Declaring the Bean

<jsp:useBean> action

Locates or instantiates a bean with a specified name and scope.

Syntax:

        <jsp:useBean id="name" scope="page|request|session|application" typeSpec />
        
        typeSpec ::= class="className" |
        class="className" type="typeName" |
        type="typeName" class="className" |
        beanName="beanName" type="typeName" |
        type="typeName" beanName="beanName" |
        type="typeName" 
        

class must be assignable to type if both are declared

If the useBean has a body, it is of the form:

        <jsp:useBean id="name" scope="page|request|session|application" typeSpec >
        body
        </jsp:useBean>
        

The body will be invoked if the Bean is created.

Typically, the body will contain either scriptlets or jsp:setProperty tags that will modify the object created in the useBean tag

The contents of the body are not restricted.

Semantics:

  1. Attempt to locate a bean based on id and scope.
  2. Define a variable with the given id in the current scope of the specified type or class.
  3. If the bean is found in the specified scope
  4. If the bean is not found in the specified scope

Previous pageContentsNext page