We are generally using scripting elements in the jsp. This approach is very easy at beginners level.


public int squareIt(int x){
  return x*x;
Square of 4 is : <% = squareIt(4) %>
Square of 5 is : <% = squareIt(5) %>

This approach has several serious disadvantages

  1. There is no clear separation of presentation and business logic.
  2. The persons who is writing jsp should have compulsory the knowledge of both java and HTML. Which may not possible always.
  3. This approach doesn’t promote re usability
  4. It reduces readability of the code also.

We can resolve all these problems by encapsulating the business logic inside java bean.


package packl;
Public class calculator Bean{
Public int squareIt(int x){
  Return x*x;


<jsp:useBean id="c" class =”packl.CalculatorBean”/>
<h1> the square of 4 is : <% = c.squareIt(4) %>
The square of 5 is :<% = c.squareIt(5) %></h1>

This approach has several advantages.

  1. Separation of presentation and business logic (presentation logic is available in jsp & business logic is available in Bean class) so that readability of the code has bean improved.
  2. Separation of responsibilities:Java developer can concentrate on business logic where as html page designer can concentrate on presentation logic. As both can work simultaneously so that we can reduce project development time.
  3. It promotes re-usability of the code,Where ever suareIt() functionality required we use the same bean without rewriting.

Java Bean

Java bean is a simple java class.To use bean inside jsp bean class has to follow the following rules.

  1. Bean class should compulsory contain public no-arg.constructor other wise we will get instantiation problem at run time and tag won’t work properly.
  2. To use tag compulsory bean class should contain corresponding public getter methods.
  3. To use tag bean class should contain corresponding public setter methods.


We can use tag to make bean object available to the jsp.

There are two forms of

    without body.

        <jsp: useBean id = "c" class = "packl.CustomerBean"/>

    with body

         <jsp:useBean id = "c" class = "packl.CustomerBean">
        </jsp: useBean>

The main objective of body is to perform initialization for these newly created bean object.

If the bean object is already available then tag won’t create any new bean object and it will use existing bean object only.At this time the body won’t be executed.

Attributes of <jsp:useBean>

This tag contains the following 5 attributes:

  1. id
  2. class
  3. type
  4. scope
  5. beanName
  1. This attribute represents the name of the reference variable of the bean object.
  2. By means of this id only, we can access bean in rest of the jsp.
  3. Id attribute is mandatory attribute.


<jsp:useBean id = "calc" class ="packl.CalculatorBean"/>
                  The equivalent java code.
Packl.CalculatorBean calc = new pacl.CalculatorBean ();
  1. This attribute specifies fully qualified name of the bean class. Jsp engine performs instantiate for this class only hence the value of class attribute should be concrete class.
  2. We can’t use abstracts class and interface as class attribute value.
  3. class attribute value ( bean class) should compulsory contain public no-arg constructor other wise we will get instantiate exceptions.
  4. class attribute is optional and when ever we are not using class attribute compulsory type attribute should be present.
  1. This attribute can be used to specify the type of the reference variable. The value of type attribute can be concrete class or abstract class or interface.
  2. The type attribute is optional and when ever we are not using type attribute compulsory we should use class attribute


<jsp:useBean id = "p" class = "employee" type = "person"/>
             Person p = new employee():
             Type    id      class.

It specifies in which scope the jsp engine has to search for the required bean object.
In the specified scope, if the bean object is already available then jsp engine uses existing bean object instead of creating new object.
In the specified scope, if the bean object is not already available then jsp engine create a new bean object and saves that bean object in that scope for the future purpose.
The allowed values for the scope are page,request, session ans application.
The scope attribute is optional & default scope is page.


<jsp:useBean id= “c” class = "CalculatorBean" scope = "request"/>
  Calculator  c =null;
   c= (CalculatorBean) pageContext.getAttribute("c" ,PageContext.REQUEST_SCOPE);
  If(c ==null){
  c= new CalculatorBean();
  pageContext.setAttribute("c", c,PageContext.REQUEST_SCOPE);

Note: To use session scope compulsory session object should be available in the jsp other wise we will get translation time error.


<% @ page session = "false" %>
<jsp:useBean id = "c" class = "packl.CalculatorBean" scope = "session"/>

Illegal for useBean to use session scope when jsp page declares (via page directive) that it does not participate in sessions.

There may be a chance of using serializable bean object from the file system in that case we have to user this attribute.

<jsp: getProperty>

We can use this standard action for getting and printing properties of bean object.


customerBean c = new customerBean ();//<jsp:useBean id="c" class="CustomerBean"/>
out.println(c.getage());//<jsp:getProperty name="c" property="age"/>

<jsp:getProperty> contains the following 2 attributes.

  1. name
  2. The name of the bean instance, from which ,required bean property we have to access: this is exactly equal to id attribute of <jsp:useBean>.
    It is mandatory attribute.

  3. property
  4. The name of the java bean property which has to retrieve. it is also mandatory attribute.

package packl;
public class CustomerBean {
public  String name = "abc";
public String mail = "abc@yahoo.com";
public String getName(){
   return name;
public String getMail(){
   return mail;


<jsp:useBean id = "c" class = "packl.CustomerBean"/>
The customer name :<jsp:getProperty name = "c" property = "name"/>
The customer mail: <jsp:getProperty name = "c" property = "mail"/>


This standard action can be used to set properties of bean object. We can use <jsp:setProperty> tag in the following forms:

<jsp: useBean id = "c" class = "CustomerBean"/>
<jsp:setProerty name = "c" property = "mail" value = "abc@yahoo.com"/>
<jsp:setProperty name = "c" param = "mail"/>

It retrieves the value of the specified request parameter and assign it’s value to the specified bean property.


If the request parameter name matches with bean property name then no need to use param attribute.

<jsp:setProperty name = "c" property = "mail"/>
    c.setMail(request.get parameter("mail"));
<jsp:setProperty name = "c" property = "*"/>

*—–>specified all properties of the bean.
It iterates through all request parameters and if any parameter name matched with bean property name then it assigns request form parameter value to the bean property.

Attribute of<jsp:setProperty>

  1. name
  2. It specifies the name of the bean object where property has to set, this is exactly same as id attribute of <jsp:useBean>. It is mandatory attribute.

  3. property
  4. The name of the java bean property which has to be set. It is mandatory attribute.

  5. value
  6. It specifies the value which has to set to the java bean property. It is optional attribute and never comes in combination with param attribute.

  7. param
  8. This attribute specifies the name of the form parameter whose value has to the bean property. It is optional attribute but never come in combination with value attribute.

Developing reusable web components

We can develop reusable web components by using the following standard actions

  1. <jsp:include>
  2. <jsp:forward>
  3. <jsp:param>


     <jsp:include page ="header.jsp" flush ="true"/>

The response of header.jsp will be included in the current jsp at request processing time,hence it is dynamic include.
This is the tag representation of pageContext.include.(“menu.jsp”)
This standard action contains the following 2 attributes:

  1. page:
  2. Represents the name of the included page and it is mandatory.

  3. flush:
  4. It determines whether the response will be flushed before inclusion or not. It is optional & default value.

Note:in jsp’s we can perform inclusion in the following 4 ways.

  1. By include directive
  2.     <%@ include file ="menu.jsp"%>
  3. By include action
  4.                <jsp:include page ="menu.jsp">
  5. By pageContext implicit object
  6.               <% pageContext.include("menu.jsp");
  7. By RequestDispatcher
  8.          <%
              RequestDispatcher rd = get request.getRequestDispacher ("menu.jsp");


If the first jsp is responsible for some preliminary and second jsp is responsible for providing complete response then we should go for forward mechanism.

<jsp:forward page = "second.jsp"/>



<h1> this is first jsp </h1>
<jsp: forward page = "second.jsp"/>


<h2> this is second jsp </h2>

<jsp:forward> contains only one attribute i.e page & it is mandatory.
In total there are 3 ways to implement forward in jsp’s.

  1. By forward action
  2.     <jsp:forward page = "second.jsp">
  3. By pageContext implicit object:
  4.     <%
  5. By RequestDispatcher
  6.     <%
        requestDispatcher rd = request.getRequestDispatcher("second.jsp");

While performing forward or include we are allowed to send parameters to the target jsp.

We can achieve this by using tag.

<jsp:param name = "c1" value = "JAVA"/>

<jsp:param> Tag contains the following 2 mandatory attributes.

  1. name
  2. It presents the name of the parameter.
    It represents the name of the parameter.

These parameters which are sending by using tag are available as form parameters in the target jsp.


<h1>this  is first jsp</h1>
<jsp:forward page = "second.jsp">
 <jsp:param name ="c1" value = "JAVA"/>
 <jsp:param name = "c1" value = "PHP"/>


<%@ page is EL Ignored = "false"%>
<h1> welcome to DSS.. the offered coursed are
<% = request.getParameter("c1")%> and ${param.c2}</h1>