JSP useBean, setProperty & getProperty

useBean, setProperty & getProperty Action Tags in JSP:

First let’s see the syntax for all these action tags so that we can understand what each tag is doing and how they are related.

useBean Action Tag:

The useBean action is widely used. It first searches for an existing object utilizing the id and scope variables. If an object is not found, it then tries to create the specified object. The useBean tag is a way of declaring and initializing the actual bean object. By bean we mean the JavaBean component object.

Syntax:

<jsp:useBean id = “beanName” class = “className”
scope = “page | request | session | application” />

 

Here the id attribute specifies the name of the bean. Scope  specifies where the bean is stored and the class attribute specify the qualified classname.

If we can see a sample declaration we can find this very easy to understand.

<jsp:useBeanid="user1"class="beans.User" scope=”request”/>

above code is equivalent to the below Java code.

package beans;
User. user1  = (User)request.getAttribute(“user1”);
if (user1 =null)
{
user1 = new User();
request.setAttribute(“user1”,user1);

If jsp:useBean tag is used with a body, the content of the body is only executed if the bean is created. If the bean already exists in the named scope the body is skipped.

 

getProperty Tag:

This tag is used to retrieve a property from a JavaBeans instance.

Syntax:

<jsp:useBeanid="beanName"class=”package.class”scope=”session”/>
 
<jsp:getPropertyname="beanName"property="PropertyName"/>

 

The name attribute represents the name of the JavaBeans instance. The property attribute represents the        property of the JavaBean.

<jsp:getProperty name=”person” property=”name” />

The above code is equivalent to below in Java.

out.println(person.getName());


setProperty Tag:
 

The setProperty tag is used to store the data in JavaBeans instance.

Syntax:

<jsp:setProperty name=”beanName” property = “*” />
Or
<jsp:setProperty name=”beanName” property=”propertyName” />
Or
<jsp:setProperty name=”beanName” property=”propertyName” param=”parameterName”  />
Or
<jsp:setProperty name=”beanName” property=”propertyName” value=”propertyValue”  />

The name attribute tells the name of Javabeans instances. This must match the id attribute in the useBean tag. The property attribute tells the property within the bean.

A sample example is given below:

<jsp:setProperty name=”person” property=”age” />

The above code is equivalent to the below in Java:

person.setAge(request.getParameter(“age”));


Sample Program:

Now we can a sample example as below:

I have made use of a class named “user.java” and two jsp’s (getbean.jsp & setbean.jsp)

 

getbean.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<jsp:useBean id="user" class="beans.User" scope="session" ></jsp:useBean>
 
Email : <%= user.getEmail() %> </p>
Password: <%= user.getPassword() %>

</body>
</html>

setbean.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<jsp:useBean id="user" class="beans.User" scope="session" ></jsp:useBean>
 
<jsp:setProperty property="email" name="user" value="www.programmingtute.com"/>
<jsp:setProperty property="password" name="user" value="abcdf"/>
 
</body>
</html>

User.java:

package beans;
 
publicclass User {
String email;
 
String Password;
 
public String getEmail() {
returnemail;
}

publicvoid setEmail(String email) {
this.email = email;
}
 
public String getPassword() {
returnPassword;
}
 
publicvoid setPassword(String password) {
Password = password;
}}

In the above code the setBean.jsp is the page which sets the bean with the values coded for the property (email and password). And the getbean.jsp just outputs the value which was set by the setbean.jsp

And the user.java class has the properties email and password which we make use of the methods getEmail() and getPassword() to output the value in the getbean.jsp

Also we should not run the get getbean.jsp first. If we run it first then we will get the below output code:

Usebean

We will get the attribute values as “null” since we have not set the values for the attributes by means of setbean.jsp

So let’s first run the setbean.jsp as below:

Usebean

We will not have any output code since we have done anything in the setbean except setting the attributes.

And then if we run the getbean.jsp we will have our desired output.

Usebean

In the setbean.jsp we have used the scope value as “session” in the useBean tag.

So what’s that scope means the life span of the bean. That is till how much time the bean values will be alive.

So let’s discuss in this detail. We have four scope values which we can use:

  1. Session
  2. Request
  3. Application
  4. page

 

Session Scope:

Objects with session scope are accessible from pages processing requests that are in the same session as the one in which they were created. A session is the time users spend using the application, which ends when they close their browser, when they go to another Web site, or when the application designer wants (after a logout, for instance). So, for example, when users log in, their username could be stored in the session and displayed on every page they access. This data lasts until they leave the Web site or log out.

We have a maximum time for a session can be found by the below code.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%= session.getMaxInactiveInterval() %>
 
</body>
</html>

Application Scope:

Objects with application scope are accessible from JSP pages that reside in the same application. This creates a global object that’s available to all pages.

Application scope uses a single namespace, which means all your pages should be careful not to duplicate the names of application scope objects or change the values when they’re likely to be read by another page (this is called thread safety). Application scope variables are typically created and populated when an application starts and then used as read-only for the rest of the application.

 

Page scope:

Objects with page scope are accessible only within the page in which they’re created. The data is valid only during the processing of the current response; once the response is sent back to the browser, the data is no longer valid. If the request is forwarded to another page or the browser makes another request as a result of a redirect, the data is also lost.

 

Request Scope:

Objects with request scope are accessible from pages processing the same request in which they were created. Once the container has processed the request, the data is released. Even if the request is forwarded to another page, the data is still available though not if a redirect is required.

Please try changing the scope value in the above code and we can find the difference and have fun with scope.

Posted on July 25, 2014 in Java Server Pages

Share the Story

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top