The scope values for tag with example

The scope values for tag with example

The <jsp:useBean> tag in (JavaServer Pages) is used to declare a JavaBean for use in a JSP page. The scope of the bean determines its lifetime and visibility. The scope values for the <jsp:useBean> tag are:

1. Page

The bean is accessible only within the current JSP page. This is the default scope.

2. Request

The bean is accessible from any JSP page that processes the same request.

3. Session

The bean is accessible across multiple requests within the same user session.

4. Application

The bean is accessible across the entire application, from any JSP page.

scope values

page Scope
Example for Understanding
  1.  page Scope 
The bean is available only in the current page.

jsp
<jsp:useBean id="bean1" class="com.example.MyBean" scope="page" />
<jsp:getProperty name="bean1" property="property1" />

In this example, bean1 is accessible only within the current JSP page.

request Scope
2.  request Scope 
The bean is available for the duration of the request.

jsp
<jsp:useBean id="bean2" class="com.example.MyBean" scope="request" />
<jsp:getProperty name="bean2" property="property1" />

In this example, bean2 is accessible to any JSP that processes the same request.

session Scope
3.  session Scope 
The bean is available for the duration of the user session.

jsp
<jsp:useBean id="bean3" class="com.example.MyBean" scope="session" />
<jsp:getProperty name="bean3" property="property1" />

In this example, bean3 is accessible to any JSP that is part of the same user session.

application Scope
4.  application Scope 
The bean is available for the entire application.

jsp
<jsp:useBean id="bean4" class="com.example.MyBean" scope="application" />
<jsp:getProperty name="bean4" property="property1" />

In this example, bean4 is accessible to any JSP page in the entire application.

Homepage

Readmore