JSP useBean action tag

In this post, you will learn:

  • What is the role of useBean action in JSP?
  • How useBean action is used in JSP?

External objects in JSP:

In a JSP page, two types of objects can be used:

1. implicit objects

2. external objects

Implicit objects are Servlet & JSP API objects which are made available by default in the _jspService() method. External objects are objects of user defined or library classes which are created in the _jspService() method on the request of the JSP programmer. Model objects, Collections of Model objects, DataSource, Connection etc. are some examples of the external objects in JSP.

<jsp:useBean /> action is used to get an external object created in the _jspService() method of the auto generated servlet. This action performs two tasks:

1. It searches the requested object in the given scope. If found, the object is fetched from the scope and is made available in the _jspService() method so that it can be used in the processing of the request.

2. If not found, the object is created and saved as attribute in the given scope so that it can be used in the processing of the current as well as subsequent requests.

It has following syntax:

<jsp:useBean id=”beanName” class=”BeanClass” scope=”[page,request,session,application]” />

Notes:

1. A Bean class is a simple Java class which has following characteristics:

  • It has default constructor.
  • It has getter & setter methods to set and obtain the value of its data members.
  • It may contain business logic, persistence logic or presentation logic.

2. Scope is an optional attribute of the useBean action. The default scope is page i.e. the object remain available in the current page only. In a request multiple page can participate through inclusion & forwarding. If the object is to be made available in all the participating pages then scope should be request. If the object is required in all the requests of a user, then scope should be session and if it is required in all the requests of all the users then scope should be application.

To understand the internal working of the useBean action lets take an example. let following be a bean class. An object of it is used to store user information through out the session i.e. its object is required in all the requests which can be submitted by a user.

package com.learningpad.techmentro;

public class User
{
private int id;
private String name,mailId,password;

//default constructor will be provided by the compiler at the time of compilation

public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setMailId(String mailId)
{
this.mailId=mailId;
}
public String getMailId()
{
return mailId;
}
public void setPassword(String password)
{
this.password=password;
}
public String getPassword()
{
return password;
}
}

An object of the User class can be get created and stored in session scope or if it already exists, can be fetched from the session scope as follows:

<jsp:useBean id=”user” class=”com.techmentro.learningpad.User” scope=”session” />

In the _jspService() method of the auto generated servlet, following code will be generated for the above action tag, at the time of translation.

//session object is locked for thread safety.
synchronized(session)
{
//user attribute is searched in the session scope.
com.techmentro.learningpad user=(com.techmentro.learningpad.User)session.getAttribute("user");
if(user==null)//user attribute is not found in the session scope
{
//User object is created.
com.techmentro.learningpad user=new com.techmentro.learningpad.User();
//User object is saved in the session scope.
sesison.setAttribute("user",user);
}
}

With the help of above code snippet, you can understand how useBean action works.


Its time to recap the main concepts:

  1. In a JSP page two types of objects can be used: Implicit & External.
  2. Implicit objects are Servlet & JSP API objects which are made available by default in the _jspService() method of the auto generated servlet.
  3. External objects are created in the _jspService() method by the JSP programmer with the help of useBean action.
  4. The class, object of which is to be created with the help of useBean action must be defined in a package.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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