Nearby lessons

11 of 30

JSP - Standard Actions (useBean)

📌 What You Will Learn
  • Use jsp:useBean to create or find JavaBean objects
  • Get and set bean properties with jsp:getProperty and jsp:setProperty
  • Include and forward with jsp:include and jsp:forward

JSP Standard Actions provide a way to use JavaBeans and perform navigation in JSPs without writing scriptlets. This lesson covers jsp:useBean, jsp:getProperty, jsp:setProperty, jsp:include, jsp:forward, and jsp:param with complete examples.

Why Standard Actions?

Using scripting elements in JSPs has several disadvantages:

  • No clear separation between presentation and business logic
  • The developer needs both Java and HTML knowledge
  • Does not promote code reusability
  • Reduces readability

We can solve these problems by encapsulating business logic inside a JavaBean and using standard actions to access it.

Before Standard Actions (Scriptlet Approach)

Example02
JCode Cell
1 
2<%!
3 public int squareIt(int x) {
4 return x * x;
5 }
6%>
7<h1>
8 The Square of 4 is: <%= squareIt(4) %><br>
9 The Square of 5 is: <%= squareIt(5) %>
10</h1>
11

After Standard Actions (Bean Approach)

Encapsulate business logic in a Java class, then use standard actions:

Example03
JCode Cell
1 
2CalculatorBean.java:
3package pack1;
4public class CalculatorBean {
5 public int squareIt(int i) {
6 return i * i;
7 }
8}
9 
10test.jsp:
11<jsp:useBean id="c" class="pack1.CalculatorBean" />
12<h1>
13 The Square of 4 is: <%= c.squareIt(4) %><br>
14 The Square of 5 is: <%= c.squareIt(5) %>
15</h1>
16

Advantages of the Bean Approach

  • Separation of Presentation and Business Logic — JSP handles presentation, Java class handles logic
  • Separation of Responsibilities — Java developers focus on logic, HTML designers focus on UI
  • Reusability — the same bean can be used in multiple JSPs

Java Bean Rules

A Java Bean is a simple Java class that follows these rules:

  • Must have a public no-arg constructor (needed by jsp:useBean)
  • For every property, must have public getter and setter methods (needed by jsp:getProperty and jsp:setProperty)

1) jsp:useBean

Makes a bean object available to the JSP. Two forms:

  • Without body: <jsp:useBean id="c" class="pack1.CalculatorBean" scope="request" />
  • With body: executes body only for newly created beans (initialization)
Example06
JCode Cell
1 
2<%-- without body --%>
3<jsp:useBean id="c" class="pack1.CalculatorBean" scope="request" />
4 
5<%-- with body --%>
6<jsp:useBean id="c" class="pack1.CalculatorBean">
7 <jsp:setProperty name="c" property="*" />
8</jsp:useBean>
9

jsp:useBean Attributes

AttributePurposeRequired
idName of the reference variable for the beanYes
classFully qualified class name of the beanAt least one of class/type
typeType of the reference variable (can be interface/abstract)At least one of class/type
scopeScope to search for the bean: page, request, session, applicationNo (default: page)
beanNameFor serialized beans from local file systemNo

jsp:useBean — Valid Combinations

  • id is always mandatory
  • Valid combinations of class/type/beanName:
    • class alone
    • type alone
    • class + type
    • type + beanName
  • If only type is used (no class), the bean must already exist in the specified scope

jsp:useBean — Scope and Equivalence

is equivalent to this Java code:

Example09
JCode Cell
1 
2CalculatorBean c = null;
3c = (CalculatorBean) pageContext.getAttribute("c", 2); // 2 = REQUEST_SCOPE
4if (c == null) {
5 c = new CalculatorBean();
6 pageContext.setAttribute("c", c, 2);
7}
8

2) jsp:getProperty

Retrieves a property value from a bean and writes it to the response.

Attributes (both mandatory):

  • name — the bean reference name (same as id in jsp:useBean)
  • property — the name of the bean property to get

Internally calls the getter method. The bean must have the corresponding getter.

Example10
JCode Cell
1 
2<jsp:useBean id="c" class="pack1.CustomerBean" />
3<h1>
4 Customer Name: <jsp:getProperty name="c" property="name" /><br>
5 Customer Mail: <jsp:getProperty name="c" property="mail" />
6</h1>
7

3) jsp:setProperty

Sets a property value on a bean. Three forms:

FormSyntaxEquivalent Java
With value<jsp:setProperty name="c" property="name" value="durga" />c.setName("durga")
With param<jsp:setProperty name="c" property="name" param="uname" />c.setName(request.getParameter("uname"))
Wildcard *<jsp:setProperty name="c" property="*" />Matches request params to bean properties automatically

jsp:setProperty — Wildcard * (Auto-Mapping)

When property="*", the JSP Engine iterates through all request parameters. If any parameter name matches a bean property name, it assigns the parameter value to that property:

Example12
JCode Cell
1 
2<%-- login.html sends: name, mail, age --%>
3<jsp:useBean id="c" class="pack1.CustomerBean" />
4<jsp:setProperty name="c" property="*" />
5<%-- This sets: c.setName(name), c.setMail(mail), c.setAge(age) --%>
6 
7<h1>
8 Name: <jsp:getProperty name="c" property="name" /><br>
9 Mail: <jsp:getProperty name="c" property="mail" /><br>
10 Age: <jsp:getProperty name="c" property="age" />
11</h1>
12

jsp:setProperty — Attributes

AttributePurposeRequired
nameBean reference name (same as jsp:useBean id)Yes
propertyProperty name to set (or * for all)Yes
valueValue to assign to the propertyNo (mutually exclusive with param)
paramRequest parameter name whose value to useNo (mutually exclusive with value)

4) jsp:include

Includes the response of another JSP at request processing time (dynamic include).

  • page — the included page (mandatory)
  • flush — flush before inclusion (optional, default: false)
Example14
JCode Cell
1 
2<jsp:include page="header.jsp" />
3Offered courses are: Java, .Net, Testing...
4<jsp:include page="footer.jsp" />
5

5) jsp:forward

Forwards the request to another JSP or servlet. The current JSP stops processing and the target takes over.

Example15
JCode Cell
1 
2first.jsp:
3<h1>This is First JSP</h1>
4<jsp:forward page="second.jsp" />
5 
6second.jsp:
7<h1>This is Second JSP</h1>
8

6) jsp:param

Sends parameters to the target JSP during forward or include. Parameters are available as request parameters in the target.

Example16
JCode Cell
1 
2<jsp:include page="second.jsp">
3 <jsp:param name="c1" value="JAVA" />
4 <jsp:param name="c2" value="TESTING" />
5</jsp:include>
6 
7second.jsp:
8<h1>The offered courses are:
9 <%= request.getParameter("c1") %> and
10 <%= request.getParameter("c2") %>
11</h1>
12

Include Methods Comparison

JSPs support 4 ways to include content:

MethodSyntaxType
Include directive<%@ include file="..." %>Static (translation time)
jsp:include action<jsp:include page="..." />Dynamic (runtime)
pageContext.include()<% pageContext.include("..."); %>Dynamic (runtime)
RequestDispatcher<% request.getRequestDispatcher("...").include(request, response); %>Dynamic (runtime)

Forward Methods Comparison

JSPs support 3 ways to forward:

MethodSyntax
jsp:forward action<jsp:forward page="..." />
pageContext.forward()<% pageContext.forward("..."); %>
RequestDispatcher<% request.getRequestDispatcher("...").forward(request, response); %>

Summary of All 9 Standard Actions

ActionPurposeKey Attributes
jsp:useBeanCreate or find a JavaBeanid, class, type, scope, beanName
jsp:getPropertyGet a bean property valuename, property
jsp:setPropertySet a bean property valuename, property, value, param
jsp:includeInclude another page (dynamic)page, flush
jsp:forwardForward request to another pagepage
jsp:paramSend parameters during include/forwardname, value
jsp:pluginEmbed an applettype, code, codebase...
jsp:fallbackMessage if plugin not supportedN/A
jsp:paramsSend parameters to appletN/A
📝 Key Takeaways
  • Standard actions separate presentation from business logic
  • jsp:useBean needs id + class (or type) + optional scope
  • JavaBeans require a public no-arg constructor and getters/setters

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3