Java Beans and JSP Actions
Java Beans and JSP Actions
LEVEL – PRACTITIONER
About the Author
2
Icons Used
Hands on
Questions Tools Exercise
Best Practices
Demonstration & Industry
Workshop
Standards
3
Objectives
4
What is server side java bean ?
A server side java bean is a class used to store the details of real
world entities
Example: Employee Employee Name and Employee Salary,
Student Student Name, Student Address.
Bean is a plain java class which contains
Fields (or) Properties: Fields to store the data, example:
Employee Name, Salary.
Methods: Methods for retrieving and modifying the attributes
like setEmployeeName(), setStudentAddress(). The methods
are referred to as accessors/mutator.
5
Java Bean design conventions
Syntax:
Public Datatype get<PropertyName> {
return value; // Returns the property value
}
Example:
Public int getEmployeeId {
return empId; // Returns the employee id value
} 6
Java Bean design conventions
For each writable property, the bean must have a method as illustrated below,
Syntax:
Public set<PropertyName>(Datatype newValue) {
property= newValue; // sets the new value into the property
}
Syntax:
Public setEmployeeId(int newEmpId) {
empId= newEmpId; // sets the new employee id into the
employee id property
}
7
Sample Bean Class
8
Why to follow naming convention for methods?
The web container maps the request attribute with the method names and
triggers the appropriate methods for retrieving or setting the values.
Example:
Request Attribute Name: empId
Method Triggered: setEmpId(), getEmpId()
9
Easy steps for creating Beans using SDE
10
Easy steps for creating Beans using SDE
(Cont)
11
Need for Beans in JSP
For example if you are handling with a registration form all the registration details
can be loaded into a RegistrationBean and can be transported across other
components as a single object.
12
Need for Beans in JSP
13
Lend a Hand : Using Java beans in JSP
In this demo we are going to familiarize how java beans are used with jsp.
We are going to develop a login page and validate the credentials and
redirect the response to success or error page.
Components
1 . login.jsp : login page
2 . success.jsp : Success page
3 . User.java : The user bean class.
The login.jsp will validate the user name/password , create a user bean if
successful redirect it to success page. Success page should access the
bean properties and display it on the screen.
14
Lend a Hand – login.jsp
15
Lend a Hand – Develop login.jsp
16
Lend a Hand – Develop user Bean
17
Lend a Hand – Develop success.jsp
Welcome TOM
18
Lend a Hand – Invalid Login
19
Lend a Hand : Deploy and Run
20
What is JSP action tag ?
JSP action tags are a set of predefined tags provided by the JSP
container to perform some common tasks thus reducing the java code in
JSP.
Where,
Example:
22
Action tags in JSP
jsp:include jsp:element
jsp:forward jsp:body
jsp:usebean jsp:text
jsp:setProperty jsp:attribute
jsp:getProperty jsp:param
jsp:fallback jsp:plugin
23
JSP:include
R
R E
E S 5
Q The footer JSP response is
2 P included in the home page and
U O
E response sent to client.
Home page N
1 translated to S S
homepage.java by T E 4
web container.
homepage.jsp homePage_jsp.java footer_jsp.java
3
Home page
requests footer
24
JSP file.
Jsp:include tag
Syntax :
25
Directive Vs Action include
Consider a scenario in which we are asked to create a login application . They have
the following requirements
Home page is common for all categories of employees
The heading is different for different designation of employees and should be
included in all the pages based on the employee designation
The footer page is common for all and should be include in all the pages .
26
Difference between directive and action include
To be included using
footer.html directive include
managerHeading.jsp traineeHeading.jsp
The forward action tag is used to transfer control to a static or dynamic resource.
The forward action terminates the action of the current page and forwards the
request to another resource such as a static page, another JSP page, or a Java
servlet.
The static or dynamic resource to which control has to be transferred is
represented as a URL.
The user can have the target file as an HTML file, another JSP file, or a servlet.
28
jsp:forward Syntax
Syntax:
<jsp:forward page=“URL" />
Forwards the request to the specified URL.
Example :
<jsp:forward page=“success.jsp" />
Forwards the request to the success.jsp
When used?
Provides the same functionality of the forward() method inRequestDispatcher
interface
Used for forwarding request from one JSP to another .
29
jsp:Param
30
jsp:Param Example
The <jsp:useBean> tag attempts to locates a bean or if the Bean does not exist,
instantiates it from the class specified.
Syntax: <jsp:useBean id="name" class="package.class“
scope=“request/session/page/application“ />
Where ,
Id – The name used for referring for the bean object
Class – The Bean Class
Scope - The scope in which the bean object is available
32
Other Attributes for useBean action tag
Attribute Description
Gives a name to the variable that will reference the bean. A previous bean object
id is used instead of instantiating a new one if one can be found with the same id and
scope.
Instantiates a Bean from a class, using the new keyword and the class constructor.
class The class must not be abstract and must have a public, no-argument constructor.
The package and class name are case sensitive.
scope="page|request|session|application"
scope
Defines a scope in which the bean exists .The default value is page.
If the Bean already exists in the scope, gives the Bean a data type other than the
type class from which it was instantiated. If you use type without class or beanName,
no Bean is instantiated. The package and class name are case sensitive.
Gives the name of the bean, as you would supply it to the instantiate method of
beanName
Beans.
33
How jsp:useBean works?
34
Bean Object Scopes
page :is available only within the JSP page and is destroyed when
the page has finished generating its output for the request.
request : valid for the current request and is destroyed when the
response is sent
session : valid for a user session and is destroyed when the
session is destroyed
application : valid throughout the application and is destroyed
when the web application is destroyed/uninstalled.
35
jsp:setProperty
The setProperty action sets the properties of a Bean. The Bean must have been
previously defined before this action.
Syntax:
<jsp:useBean id="myName" class=“package.class” />
<jsp:setProperty name="myName" property="someProperty”
value =“someValue” />
Where,
name : the name of the bean object and should be the same as the id value specified in
usebean
property :the bean property (field name) for which the value is to be set. There should
be a instance variable with the property name specified and accessors/mutator
methods.
value : the value to be set for the property .
36
jsp:setProperty mapping HTML forms
The following options can be used to automatically set the values from an HTML form to
a java bean using the setProperty action.
Option 1
<jsp:setProperty name=“UserBean" property="someProperty” param =“userName” />
Sets the value of the HTML form element with the name userName into the bean UserBean’s
property someProperty.
Option 2
<jsp:setProperty name="myName" property="*” />
Sets all the values of the form elements into the bean properties.
In this case the name of the property and the name of the form element should be the same
37
Example for jsp.setProperty usage
38
jsp:getProperty
The getProperty action is used to retrieve the value of a given property and
converts it to a string, and finally inserts it into the output.
Syntax:
<jsp:useBean id=“myName” type=“package.class” />
<jsp:getProperty name="myName" property="someProperty" />
Where,
name : Bean name same as the id specified in the usebean action
property : The bean property name whose value is to be retrieved
39
jsp:getProperty Example
Example:
<jsp:useBean id=“user” type=“com.catp.beans.UserBean” />
<jsp:getProperty name=“user" property=“userName" />
This Reads the value of the property named userName from the
UserBean and prints it
40
Recap: JSP Action tags
jsp:getProperty Gets the property value of a bean referred in use bean tag.
jsp:Param Used to share parameter and its value with the page being
included or forwarded.
41
Time To Reflect
What is the action tag used for setting a beans property value?
What is the action tag used for mapping HTML form elements with the bean.?
42
Lend a Hand : Action Tags
Using this demo the associates gets familiarized with the following action
tags in JSP
jsp:include
jsp:param
jsp:useBean
jsp:setProperty
jsp:getProperty
jsp:forward
43
Lend a Hand - Requirement
Consider a scenario in which ABC Soft corp have approached you to create a
registration form for their Employees for maintaining profile of each employee in the
company.
The requirement is as slated below
There are three designation of employees
a)Manager
b)Developer
c)Trainee
Employees should enter details in the registration form
On successful entry the employee is forwarded to their respective home pages.
44
Lend a Hand- Requirement (Cont)
The heading of the home page will be based on the designation of the
employee.
The heading should be dynamically loaded based on designation of employee.
A welcome message should be present in the home page.
Components
1. Registration.jsp : Registration page used by employees to register.
2. Success.jsp : Common Home page for all employees.
3. traineesHeading.jsp : Heading page for trainee’s success page.
4. developersHeading.jsp : Heading page for developer’s success page.
5. managersHeading.jsp : Heading page for manager’s success page.
6. Employee.java : The java bean class for storing the employee details.
45
Lend a Hand : Registration page Design
46
Lend a Hand : Registration.jsp code
Use JSP:forward to
forward request to success
page and jsp:param to set
success message.
47
Lend a Hand – Develop User Bean
48
Lend a Hand- Success page Design
49
Lend a Hand - Develop success.jsp
Use JSP:include to
include the respective
pages and use jsp:param
to set name value as a
parameter for heading
page to display.
50
Lend a Hand – Develop traineesHeading.jsp
51
Lend a Hand – Develop managersHeading.jsp
52
Lend a Hand – Develop developersHeading.jsp
53
Lend a Hand – Deploy and Run
54
Advance Java