Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Related Tutorials


 
 

Share on Google+Share on Google+

in JSP

Advertisement
Bean is a reusable component which mostly contains the setter and getter values, we also called it as mutators.

<jsp:useBean> in JSP

        

Syntax:  <jsp:useBean id= "nameOfInstance" scope= "page | request | session | application" class= "package.class" type= "package.class > </jsp:useBean>.

Bean is a reusable component which mostly contains the setter and getter values, we also called it as mutators.

The <jsp:useBean> is a standard action element used to locate or instantiates a JavaBeans component. Firstly <jsp: useBean> tries to locate an instance of the Bean class if found its fine, if not then it will instantiates it from a class. The name of the bean is same as we have given in id attribute of <jsp:useBean>. If the object reference doesn't exist with the name we have specify  then it will create a instance and find the scope of the variable, class attributes defines the bean class and type attribute defines the parent class or interface of the Bean class.

The body of a <jsp: useBean> action often contains<jsp:setProperty> elements that sets the property values in the Bean class. The child tags of the <jsp:useBean> will only processed if the <jsp:useBean> instantiates the Bean. The child tags of <jsp:useBean> are:

<jsp:setProperty name = "nameOfBeanInstance" property="*"  | propertyName ("parameterName")  | value=string | <%= expression%> >

<jsp:getProperty name="nameOfBeanInstance" property="propertyName"/>

Scope of <jsp:useBean>

1. page: It means that we can use the Bean within the JSP page.

2. request: It means that we can use the Bean from any JSP page processing the same request.

3. session: It means that we use the Bean from any Jsp page in the same session as the JSP page that created the Bean.

4. application: It means that we use the Bean from any page in the same application as the Jsp page that created the Bean.

The code of the program is given below:

//MyBean.java
   public class MyBean {
        // Initialize with random values
        int prop1 = (int)(Integer.MAX_VALUE*Math.random());
        String prop2 = ""+Math.random();
    
            public int getProp1() {
            return prop1;
        }
        public void setProp1(int prop1) {
            this.prop1 = prop1;
        }
    
            public String getProp2() {
            return prop2;
        }
        public void setProp2(String prop2) {
            this.prop2 = prop2;
        }
    }
    
        //UseBean.jsp
    <jsp:useBean id="myBean" class="Mybean.MyBean" scope="session" >
                <jsp:setProperty name="myBean" property="name" value=" James" />
				<jsp:setProperty name="myBean" property="address"
 value=" 007,Gali No.2" />
    </jsp:useBean>
      <%--  <jsp:getProperty name="myBean" property="name" />
    <jsp:getProperty name="myBean" property="address" /> --%>
	The name is<%= myBean.getName()%> <br>
	The address is<%= myBean.getAddress() %>

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: June 15, 2009

Related Tutorials

Discuss: in JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
sreekar
November 3, 2011
Jsp

Showing error that get name method not exists in (UseBean.jsp)jsp file...
sreekar
November 3, 2011
Jsp

Error in JSP file....
pugalenthi
March 4, 2013
super

use ful the notes I'am writing the assignment and use ful the gatharing more knowlege
Nirav
February 18, 2012
Upper Code Will Give Error..

Will Give Error!!!
Sessel
March 12, 2012
Very good example

Everything what i searched for!
Suresh Kumar
July 26, 2012
Doubt

in jsp:useBean where the Mybean(i think package name ) come from? you did not mention it that is why i have doubt. My example is not running: error:/ExLang.jsp(12,0) The value for the useBean class attribute Person is invalid please tell me.
DMCA.com