Jsp Tutorial Home

Jsp Home

JSP Resources

Community

Site

The following example will demonstrate the various bean scopes for the JSP application. All the examples will use the Counter bean which has only one property counter of type int .


(1) Page Scope


Note:- This is the default scope for the bean object. So in the following JSP example "bean1" is accessed only in this page and not anywhere else.



<HTML>
<BODY>
<H1>
Using Beans and Page Scope</H1>
<jsp:useBean
id="bean1" class="com.visualbuilder.Counter" scope="page" />
    <%
bean1.setCounter(bean1.getCounter() + 1);%>
    The counter value is: <jsp:getProperty name="bean1" property="counter" />
</BODY>
</HTML>


(1) Request Scope


index.jsp


<jsp:useBean id="counter" scope="request" class="com.visualbuilder.Counter" />
<html>
<head>
  <title>
Request Bean Example</title>
</head>
<body>
<%
   
counter.setCounter(10);
%>
<jsp:forward
page="request.jsp" />
</body>
</html>


request.jsp

<jsp:useBean id="counter" scope="request" class="com.visualbuilder.Counter" />

<html>
<body>
  <H3>
Request Bean Example</H3>
  <center><b>
The current count for the counter bean is: </b>
  <%=
counter.getCounter() %></center>
</body>
</html>


(3) Session Scope


Example for Session Scope.


<HTML>
<BODY>
  <jsp:useBean id="bean1" class="com.visualbuilder.Counter" scope="session" />
  <%bean1.
setCounter(bean1.getCounter() + 1);%>
  The counter value is:
  <jsp:getProperty
name="bean1" property="counter" />
</BODY>
</HTML>



(3) Application Scope


<jsp:useBean id="counter" scope="application" class="com.visualbuilder.Counter" />


<html>
<body>
<center>
&nbsp;&nbsp;<b>
The current count for the counter bean is: </b> <%=counter.getCounter() %>
</center>
</body>
</html>


 

                    

Copyright © 2013 VisualBuilder. All rights reserved