java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3946
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Sharing beans between application components E-mail
User Rating: / 3
PoorBest 

Although the beans are indeed bound to local variables, that is not the only behavior. They are also stored in one of four different locations, depending on the value of the optional scope attribute of jsp:useBean. The scope attribute has the following possible values: page, application, session, request.

The sample below shows the usage of a bean in topmost scope - application. The beas is used there to track count of page access. During this the firstPage property of that bean is set in to ways: once the bean is constructed its value is set to initial one; then its value is modified with each page link activation:

Simple bean to illustrate sharing beans through use of the scope attribute of jsp:useBean.

public class AccessCountBean {
  private String firstPage;
  private int accessCount = 1;

  public String getFirstPage() {
    return firstPage;
  }

  public void setFirstPage(String page) {
    firstPage = page;
  }

  public int getAccessCount() {
    accessCount++;
    return accessCount;
  }
}

////////////// here is the code for one of the pages sharing that bean;
////////////// the rest pages will have quite the same contents except 
////////////// their titles:

<%@page language="java"%>

<html>
  <head>
    <title>Shared Access Counts: Page 1</title>
  </head>

  <jsp:useBean id="counter" class="test.AccessCountBean" 
  scope="application">
    <jsp:setProperty name="counter" property="firstPage" 
    value="SharedCounts1.jsp"/>
  </jsp:useBean>
  
  <body>
    <table border=align="center">
      <tr><th>Shared Access Counts: Page 1</th></tr>
    </table>
    
    <p>
      Of SharedCounts1.jsp (this page),
      <a href="SharedCounts2.jsp">SharedCounts2.jsp</a>
      , and
      <a href="SharedCounts3.jsp">SharedCounts3.jsp</a>
      ,
      <jsp:getProperty name="counter" property="firstPage"/>
      was the first page accessed.
    </p>
    <p>
      Collectively, the three pages have been accessed
      <jsp:getProperty name="counter" property="accessCount"/>
      times.
    </p>
  </body>
</html>


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.