Lecture 37
Lecture 37
Lecture 37
Let’s first quickly look on the JSP journey to find out where we have reached.
Directive Elements
– Provides global control of JSP ……..…………….. <%@ %>
Scripting Elements
– JSP comments ……………………………………... <%-- --%>
Format
- 460 -
Handout 37
Web Design & Development CS-506
<jsp:useBean id = “name”
scope = “page|request|session|application”
class=“package.Class ” />
The id attribute specifies the name of the JavaBean object that is also used for later
references. The scope attribute can have one possible value out of page, request,
session and application. If this attribute is omitted, the default value of scope attribute is
page. We’ll discuss in detail about scope shortly.
The class attribute specifies the type of object is going to be created.
- 461-
Handout 37
Web Design & Development CS-506
Achieving above functionality using jsp:useBean action element will look like
this:
<jsp:useBean id = “m”
scope = “page”
class=“vu.MyBean” />
In the above code snippet, we are assuming that MyBean lies in vu package.
To set or change the property value of the specified bean. String values are converted to
types of properties by using the related conversion methods.
The name attribute should match the id given in jsp:useBean. The property
attribute specifies the name of the property to change and the value attribute specifies
the new value.
jsp:setProperty is being equivalent to following code of scriptlet. For example to
change the name property of m (instance of MyBean) using scriptlet is:
<%
m.setProperty(“ali”);
%>
Achieving above functionality using jsp:setProperty action element will look like
this:
- 462 -
Handout 37
Web Design & Development CS-506
Use to retrieves the value of property, converts it to String and writes it to output stream.
Achieving above functionality using jsp:getProperty action element will look like
this:
-463 -
Handout 37
Web Design & Development CS-506
This example contains index.jsp and result.jsp and one JavaBean i.e.
SumBean. User will enter two numbers on index.jsp and their sum will be
displayed on result.jsp. Let’s examine these one after another
SumBean.java
– firstNumber
– secondNumber
– sum
The firstNumber and secondNumbers are “write-only” properties means for these only
setters would be defined. Whereas sum is a “read-only” property as only getter would be
defined for it.
The SumBean also contain one additional method for calculating sum i.e.
calulateSum(). After performing addition of firstNumber with secondNumber, this
method will assign the result to sum attribute.
package vu;
import java.io.*;
// no argument constructor
public SumBean() {
firstNumber = 0;
secondNumber = 0;
sum = 0;
}
- 464-
Handout 37
Web Design & Development CS-506
index.jsp
This page will display two text fields to enter number into them.
<html>
<body>
</body>
</html>
result.jsp
This page will calculate the sum of two entered numbers by the user and displays the sum
back to user. The addition is performed using SumBean
<%-- importing vu package that contains the SumBean --%>
<%@page import="vu.*"%>
<html>
<body>
- 465-
Handout 37
Web Design & Development CS-506
<%--
//Servlet equivalent code of useBean
SumBean sBean = new SumBean();
--%>
<%--
//Servlet equivalent code of setProperty for num1
int no = Integer.parseInt(request.getParameter("num1"));
sBean.setFirstNumber(no);
--%>
<jsp:setProperty name="sBean"
property="firstNumber" param="num1" />
<%--
//Servlet equivalent code of setProperty for num2
int no = Integer.parseInt(request.getParameter("num2"));
sBean.setSecondNumber(no);
--%>
<jsp:setProperty name="sBean"
property="secondNumber" param="num2" />
<%
// calling calculateSum() method that will set the value of
// sum attribute
sBean.calculateSum();
%>
<%--
// servlet equivalent code of displaying sum
int res = sBean.getSum();
out.println(res);
--%>
</h2>
</body>
</html>
- 466-
Handout 37
Web Design & Development CS-506
Although the beans are indeed bound to local variables, that is not the only behavior.
They are also stored in four different locations, depending on the value of the optional
scope attribute of jsp:useBean. The scope attribute has the following possible
values: page, request, session and application.
Let’s discover what impact these scopes can produce on JavaBeans objects which are
stored in one of these scopes.
page
This is the default value of scope attribute, if omitted. It indicates, in addition to being
bound to local variable, the bean object should be placed in the pageContext
object. The bean’s values are only available and persist on JSP in which bean is
created.
In practice, beans created with page scope are always accessed (their values) by
jsp:getProperty, jsp:setProperty, scriptlets or expressions later in the
same page. This will be more cleared with the help of following diagram:
PageContext
- 467 -
Handout 37
Web Design & Development CS-506
request
This value signifies that, in addition to being bound to local variable, the bean object
should be placed in ServletRequest object for the duration of the current
request. In other words, until you continue to forward the request to another
JSP/servlet, the beans values are available. This has been illustrated in the following
diagram.
MyBean m
[name = ali]
ServletRequest
- 468-
Handout 37
Web Design & Development CS-506
session
This value means that, in addition to being bound to local variable, the bean object
will be stored in the HttpSession object associated with the current request. As
you already know, object’s value stored in HttpSession persists for whole user’s
session. The figure below helps in understanding this concept.
(3) create
(5) values available (7) values available
MyBean m
[name = ali]
HttpSession
application
This very useful value means that, in addition to being bound to local variable, the
bean object will be stored in ServletContext. The bean objects stored in
ServletContext is shared by all JSPs/servlets in the same web application. The
diagram given below illustrates this scenario:
- 469-
Handout 37
Web Design & Development CS-506
(3) create
(5) values available (7) values available
MyBean m
[name = ali]
ServletContext
- 470 -
Handout 37
Web Design & Development CS-506
Let’s take another view of session, request & page scopes in the next figure that helps us
to understand the under beneath things.
The figure shows four JavaServer Pages. Each page has its own page scope. Therefore
objects stored in page scope are only available to same pages on which they are created.
Suppose page1 forwards the request to page2. Objects stored in request scope remains
available to page1 as well to page 2. Similar case is true for page 3 & page 4.
If user makes a visit to all these pages in one session, object’s values stored in session
scope remains available on all these pages.
- 471 -
Handout 37
Web Design & Development CS-506
To understand the difference between sessions & application scope, consider the
following figure:
As you can conclude from the figure, for each user (client), objects are stored in different
sessions. However, in the case of application scope, all users stores objects in single
place.
- 472-
Handout 37
Web Design & Development CS-506
Let’s talk about two important action elements. These are include & forward.
The jsp:include action element requires two attributes: page & flush.
- page: a relative URL of the file to be included.
- flush: must have the value “true”
Achieving above functionality using jsp:include action element will look like this:
<jsp:include page = “one.jsp” flush = “true” />
-------------------
- 473 -
Handout 37
Web Design & Development CS-506
References:
- 474-