09 - JSP Actions: Page 2 of 3

9.6 <jsp:useBean>

The JSP  action tag <jsp:useBean> is used to interact with java beans in JSP. The jsp:useBean action tag searches for an existing java bean object based on id in a given scope. If object does not found, it creates a the java bean.

General syntax of <jsp:useBean> is

<jsp:useBean
  id= "instanceName"
  scope= "page | request | session | application"
  class= "packageName.className"
</jsp:useBean>

Where

· id is the identifier used to identify the bean in a given scope

· class attribute is used to instantiate the specified bean. Bean must have zero argument constructor and must not be an abstract class.

· scope is scope of bean and can have four possible values.

o request-specifies that bean is available in all jsp (sharing same request)

o page- specifies that this bean can be used within the page only and this is default scope.

o session- bean is available in session scope

o application- bean is available in application scope

Most wider scope is application

For example below <jsp:useBean> tag in jsp

      <jsp:useBean id=”sampleBean” class=”com.sample.jsp.tutorial.SampleBean” scope=”request” />

is equivalent to following java code

com.sample.jsp.tutorial.SampleBean sampleBean= (com.sample.jsp.tutorial.SampleBean)request.getAttribute(“sampleBean”);
if(sampleBean==null)
{
sampleBean= new com.sample.jsp.tutorial.SampleBean();
request.setAttribute(“sampleBean”,sampleBean);
}

Section highlighted in a yellow color has to be same.

Lets write one Person class with two fields (firstName and lastName) with the default values as “Not Provided “ like below

package com.sample.jsp.tutorial;
public class Person {
    private String firstName = ”Not Provided”;
    private String lastName = ”Not Provided” ;
    public String getFirstName() {
      return firstName;
    }
    public void setFirstName(String firstName) {
      this.firstName = firstName;
    }
    public String getLastName() {
      return lastName;
    }
    public void setLastName(String lastName) {
      this.lastName = lastName;
    }
}

Create a useBean.jsp in WebContent directory with with below content

<html>
  <head>
    <title> Use Bean Example </title>
  </head>
  <body>
    <jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request"/>
    First Name :: <%=person.getFirstName() %>
    <br/>
    Last Name :: <%=person.getLastName() %>
  </body>
</html>

 

Access useBean.jsp using http://localhost:8080/jsp-tutorial/useBean.jsp


We can see default values because these attributes are not set explicitally.

Now update useBean.jsp file to set firstname and last name and use <jsp:include> tag to include anotherUseBean.jsp file which will use useBean tag and display value.

Updated useBean.jsp

<html>
  <head>
    <title> Use Bean Example </title>
  </head>
  <body>
    <jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request"/>
    First Name :: <%=person.getFirstName() %>
    <br/>
    Last Name :: <%=person.getLastName() %>
    <%
      person.setFirstName("First Name");
      person.setLastName("Last Name");
    %>
    <br/>
    <jsp:include page="anotherUseBean.jsp"></jsp:include>
  </body>
</html>

anotherUeBean.jsp code

<html>
  <head>
    <title> Use Bean Example </title>
  </head>
  <body>
    <jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request"/>
    First Name :: <%=person.getFirstName() %>
    <br/>
    Last Name :: <%=person.getLastName() %>
  </body>
</html>

 

Access useBean.jsp using http://localhost:8080/jsp-tutorial/useBean.jsp

You can see the values of first name and last name in anotherUseBean.jsp.The Reason is –

· in userBean.jsp file, bean was not available in request scope so <jsp:useBean> tag created an instance of it so it was having a default value.

· anotherUserBean.jsp is having same request so in this jsp bean object was found as it was already created by <jsp:useBean> tag in useBean.jsp and sets its attributes.

Note : 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 given scope, the body is skipped.

By term “Used with Body” we mean

<jsp:useBean id=”someID” class=”someClass” scope=”request’>
….
….
</jsp:useBean>
<jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request"/>  to  <jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request"> Hello<br/> </jsp:useBean>  

And re access the page again

 

You can see Hello (body section of <jsp:useBean> is invoked only for useBean.jsp and not for anotherUseBean.jsp file) because instance was already found in case of anotherUserBean.jsp file

9.7 <jsp:getProperty>

This action is used to get the value of attribute of a java bean instance and it displays the value of the property. Syntax of getProperty tag is

<jsp:getProperty name=”bean_name” property=”property_name” />

Where name is the name of java bean and property is the name of attribute of bean.

For example

      <jsp:getProperty name=”bean_name” property=”property_name” />

Is equivalent to below code

      <% out.println(person.getLastname()); %>

Lets change the useBean.jsp and anotherUseBean.jsp created in earlier section to use <jsp:getProperty> tag in place of scriplets. Changes are highlighted below

useBean.jsp updated code

<html>
  <head>
    <title> Use Bean Example </title>
  </head>
  <body>
    <jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request">
       Hello<br/>
    </jsp:useBean>
    First Name :: <jsp:getProperty property="firstName" name="person"/>
    <br/>
    Last Name :: <jsp:getProperty property="lastName" name="person"/>
    <%
     person.setFirstName("First Name");
     person.setLastName("Last Name");
    %>
    <br/>
    <jsp:include page="anotherUseBean.jsp"></jsp:include>
  </body>
</html>

anotherUseBean.jsp updated code

<html>
  <head>
    <title> Use Bean Example </title>
  </head>
  <body>
    <jsp:useBean id="person" class="com.sample.jsp.tutorial.Person" scope="request">
       Hello
    </jsp:useBean>
       First Name :: <jsp:getProperty property="firstName" name="person"/>
       <br/>
       Last Name :: <jsp:getProperty property="lastName" name="person"/>
  </body>
</html>

 

On accessing http://localhost:8080/jsp-tutorial/useBean.jsp we will see

Like us on Facebook