Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : J2EE : JSP : Calling JavaBeans from a JSP Page
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

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 :

  • id - name of the object e.g.
    	String name = null;
    In the above code, name is the 'id' we are talking about.
  • scope - an optional attribute by which you can control when your JavaBean object will be destroyed. Default is page, which means every page view will create a new JavaBean.
  • type - type of the object which can be the same class, a super class or an interface which the class implements. This parameter is optional. e.g.
    	String name = "Faisal Khan";
    In the above code, String is the 'type' we are talking about.
  • class - a fully qualified class name e.g.
    	Date d = new java.util.Date();
    In the above code, java.util.Date is the 'class' we are talking about.
  • beanName - it is also fully qualified class name just like 'class' attribute above. Only difference is that the class name in the case of 'beanName' can be provided at request time.

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?


 ( 1 Remaining ) Next

Comments/Questions ( Threads: 9, Comments: 10 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Need urgent help...
  2. sorry not Servlet -- JavaBeans--
  3. hi i followed all steps in calling Servlet from jsp but i've got error ( 1 Reply )
  4. java beans & jsp
  5. Java Beans, Calling from JSP
  6. JSP
  7. ok now , I understand , and I badly want a bean example to handle the dabatase
  8. THE ENVIRONMENT SETTINGS ( 1 Reply )

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.