java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips arrow JavaServer Pages arrow How to use a Bean in a JSP page
 
 
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: 4084
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:
 
 
 
How to use a Bean in a JSP page E-mail
User Rating: / 9
PoorBest 

This Java tip illustrates a method of using a Bean in a JSP page. To use a bean in a JSP page, three attributes must be supplied - an id, which provides a local name for the bean, the bean's class name, which is used to instantiate the bean if it does not exit, and a scope, which specifies the lifetime of the bean.

There are four scopes available - page, request, session, and application. A page-scoped bean is available only within the JSP page and is destroyed when the page has finished generating its output for the request. A request-scoped bean is destroyed when the response is sent. A session-scoped bean is destroyed when the session is destroyed. An application-scoped bean is destroyed when the web application is destroyed.

This example retrieves a session-scoped bean and makes it available using the id myBean. The bean is automatically created if it does not exist:

    <jsp:useBean id="myBean" class="com.mycompany.MyBean" scope="session" />

If it is necessary to initialize the bean after it is created, the initialization code can be placed in the body of the jsp:useBean action. The body will not be executed if the bean already exists. Here is an example:

    <jsp:useBean id="myBean2" class="com.mycompany.MyBean" scope="session">
        <%-- this body is executed only if the bean is created --%>
    
        <%-- intialize bean properties --%>
        <jsp:setProperty name="myBean2" property="prop1" value="123" />
    </jsp:useBean>

The value of a bean property can be included in the generated output using the jsp:getProperty action:

    <jsp:getProperty name="myBean" property="prop1" />
    <jsp:getProperty name="myBean" property="prop2" />

The jsp:useBean action also declares a local Java variable to hold the bean object. The name of the local variable is exactly the value of the id attribute. Here is an example that accesses the bean within a scriptlet:

    <%
        if (myBean.getProp1() == 0) {
            out.println("<font color=\"red\">"+myBean.getProp1()+"</font>");
        else {
            out.println("<font color=\"green\">"+myBean.getProp1()+"</font>");
        }
    %>

Here is the bean used in the example:

    package com.mycompany;
    
    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;
        }
    }

 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.