Signup/Sign In

JSP jsp:useBean Tag

If you want to interact with a JavaBeans component using the Action tag in a JSP page, you must first declare a bean. The <jsp:useBean> is a way of declaring and initializing the actual bean object. By bean we mean JavaBean component object. Syntax of <jsp:useBean> tag

<jsp:useBean id = "beanName" class = "className" 
              scope = "page | request | session | application">

Here the id attribute specifies the name of the bean. Scope attribute specify where the bean is stored. The class attribute specify the fully qualified classname.

useBean Tag in JSP

Given a useBean declaration of following :

<jsp:useBean id="myBean" class="PersonBean" scope="request" />

is equivalent to the following java code,

PersonBean myBean = (PersonBean)request.getAttribute("myBean");
if(myBean == null)
   myBean = new PersonBean();
   request.setAttribute("myBean", myBean);

If jsp:useBean tag is used with a body, the content of the body is only executed if the bean is created. If the bean already exists in the named scope, the body is skipped.

Time for an Example

In this example we will see how <jsp:useBean> standard tag is used to declare and initialize a bean object. We will use PersonBean class as JavaBean Component.


public class PersonBean implements Serializable
 private String name;
  public PersonBean()
   public void setName(String name)
   { = name;
   public String getName()
    return name;


        <title>Welcome Page</title>
    <jsp:useBean id="person" class="PersonBean" scope="request" />
        //Use the bean here...  

Here jsp:useBean declares a "person" bean in the jsp page which can be used there. How to use it, modify it, we will study in coming lessons.