Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Using Beans And Session Scope 
 

The scope in which the Bean exists and the variable named in id is available.

 

Using Beans And Session 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. In jsp we have been provided with the four scope of a bean.

session: In this the bean will get stored in the HttpSession object associated with the current session.

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 current session. 

The code of the program is given below:

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

 

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

The output of the program is given below:

Download this example.

                          

» View all related tutorials
Related Tags: java c web com ide session data application io module copy help sessions sed output vi value using this id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.