0% found this document useful (0 votes)
85 views

Java Server Pages Chapter 10: Sharing Data Between: JSP Pages, Requests, and Users

This document discusses sharing data between JSP pages, requests, and users. It covers passing control and data between pages using the MVC model. Data can be shared using different scopes like page, request, session, and application. Form data can be passed between pages using request parameters and the param object. Data can also be stored in scopes like session using the set tag. Multiple pages are used to process a single user request, passing data and control between pages.

Uploaded by

chitra devi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Java Server Pages Chapter 10: Sharing Data Between: JSP Pages, Requests, and Users

This document discusses sharing data between JSP pages, requests, and users. It covers passing control and data between pages using the MVC model. Data can be shared using different scopes like page, request, session, and application. Form data can be passed between pages using request parameters and the param object. Data can also be stored in scopes like session using the set tag. Multiple pages are used to process a single user request, passing data and control between pages.

Uploaded by

chitra devi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

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="userinfoinput.jsp" />
• 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="userinfoinput.jsp" >
<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


FormExample.jsp
<!-- FormExample.jsp -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> Accessing Form parameters </title>
</head>

<body>
<form action="Welcome.jsp">
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


Welcome.jsp
<!-- Welcome.jsp -->

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<html>
<head>
<title> Accessing Form Parameters </title>
</head>

<body>
Hello ${param.name}
</body>
</html>

Rakhi Saxema (Internet Technologies) 10


Program

GetUserInfo.jsp ----- Accept <uname> in HTML form


WelcomeUser.jsp ----- Display Hello <uname>
Accept <product> in HTML form
Redirect to another JSP
DisplayuserInfo.jsp ---- Display Hello <uname> and <product>

Rakhi Saxema (Internet Technologies) 11


GetUserInfo.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> Session Scope Demo Page 1</title>
</head>

<body>
<form action="WelcomeUser.jsp">
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


WelcomeUser.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> Session Scope Demo Page 2</title>
</head>

<body>
Hello ${param.name}
<form action="DisplayUserInfo.jsp">
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


WelcomeUser.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> Session Scope Demo Page 2</title>
</head>

<body>
Hello ${param.name}
<c:set var="name" value="${param.name}" scope=“session”/>
<form action="DisplayUserInfo.jsp">
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


DisplayUserInfo.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" 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="${param.product}" />

</body>
</html>

Rakhi Saxema (Internet Technologies) 15


Counting Page Hits
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<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

You might also like