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
Loading editor…

After Standard Actions (Bean Approach)

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

Example03
Loading editor…

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
Loading editor…

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
Loading editor…

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
Loading editor…

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
Loading editor…

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
Loading editor…

5) jsp:forward

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

Example15
Loading editor…

6) jsp:param

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

Example16
Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.