Java Reference
In-Depth Information
application Scope
Beans with application scope are accessible within pages processing requests that are in the
same application space as the page in which they were created. References to the object will be
released when the runtime environment reclaims the ServletContext . More simply put, this
means that until the JSP engine is restarted, the bean created with application scope will be
available. Beans with application scope are best used when you need to share information
between JSPs and servlets for the life of your application.
To give an example of application scope, you will use two JSPs. The first will load the
Counter bean using an id of “counter” and a scope of “application” . It will then print out
the current value of the counter bean, using the Counter.getCount() method. Listing 17.7
contains the source for your first JSP.
17
L ISTING 17.7
ApplicationBean1.jsp
<%@ page errorPage=”errorpage.jsp” %>
<!-- Instantiate the Counter bean with an id of “counter” -->
<jsp:useBean id=”counter” scope=”application” class=”Counter” />
<html>
<head>
<title>Application Bean Example 1</title>
</head>
<body>
<H3>Application Bean Example 1</H3>
<center><b>The current count for the counter bean is: </b>
<%=counter.getCount() %></center>
</body>
</html>
The second JSP does exactly the same thing as the first. However, because both beans have an
id of “counter” and “application” scope, the second JSP will find the bean and not have to
create it. Listing 17.8 contains the source for your second JSP.
L ISTING 17.8
ApplicationBean2.jsp
<%@ page errorPage=”errorpage.jsp” %>
<!-- Instantiate the Counter bean with an id of “counter” -->
<jsp:useBean id=”counter” scope=”application” class=”Counter” />
<html>
<head>
<title>Application Bean Example 2</title>
</head>
 
Search WWH ::




Custom Search