1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

JSP Tutorials

Beans scopes in JSP

Scopes are :

Page scope :
This scope helps to keep the data available while the page is loading. Any object whose scope is defined as page scope will disappear as soon as the response is sent to the browser. The object with a page scope may be modified as often as desired within the particular page but the changes are lost as soon as the user moves away from the page. By default all beans have page scope.

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


Request scope:
Any object created in the request scope will be available as long as the request object is valid. For example if the JSP page uses a <jsp:forward> tag, then the bean will be accessed in the forwarded page and if redirect is used then the bean is destroyed.


<jsp:useBean id="test1" scope="request" class="com.techfaq.CounterTest" />
<html>
<head>
<title>Request Bean </title>
</head>
<body>
<%
test1.setCounter(10);
%>
<jsp:forward page="forwrad.jsp" />
</body>
</html>

In the forwrad.jsp ( value of counter is 0)


The Session scope:
In JSP terms, the data associated with the user has session scope. A session does not correspond directly to the user; rather, it corresponds with a particular period of time the user spends at a site. Typically, this period is defined as all the hits a user makes to a website between starting and exiting his browser.

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

In any other JSP also you will get the same value in the same session.


Application Scope:
The bean associated with the application scope will be accessible to all the users and all the pages in the application.

<jsp:useBean id="test1" scope="application" class="com.techfaq.CounterTest" />
<%test1.setCounter(19);%>

Throught all the JSP has the same value 19. This is share to all the user and session in the webapplication.


Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy