Nearby lessons
11 of 30JSP - Standard Actions (useBean)
- 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)
After Standard Actions (Bean Approach)
Encapsulate business logic in a Java class, then use standard actions:
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:getPropertyandjsp: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)
jsp:useBean Attributes
| Attribute | Purpose | Required |
|---|---|---|
id | Name of the reference variable for the bean | Yes |
class | Fully qualified class name of the bean | At least one of class/type |
type | Type of the reference variable (can be interface/abstract) | At least one of class/type |
scope | Scope to search for the bean: page, request, session, application | No (default: page) |
beanName | For serialized beans from local file system | No |
jsp:useBean — Valid Combinations
idis always mandatory- Valid combinations of class/type/beanName:
classalonetypealoneclass+typetype+beanName
- If only
typeis used (noclass), the bean must already exist in the specified scope
jsp:useBean — Scope and Equivalence
is equivalent to this Java code:
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 asidin jsp:useBean)property— the name of the bean property to get
Internally calls the getter method. The bean must have the corresponding getter.
3) jsp:setProperty
Sets a property value on a bean. Three forms:
| Form | Syntax | Equivalent 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:
jsp:setProperty — Attributes
| Attribute | Purpose | Required |
|---|---|---|
name | Bean reference name (same as jsp:useBean id) | Yes |
property | Property name to set (or * for all) | Yes |
value | Value to assign to the property | No (mutually exclusive with param) |
param | Request parameter name whose value to use | No (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)
5) jsp:forward
Forwards the request to another JSP or servlet. The current JSP stops processing and the target takes over.
6) jsp:param
Sends parameters to the target JSP during forward or include. Parameters are available as request parameters in the target.
Include Methods Comparison
JSPs support 4 ways to include content:
| Method | Syntax | Type |
|---|---|---|
| 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:
| Method | Syntax |
|---|---|
| jsp:forward action | <jsp:forward page="..." /> |
| pageContext.forward() | <% pageContext.forward("..."); %> |
| RequestDispatcher | <% request.getRequestDispatcher("...").forward(request, response); %> |
Summary of All 9 Standard Actions
| Action | Purpose | Key Attributes |
|---|---|---|
jsp:useBean | Create or find a JavaBean | id, class, type, scope, beanName |
jsp:getProperty | Get a bean property value | name, property |
jsp:setProperty | Set a bean property value | name, property, value, param |
jsp:include | Include another page (dynamic) | page, flush |
jsp:forward | Forward request to another page | page |
jsp:param | Send parameters during include/forward | name, value |
jsp:plugin | Embed an applet | type, code, codebase... |
jsp:fallback | Message if plugin not supported | N/A |
jsp:params | Send parameters to applet | N/A |
- 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