|
Calling JavaBeans from a JSP Page by Faisal Khan.
Overview
In the earlier article, What are JavaBeans?,
we learned what are JavaBeans and we even created a simple JavaBean class file;
SimpleBean.
We will be using this SimpleBean class in this tutorial, so if you haven't read above
article then I suggest you do it now.
Before we continue to describe how to code a JSP page to call that JavaBean, let's
first discuss the three JSP tags provided to us to make use of JavaBeans.
JSP JavaBean Tags
Following are the three tags :
- <jsp:useBean>
- <jsp:setProperty>
- <jsp:getProperty>
Let us now study them one by one.
i. <jsp:useBean>
This tag is used to declare and instantiate the JavaBean class. It's syntax is as
follows :
<jsp:useBean
id="object-name"
scope="page | request | session | application"
type="type-of-object"
class="fully-qualified-classname"
beanName="fully-qualified-beanName"
/>
Let us now see what are the different attributes :
ii. <jsp:setProperty>
This tag is used to set the value of one or all the properties of given JavaBean.
It's syntax is as follows :
<jsp:setProperty
name="id-of-the-JavaBean"
property="name-of-property"
param="name-of-request-parameter-to-use"
value="new-value-of-this-property"
/>
Let us now see what the different attributes mean :
- name - 'id' of the <jsp:useBean> tag you set above.
- property - name of the property whose value you want to set.
- param - name of the request paramter you want to use to set the value of this
property.
- value - the new value you want to set for this property.
iii. <jsp:getProperty>
This tag is used to retrieve the value of a given property from the given JavaBean.
It's syntax is as follows :
<jsp:getProperty
name="name-of-the-object"
property="name-of-property"
/>
Let us see what are the different attributes for this tag :
- name - 'id' of the <jsp:useBean> tag we set above.
- property - name of the property whose value you want to retrieve.
We are now finished studying the JSP tags provided to manipulate JavaBeans. Let's
just spend a few minutes studying what is scope attribute we discussed in <jsp:useBean>
tag.
Object Scope
Every JavaBean class object or any other class object we create will have a scope.
Scope means the length of time this object will remain in memory. There are four kinds
of scopes :
- page - it means a new object will be created and destroyed for every page view. This
is the default for <jsp:useBean> tag when you don't explicitly give it any.
- request - it means the newly created object will be created and bound to the request
object. It is used in different JSP architectures we will study later when we have covered
all the basic topics of JSP pages.
- session - the newly created object will be bound to the session object. What this means
is that every visitor coming to your site will have a separate session for it, so you
will not have to create a new object every time for it. You can just retrieve that object
later again from the session object when you want it.
- application - an object bound to application object means that your object will stay
as long as the application remains loaded. This can be useful when for instance you want
to count page views or daily sessions for your site.
Ok, we have now studies all the three tags for JavaBeans manipulation and also studied briefly
what are the different object scopes and what they mean to us.
Let us now move forward on the next page where we code our SimpleBean.jsp JSP page
which will call this JavaBean, set new values for it's parameters and then display the
values of these parameters to the user.
SimpleBean.jsp JSP page
Create a new SimpleBean.jsp page and place it in the /WEB-APP folder. WEB-APP is
the complete path to your web application e.g. C:\yoursite, then place SimpleBean.jsp
page in C:\yoursite\SimpleBean.jsp. Remember, never put JSP pages in /WEB-INF/ folder.
Now copy the following code and paste it into the SimpleBean.jsp page above :
<html>
<head>
<title>SimpleBean Test Page</title>
</head>
<body>
<%-- Creating JavaBeans --%>
<jsp:useBean id="simple" class="com.stardeveloper.bean.test.SimpleBean">
<jsp:setProperty name="simple" property="name" value="Faisal Khan" />
<jsp:setProperty name="simple" property="age" value="24" />
</jsp:useBean>
<%-- Displaying JavaBean property's value --%>
<p>Name retrieved from JavaBean has the value of :
<b><jsp:getProperty name="simple" property="name" /></b>.<br>
Age retrieved from JavaBean has the value of :
<b><jsp:getProperty name="simple" property="age" /></b> years.<br>
</p>
</body>
</html>
Explanation
Above code is a simple JSP page which makes use of all the three JavaBean tags we
discussed earlier. First we create a new JavaBean object for our SimpleBean class by
using <jsp:useBean> tag :
<jsp:useBean id="simple" class="com.stardeveloper.bean.test.SimpleBean">
Then we use <jsp:setProperty> tag to set different values for both the name
and age properties of our SimpleBean class.
<jsp:setProperty name="simple" property="name" value="Faisal Khan" />
<jsp:setProperty name="simple" property="age" value="24" />
We then close the <jsp:useBean> tag.
</jsp:useBean>
Next we display the values of these properties using the <jsp:getProperty>
tag twice.
<jsp:getProperty name="simple" property="name" /></b>.<br>
<jsp:getProperty name="simple" property="age" /></b>.<br>
All the tags and their attributes are extremely simple and we have already studied
them earlier. See how easy it is to access a JavaBean from a JSP page?
|