jsp:useBean  

A jsp:useBean action is used to associate a JavaBean with the JSP. It ensures that the object is available for the appropriate scope specified in the tag. The bound object can be referenced from the JSP using the id it is associated with (or even from other JSPs, depending on the scope).

Internally, the container first tries to locate the object using the id and scope and, if it is not found, uses the name of the bean as an argument to the instantiate() method of the java.beans.Beans using the current class loader. (If this instantiation fails, a request-time exception due to the instantiate() is thrown.)

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

Where beandetails is one of:

  • class="className"
  • class="className" type="typeName"
  • beanName="beanName" type="typeName"
  • type="typeName"
    <jsp:useBean id="mparam" scope="session" class="java.lang.String">
    This is used to initialize the bean...
    </jsp:useBean>
id  
   
 

The case sensitive name used to identify the object instance.

scope  
Default: page  
 

The scope within which the reference is available.

Scope in tag

Description

page

The object reference is discarded upon completion of the current Servlet.service() invocation.

The servlet engine, when generating the servlet, creates an object in the service() method, which follows the usual object scope convention in Java.

The object exists for every client request to the resource.

request

The object reference is available as long as the HttpRequest object is not discarded, even if the request is passed/chained to different pages.

The underlying generated servlet relies on binding the object to the HttpServletRequest using the setAttribute(String key,Object value) method in the HttpServletRequest

This however is transparent to the user.

The object is distinct for every client request.

session

In the pre Servlet 2.1 specifications, a session always ran under a session context. (Though deprecated in Servlet 2.1, an implementation for an HttpSessionContext is still available in most servers.)

The underlying generated servlet relies on binding the object to the HttpSession using the putValue(String key,Object value) method in the HttpServletSession.

This however is transparent to the user.

The object is distinct for every client, and is available as long as the client's session is valid.

application

This is the most persistent.

The underlying generated servlet relies on binding the object to the ServletContext using the setAttribute(String key,Object value) method in the ServletContext.

This is not unique for clients, and consequently all clients access the same object.

class  
   
 

The fully qualified class name.

beanName  
   
 

The name of a Bean, as you would supply to the instantiate() method in the java.beans.Beans class. This attribute can also be a request time expression.

It is permissible to supply a type and a beanName, and omit the class attribute.

The beanName follows the standard bean specification and can be of the form "a.b.c", where "a.b.c" is either a class, or the name of a serialized resource in which case it is resolved as "a/b/c.ser" (see the JavaBeans specification).

Type  
Default: Same as the value of the class attribute.  
 

This optional attribute specifies the type of the class, and follows standard Java casting rules. The type must be a superclass, an interface, or the class itself.

Just like any casting operation, if the object is not of this type then java.lang.ClassCastException can be thrown at request time.