Java Server Pages
Chapter 10: Sharing Data Between
JSP Pages, Requests, and Users
Slides material compiled from
Java Server Pages 3rd Edition
by
Hans Bergsten
Sharing Data Between
JSP Pages, Requests, and Users
• Any real application consists of more than a
single page
• Multiple pages often need access to the same
information and server-side resources
• e.g., one page that retrieves the data the user
asked for and another that displays it (online
shopping application)
Rakhi Saxema (Internet Technologies) 2
Passing Control and Data Between
Pages
• Model-View-Controller (MVC) model
– most fundamental features of JSP
– allows for separation of request processing,
business logic and presentation
• User Info example
– Display the form for user input (presentation)
– Validate the input (request processing and
business logic)
– Display the result of the validation (presentation)
Rakhi Saxema (Internet Technologies) 3
User Info application pages
Rakhi Saxema (Internet Technologies) 4
Passing Control and Data
• Using different JSP pages as Controller and
View means that more than one page is used
to process a request.
• To make this happen, you need to be able to
do two things:
– Pass control from one page to another
– Pass data from one page to another
Rakhi Saxema (Internet Technologies) 5
Passing Control from One Page to
Another
• <jsp:forward> action
<jsp:forward page="[Link]" />
• Action stops processing of one page and starts
processing the page specified by the page
attribute (target page)
• Control never returns to the original page
• Target page has access to all information about
the request, including all request parameters
• Can add additional request parameters
<jsp:forward page="[Link]" >
<jsp:param name="msg" value="Invalid email address" />
</jsp:forward>
Rakhi Saxema (Internet Technologies) 6
Passing Data from One Page to
Another
• JSP provides different scopes for sharing data objects
• Scope defines how long the object is available and whether it's
available only to one user or to all application users
• Following scopes are defined:
– page, request, session, and application
• page scope (default scope)
– Objects available only within that page
• request scope
– Objects available to all pages processing the same request
• session scope
– Objects available to all requests made from the same browser
• application scope
– Objects shared by all users of the application
Rakhi Saxema (Internet Technologies) 7
Lifetime of objects in different scopes
Rakhi Saxema (Internet Technologies) 8
[Link]
<!-- [Link] -->
<%@ taglib uri="[Link] prefix="c" %>
<html>
<head>
<title> Accessing Form parameters </title>
</head>
<body>
<form action="[Link]">
Enter your Name :
<input type = "text" name = "name" size="25"><BR/> <BR/>
<input type="submit" value ="Submit"/>
</form>
</body>
</html>
Rakhi Saxema (Internet Technologies) 9
[Link]
<!-- [Link] -->
<%@ taglib uri="[Link] prefix="c" %>
<html>
<head>
<title> Accessing Form Parameters </title>
</head>
<body>
Hello ${[Link]}
</body>
</html>
Rakhi Saxema (Internet Technologies) 10
Program
[Link] ----- Accept <uname> in HTML form
[Link] ----- Display Hello <uname>
Accept <product> in HTML form
Redirect to another JSP
[Link] ---- Display Hello <uname> and <product>
Rakhi Saxema (Internet Technologies) 11
[Link]
<%@ taglib uri="[Link] prefix="c" %>
<html>
<head>
<title> Session Scope Demo Page 1</title>
</head>
<body>
<form action="[Link]">
Enter your Name :
<input type = "text" name = "name" size="25"><BR/> <BR/>
<input type="submit" value ="Submit"/>
</form>
</body>
</html>
Rakhi Saxema (Internet Technologies) 12
[Link]
<%@ taglib uri="[Link] prefix="c" %>
<html>
<head>
<title> Session Scope Demo Page 2</title>
</head>
<body>
Hello ${[Link]}
<form action="[Link]">
Which Product do you wish to buy?
<input type = "text" name = "product" size="25"><BR/> <BR/>
<input type = "Submit" name = "submit" value="Submit"> <BR/>
</form>
</body>
</html>
Rakhi Saxema (Internet Technologies) 13
[Link]
<%@ taglib uri="[Link] prefix="c" %>
<html>
<head>
<title> Session Scope Demo Page 2</title>
</head>
<body>
Hello ${[Link]}
<c:set var="name" value="${[Link]}" scope=“session”/>
<form action="[Link]">
Which Product do you wish to buy?
<input type = "text" name = "product" size="25"><BR/> <BR/>
<input type = "Submit" name = "submit" value="Submit"> <BR/>
</form>
</body>
</html>
Rakhi Saxema (Internet Technologies) 14
[Link]
<%@ taglib uri="[Link] prefix="c" %>
<html>
<head>
<title> Session Scope Demo Page 3</title>
</head>
<body>
Hello <c:out value="${name}" /> <BR/>
You have ordered <c:out value="${[Link]}" />
</body>
</html>
Rakhi Saxema (Internet Technologies) 15
Counting Page Hits
<%@ taglib prefix="c" uri="[Link] %>
<html>
<head>
<title>Counter page</title>
</head>
<%-- Increment counters --%>
<c:set var="sessionCounter" scope="session“ value="${sessionCounter + 1}" />
<c:set var="applCounter" scope="application“ value="${applCounter + 1}" />
<h1>Counter page</h1>
This page has been visited <b>${sessionCounter}</b> times
within the current session, and <b>${applCounter}</b> times
by all users since the application was started.
</body>
</html>
Rakhi Saxema (Internet Technologies) 16