Usebean Action in jsp

Chapter 8 - Building JSP Pages Using Standard Actions

    Usebean Action in jsp

  • Action jsp:useBean:

    A jsp:useBean action is an instance of a Java programming language object defined within a given scope and available with a given id with a newly declared scripting variable of the same id.

    When a jsp:useBean action is used in an scriptless page or scriptless context there is no Java scripting variables created but instead an EL variable is created.

    It is also use this to give a local name to an object defined in another JSP page or in a servlet. This can be done by using the type attribute and not providing class or beanName attributes.

    At least ONE of type and class MUST be present, and it is NOT VALID to provide both class and beanName.

    If type and class are present, class must be assignable to type.For it not to be assignable is a translation time error.

    The value of this attribute can be a request-time attribute expression. In the following example, a Bean with name connection of type com.myco.myapp.

    Connection is available after actions on this element, either because it was already created and found, or because it is newly created.

     < jsp:useBean id="connection" class="com.myco.myapp.Connection" / >

    In the next example, the timeout property is set to 33 if the Bean was instantiated:

     < jsp:useBean id="connection" class="com.myco.myapp.Connection" >
    < jsp:setProperty name="connection" property="timeout" value="33" >
    < /jsp:useBean >

    In the following example, the object should have been present in the session. It is given the local name wombat with WombatType.

    A ClassCastException may be raised if the object is of the wrong class, and an InstantiationException may be raised if the object is not defined:


    < 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" |


    < jsp:useBean id="name" scope="page|request|session|application" typeSpec >
    < /jsp:useBean > jsp:useBean attributes:
    Attribute Description
    id The name used to identify the object instance in the specified scope’s namespace, and also the scripting variable name declared and initialized with that object reference. The name specified is case sensitive and shall conform to the current scripting language variable-naming conventions.
    scope The scope within which the reference is available. The DEFAULT value is page. See the description of the scope attribute defined earlier herein. A translation error must occur if scope is not one of page, request, session or application.
    class The fully qualified name of the class that defines the implementation of the object. The class name is case sensitive. If the class and beanName attributes are not specified the object must be present in the given scope.
    beanName The name of a bean, as expected by the instantiate method of the java.beans.Beans class. This attribute can accept a request-time attribute expression as a value.
    type It defines the type of the scripting variable defined. This allows the type of the scripting variable to be distinct from, but related to, the type of the implementation class specified. The type is required to be either the class itself, a superclass of the class, or an interface implemented by the class specified.

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy