0% found this document useful (0 votes)
98 views

Attributes and Usage of Jsp:Usebean Action Tag

The document discusses the jsp:useBean, jsp:setProperty, and jsp:getProperty tags in JSP. The jsp:useBean tag is used to locate or instantiate a bean class. The jsp:setProperty tag sets property values in a bean using setter methods. The jsp:getProperty tag returns property values from a bean using getter methods. Simple examples are provided to demonstrate using these tags to access and modify properties of a Calculator bean class from a JSP page. More complex examples illustrate setting multiple properties, retrieving values, and reusing beans across multiple pages.

Uploaded by

karunakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Attributes and Usage of Jsp:Usebean Action Tag

The document discusses the jsp:useBean, jsp:setProperty, and jsp:getProperty tags in JSP. The jsp:useBean tag is used to locate or instantiate a bean class. The jsp:setProperty tag sets property values in a bean using setter methods. The jsp:getProperty tag returns property values from a bean using getter methods. Simple examples are provided to demonstrate using these tags to access and modify properties of a Calculator bean class from a JSP page. More complex examples illustrate setting multiple properties, retrieving values, and reusing beans across multiple pages.

Uploaded by

karunakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

next>><<prev

jsp:useBean action tag


1. jsp:useBean action tag
2. Syntax of jsp:useBean action tag
3. Attributes and Usage of jsp:useBean action tag
4. Simple example of jsp:useBean action tag

The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of
the Bean class is already created, it doesn't create the bean depending on the scope. But
if object of bean is not created, it instantiates the bean.

Syntax of jsp:useBean action tag

1. <jsp:useBean id= "instanceName" scope= "page | request | session | application"   
2. class= "packageName.className" type= "packageName.className"  
3. beanName="packageName.className | <%= expression >" >  
4. </jsp:useBean>  

Attributes and Usage of jsp:useBean action tag

1. id: is used to identify the bean in the specified scope.


2. scope: represents the scope of the bean. It may be page, request, session or
application. The default scope is page.
o page: specifies that you can use this bean within the JSP page. The
default scope is page.
o request: specifies that you can use this bean from any JSP page that
processes the same request. It has wider scope than page.
o session: specifies that you can use this bean from any JSP page in the
same session whether processes the same request or not. It has wider
scope than request.
o application: specifies that you can use this bean from any JSP page in the
same application. It has wider scope than session.
3. class: instantiates the specified bean class (i.e. creates an object of the bean
class) but it must have no-arg or no constructor and must not be abstract.
4. type: provides the bean a data type if the bean already exists in the scope. It is
mainly used with class or beanName attribute. If you use it without class or
beanName, no bean is instantiated.
5. beanName: instantiates the bean using the java.beans.Beans.instantiate()
method.

Simple example of jsp:useBean action tag


In this example, we are simply invoking the method of the Bean class.

For the example of setProperty, getProperty and useBean tags, visit next page.

Calculator.java (a simple Bean class)

1. package com.javatpoint;  
2. public class Calculator{  
3.   
4. public int cube(int n){return n*n*n;}  
5.   
6. }  

index.jsp file

1. <jsp:useBean id="obj" class="com.javatpoint.Calculator"/>  
2.   
3. <%  
4. int m=obj.cube(5);  
5. out.print("cube of 5 is "+m);  
6. %>  

download this example


jsp:setProperty and jsp:getProperty
action tags
1. jsp:setProperty and jsp:getProperty action tags
2. Syntax of jsp:setProperty action tag
3. Example of jsp:setProperty
4. jsp:getProperty action tag
5. Syntax of jsp:getProperty action tag
6. Example of jsp:getProperty action tag
7. Example of bean development in JSP

The setProperty and getProperty action tags are used for developing web application with
Java Bean. In web devlopment, bean class is mostly used because it is a reusable software
component that represents data.

The jsp:setProperty action tag sets a property value or values in a bean using the setter
method.

Syntax of jsp:setProperty action tag

1. <jsp:setProperty name="instanceOfBean" property= "*"   |   
2. property="propertyName" param="parameterName"  |   
3. property="propertyName" value="{ string | <%= expression %>}"   
4. />  

Example of jsp:setProperty action tag if you have to set all the


values of incoming request in the bean

1. <jsp:setProperty name="bean" property="*" />  

Example of jsp:setProperty action tag if you have to set value of


the incoming specific property

1. <jsp:setProperty name="bean" property="username" />   

Example of jsp:setProperty action tag if you have to set a


specific value in the property

1. <jsp:setProperty name="bean" property="username" value="Kumar" />  

jsp:getProperty action tag


The jsp:getProperty action tag returns the value of the property.

Syntax of jsp:getProperty action tag

1. <jsp:getProperty name="instanceOfBean" property="propertyName" />  

Simple example of jsp:getProperty action tag

1. <jsp:getProperty name="obj" property="name" />  

Example of bean development in JSP


In this example there are 3 pages:

o index.html for input of values


o welocme.jsp file that sets the incoming values to the bean object and prints the one
value
o User.java bean class that have setter and getter methods

index.html

1. <form action="process.jsp" method="post">  
2. Name:<input type="text" name="name"><br>  
3. Password:<input type="password" name="password"><br>  
4. Email:<input type="text" name="email"><br>  
5. <input type="submit" value="register">  
6. </form>  

process.jsp

1. <jsp:useBean id="u" class="org.sssit.User"></jsp:useBean>  
2. <jsp:setProperty property="*" name="u"/>  
3.   
4. Record:<br>  
5. <jsp:getProperty property="name" name="u"/><br>  
6. <jsp:getProperty property="password" name="u"/><br>  
7. <jsp:getProperty property="email" name="u" /><br>  

User.java

1. package org.sssit;  
2.   
3. public class User {  
4. private String name,password,email;  
5. //setters and getters  
6. }  

download this example


 

Reusing Bean in Multiple Jsp Pages


Let's see the simple example, that prints the data of bean object in two jsp pages.

index.jsp

Same as above.

User.java

Same as above.
process.jsp

1. <jsp:useBean id="u" class="org.sssit.User" scope="session"></jsp:useBean>  
2. <jsp:setProperty property="*" name="u"/>  
3.   
4. Record:<br>  
5. <jsp:getProperty property="name" name="u"/><br>  
6. <jsp:getProperty property="password" name="u"/><br>  
7. <jsp:getProperty property="email" name="u" /><br>  
8.   
9. <a href="second.jsp">Visit Page</a>  

second.jsp

1. <jsp:useBean id="u" class="org.sssit.User" scope="session"></jsp:useBean>  
2. Record:<br>  
3. <jsp:getProperty property="name" name="u"/><br>  
4. <jsp:getProperty property="password" name="u"/><br>  
5. <jsp:getProperty property="email" name="u" /><br>  

Using variable value in setProperty tag


In some case, you may get some value from the database, that is to be set in the bean
object, in such case, you need to use expression tag. For example:

process.jsp

1. <jsp:useBean id="u" class="org.sssit.User"></jsp:useBean>  
2. <%  
3. String name="arjun";  
4. %>  
5. <jsp:setProperty property="name" name="u" value="<%=name %>"/>  
6.   
7. Record:<br>  
8. <jsp:getProperty property="name" name="u"/><br>  

You might also like