JSP Standard Action 'jsp:useBean'


 

JSP Standard Action 'jsp:useBean'

In this Section, we will discuss about some standard actions :"jsp:usebean" with a small example.

In this Section, we will discuss about some standard actions :"jsp:usebean" with a small example.

JSP Standard Action <jsp:useBean>

This action let you use java bean in a JSP page. This action instantiates or locates a Bean with a specific name and scope.

Syntax :

<jsp:useBean
        id="beanInstanceName"
        scope="page | request | session | application"
        {
            class="package.class" |
            type="package.class" |
            class="package.class" type="package.class" |
            beanName="{package.class | <%= expression %>}" type="package.class"
        }
        {
            /> |
            > other elements </jsp:useBean>
        }


Example :    <jsp:useBean id="test" class="foo.usebeanexample" />      
                           
<jsp:setProperty name="test" property="message" value="Hello ANKIT" />

This usually means "instantiate an object of the class specified by class, and bind it to a variable with the name specified by id".

Attributes and Usage

  • id="beanInstanceName

Gives a name to the variable that will reference the bean. A previous bean object is used instead of instantiating a new one if one can be found with the same id and scope.

  • scope="page | request | session | application"

Indicates the context in which the bean should be made available. There are four possible values:

  • page :The default, page, indicates that the bean is only available on the current page (stored in the PageContext of the current page).
  • request :A value of requestindicates that the bean is only available for the current client request (stored in the ServletRequest object).
  • session :A value of session indicates that the object is available to all pages during the life of the current HttpSession.
  • application : a value of application indicates that it is available to all pages that share the same ServletContext.
  • class="package.class" : Designates the full package name of the bean.
  • beanName="{package.class | <%= expression %>}" type="package.class" :
Instantiates a Bean from either a class or a serialized template, using the 'ava.beans.Beans.instantiate' method, and gives the Bean the type specified in type. The value of 'beanName 'is either a package and 'class name' or an 'Expression' that evaluates to a package and class name, and is passed to 'Beans.instantiate'. The value of type can be the same as 'beanName', a 'superclass' of 'beanName', or an interface implemented by 'beanName'.

Example : Given below a example that loads a bean and sets/gets a simple String parameter :
usebean.jsp

<HTML>

<head>

<TITLE>JavaBeans in JSP</TITLE>

<meta http-equiv="Content-Language" content="en-us">

<style type="text/css">

.style1 {

border: 2px solid #0000FF;

background-color: #CCFFFF;

}

</style>

</head>

<BODY>

<table class="style1" style="float: CENTER" align="center">

<tr>

<td style="width: 430px; height: 38px">

<h3><strong>EXAMPLE OF JSP:USEBEAN STANDARD ACTION</strong></h3>

</td>

</tr>

</table>

<P>

<jsp:useBean id="test" class="foo.usebeanexample" />

<jsp:setProperty name="test" property="message" value="Hello ANKIT" />

<center><H1>Message:

<jsp:getProperty name="test" property="message" />

0

</H1></center>

</BODY>

</HTML>

1

Given below is the code for the bean used in the "usebean.jsp" :

usebeanexample.java

package foo;

2

public class usebeanexample {

private String message = "No message specified";

3

public String getMessage() {

return(message);}

4

public void setMessage(String message) {

this.message = message;}

}

5

Output of  "usebean.jsp"




Download Source code

See Also :(click below)

JSP standard Actions :<jsp:setProperty> & <jsp:getProperty>

Ads