The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn’t create the bean depending on the scope. But if object of bean is not created, it instantiates the bean.

Syntax of jsp:useBean action tag

" >  

Attributes and Usage of jsp:useBean action tag

  1. id: is used to identify the bean in the specified scope.
  2. scope: represents the scope of the bean. It may be page, request, session or application. The default scope is page.
    • page: specifies that you can use this bean within the JSP page. The default scope is page.
    • request: specifies that you can use this bean from any JSP page that processes the same request. It has wider scope than page.
    • session: specifies that you can use this bean from any JSP page in the same session whether processes the same request or not. It has wider scope than request.
    • application: specifies that you can use this bean from any JSP page in the same application. It has wider scope than session.
  3. class: instantiates the specified bean class (i.e. creates an object of the bean class) but it must have no-arg or no constructor and must not be abstract.
  4. type: provides the bean a data type if the bean already exists in the scope. It is mainly used with class or beanName attribute. If you use it without class or beanName, no bean is instantiated.
  5. beanName: instantiates the bean using the java.beans.Beans.instantiate() method.

Simple example of jsp:useBean action tag

In this example, we are simply invoking the method of the Bean class.

Calculator.java (a simple Bean class)

package com.javatpoint;  
public class Calculator{  
  
public int cube(int n){return n*n*n;}  
  
}

index.jsp file

  
  
<% int m=obj.cube(5); out.print("cube of 5 is "+m); %>

example of jsp:useBean tag