Home Jsp Simple-jsp-example Using Beans And Application Scope
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Using Beans And Application Scope

Advertisement
The scope in which the Bean exists and the variable named in id is available. The default value of scope is page. We use the scope attribute to specify additional places where bean is stored.

Using Beans And Application Scope

        

The scope in which the Bean exists and the variable named in id is available. The  default value of scope is page. We use the scope attribute to specify additional places where bean is stored. 

application: In this the bean will get stored in the ServletContextServletContext is shared by all servlets in the same web application.

In this example we are making one bean class inside which we are declaring one variable counter of type int. Inside this class declare one setter and getter method. Now make one jsp page inside which declare one standard tag <jsp:useBean> which is used to build the object of the bean class. To set the the value of the setter method of the bean class in the jsp use a scriptlet directive. Inside this directive call the setCounter() defined in the bean class by the reference of the bean class. At last to get the value which we have set in the setter method call the method getCounter() defined in the bean class by the reference of the bean class. This method will return how many times the page has been visited in the particular request.

The code of the program is given below:

 

package Mybean;
public class UsingBeanScopeApplication{
  private static int counter = 0;
  public void setCounter(int counter) {
    this.counter = counter;
  }
  public int getCounter() {
    return counter;
  }
}

 

<html>
  <head>
  <title>Using Beans and Application Scope</title>
  </head>
  <body>
  <h1>Using Beans and Application Scope</h1>
  <jsp:useBean id="applicationScopeBean" class="Mybean.UsingBeanScopeApplication" scope="application" />
  <% 
  applicationScopeBean.setCounter(applicationScopeBean.getCounter()+1);
  %>
Counter value is <%= applicationScopeBean.getCounter() %>
  </body>
</html>

The output of the program is given below:

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 11, 2008

Related Tutorials

Ask Questions?    Discuss: Using Beans And Application Scope  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com