Listing 1: Course - Java - Java Bean
Listing 1: Course - Java - Java Bean
A java bean is a simple java component which should satisfy the below mentioned points.
A java bean should not have any public variables. All the variables should be accessed using the
getter/setter methods.
Java bean constructor should be a no argument constructor. To meet this requirement better leave the
file without creating any constructor with arguments or create a no argument constructor explicitly.
Below code fragment is an example of a basic bean.
package com.javaBeans;
Following JSP standard actions embed the Java bean in a JSP file.
<jsp:useBean>
<jsp:getProperty>
<jsp:setProperty>
Load Java bean inside a JSP:
To start working with java beans inside a jsppage , first the bean should be loaded into the page. Once
the bean is loaded , the variable properties of the bean can be accessed.
Hence to load a bean the standard action is used. The basic syntax of the action is as follows:
Writing above syntax in a JSP page creates an object referencing to the class “Course” and the name of
the object is “course1”.
<jsp:useBean> has some other attributes which provides additional benefits when creating a bean
object.
<jsp:useBean> “scope” attribute allows the bean object to be sharable across the application.
Depending on the values of the scope attribute , if the bean is shared and has the same id and scope on
the other page, the same bean object is associated to the other jsp page. The property values also
persist the same when the same object is associated in different pages .
If the bean is not sharable or the id and scope are different <jsp:useBean> instantiates a new object of
the class .
After the bean gets loaded into the page, the properties can be accessed using the following standard
actions.
<jsp:getProperty>
<jsp:setProperty>
<jsp:getProperty>
This standard action accesses a property of the bean to get the value and put inside a jsp page.
The attribute name in the above syntax represents the object created using the action. The value of the
name attribute of the <jsp:getProperty> and the id attribute of the <jsp:useBean> property should be
same to refer to the object created.
The attribute property holds the name of the any variable of the bean loaded.
To get all the properties of the bean inside the jsp page the syntax of the <jsp:getProperty> should be
Step 1:
Listing 2: Initializing the variables "title" and "code" of the java bean "Course.java"
package com.javaBeans;
}
Step 2:
Listing 3 : getPropertyEg.jsp - Getting the properties of the bean using the <jsp:getProperty>
<jsp:setProperty>
To modify (or) assign value to any variable of the bean object standard action is used . The basic syntax
is as below
The attributes “name” and “property” carry the same functionality as similar to the <jsp:getProperty> .
The additional attribute “value” holds some data which should be assigned to the property.
Let us look into the example on how to use <jsp:setProperty> to assign any value to the bean properties.
Step 1:
Till now we have studied about setting the bean properties assigning some string values to the "value"
attribute.
The bean properties can also be set using the request parameter i;e, the parameter values of the
obtained from the requesting page. Below syntax shows about setting the parameter using request
parameter.
<jsp:setParameter name="course"
property="title"
value= '<% = request.getParameter("title") %>' />
But "request.getParameter" always returns a string object. If any property of the Java bean has a data
type other than string, data type conversion should be done explicitly.
Let us assume that we have an additional parameter "numberOfStudents" in the Course.java whose
data type is integer.
To set the value of the property "numberOfStudents" using the request parameter following type
conversion should be done explicitly.
<%
int studentNumber = 0;
try {
studentNumber =
Integer.parseInt(request.getParameter("numberOfStudents"));
} catch(NumberFormatException nfe) {}
%>
<jsp:setProperty name="course"
property=" numberOfStudents "
value= '<% = request.getParameter("studentNumber ") %>' />
Instead of doing all the above coding <jsp:setProperty> has an additional attribute called as "PARAM" .
This attribute takes the name of the request parameter and does the conversion automatically without
any explicit conversion.
<jsp:setProperty name="course"
property=" numberOfStudents "
param="studentNumber" />
From the above syntax "numberOfStudents" property of the bean is directly set to the value of the
request parameter "studentNumber" .
Step 1:
Modify the existing bean by adding an additional variable whose data type is an "int".
Generate getter/setter methods for the new variable added.
Listing 5: Course.java - Added a new variable "numberOfStudents" which holds a value of number of
students taken the course.
package com.javaBeans;
Step 2:
Redirected to the page paramEg.jsp page after entering details in courseForm.jsp and clicking on the
submit button
Figure 4: Redirected to the page paramEg.jsp page after entering details in courseForm.jsp and clicking
on the submit button. Values printed in the resulting page are coming from courseForm.jsp as a request
parameters and set to the bean using PARAM attribute.
We have seen using java beans in jsp page till now. According to the basic syntax , <jsp:useBean> loads
and instantiates the bean object and this object to visible only to the page in which it is created.But in
real time scenario, we might need an object to persist either for a whole session or per each user
request etc.
This requirement is handled by the scope attribute of the standard action <jsp:useBean> .
Scope attribute gives the user a feasibility to change the visibility and accessibility of the object
depending on the requirement.Like in java , variables has scope (local/global) , java beans in jsp also
have the scope.
Scope is an attribute of the jsp standard action <jsp:useBean> . When declaring the bean object itself we
mention the scope of the object.
Page
Request
Session
Application
At the time of the bean creation , the container creates a new reference of the object only if the scope is
local. If the bean already exists , container checks to see if the new bean has the same id and scope as
that of the existing bean. If both have the same id and scope values then the container uses the existing
bean reference. Otherwise creates the new reference.
This is the default scope. As you have seen in our previous examples , scope is not mentioned anywhere.
In such scenarios where no scope value is mentioned, the default value “page” is assigned to the scope
attribute.
This scope restricts the object’s visibility to the current processed page. i.e; the object is accessible to
<jsp:getProperty> , <jsp:setProperty>, scriptlets or expressions coded in that particular page alone.
The object’s reference is diminished once the page is processed and is not accessible to any other JSP or
servlets under any circumstances.
Request:
This scope refers to the next level objects visibility ie; the object can be accessed across the page with in
the same request .
The objects reference is available to the JSP or servlet that is called by jsp forward or include or request
dispatcher object.
The object’s reference is available even though the user navigates through different pages .
But once the session is invalidated the object’s references are removed and are not available to the
user.
This scope should not be used until and unless it is really necessary according to the requirements, as
any sensitive data might be visible to all other users.
For example, let us think of a bean object which holds the telephone number of the user has an
application scope. Since the object’s reference is accessible throughout the application , whenever the
bean is populated with the telephone number , the data is published to all the users , which is not
permissible in real time scenario.
Archit Sethi
Have done masters in computer and have done various courses on java, PHP, android
Sanjay Vekariya
This is nice tutorial. i like it. have you a EJB example or tutorial?
[+1 year ago] Answer it