Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scope and State Handling in JSPs

Similar presentations


Presentation on theme: "Scope and State Handling in JSPs"— Presentation transcript:

1 Scope and State Handling in JSPs
CS Programming Languages for Web Applications

2 Session State Information
review Session State Information The initial versions of the web suffered from a lack of state If a web app needed multiple screens, there was no way for state to be accumulated or stored This is due to the stateless property of HTTP HTML Form Server Page State data response S1 S1+S2+S3 Form1 Form2 Form3 Server Form4 S1+S2 S1+S2+S3+S4 CS 4640

3 review Session Tracking Web sites that are service-oriented or e-commerce need to maintain user state This is called session tracking Session = a series of related interactions between a client and a web server Session tracking = keeping data between multiple HTTP requests The web brings in unique constraints: HTTP is connectionless Web apps are distributed CS 4640

4 State on the Web Two assumptions (traditional software):
review State on the Web Two assumptions (traditional software): The software components share physical memory The program runs to completion with active memory These assumptions are violated in web apps due to Distributed software components Connectionless nature of HTTP Need ways to store and access variables and objects to keep state in web apps CS 4640

5 Context (application)
review Context Scope Container Engine session object 1 session object 2 Servlet S2 Servlet S1 JSP 3 JSP 1 context object JSP 2 Session 1 Session 2 Context (application) CS 4640

6 JSP Scope and State Management
JSPs formalize this with four separate scopes Page : Within the same program component (web page) Request : Within the same request Session : Within all requests from the same session Application : Within all sessions for one servlet context Each can be accessed by different sets of program components Some exist for different periods of time CS 4640

7 Sharing Data with Scope
application session request forward page request Client 1 page request forward request Client 2 request page Scope and Handling State in JSPs, slides from SWE 642, reproduced with permission from J. Offutt CS 4640

8 Activity Work in groups, you have 5 mins, discuss and come up with a design decision Imagine you are going to design a web app for students to take online quiz. There are multiple students, let's say 20 students There is a pool of multiple choice questions that will be used by all students Each student needs to login to the system and logout when finish Each student will answer 10 multiple choice questions For each question, a student selects an answer then click submit button (=> 1 request) The web app stores the answer and produces a screen which displays the next question => 10 request / 1 session Summarize your ideas to share with the class When does a session start / end? How many sessions? How to distinguish between sessions? How do we keep track of each student's session? What scope? How do we handle state of each request? What scope? How do we handle the pool of the questions? These questions are accessible by all students who take this quiz. What scope? CS 4640

9 Sharing Data with Scope (2)
Using JSP Scriptlets getParameter(); // retrieves client form data request.getAttribute(), request.setAttribute(); session.getAttribute(), session.setAttribute(); context.getAttribute(), context.setAttribute(); For example : <% session.setAttribute (“ID”, request.getParameter (“ID”)); %> Predefined variable Application scope CS 4640

10 Sharing Data with Scope (3)
The previous approach makes the code kind of clumsy Alternative approach – expanded use of JavaBean A Java Bean is a Java class with 3 characteristics: public class public constructor with no arguments public get and set methods (called getters and setters) Property : A special, simple data object (that is, variable) getName () … <jsp:getProperty> setName (String name) … <jsp:setProperty> Note that a bean is not a Java language feature, but a design convention (pattern) CS 4640

11 useBean Action Tag useBean causes a JavaBean object to be instantiated
useBean gives a name to the new object (id=) useBean defines the scope useBean declares the location (bean details) CS 4640

12 Java Bean Example Syntax for using a bean
Note: scope=“application” allows Beans to be shared among different servlets – DON’T USE IT! Lead to interactions among each other … more later … Converts to Java import statement, Java 4 requires all imports to be packages page import=“jspexamples.*” %> <jsp:usebean id=“letterColor” class=“AlphabetCode” scope=“page” /> ID name to use for object (AlphabetCode LetterColor = new … ) Name of class JSPs offer several useful scopes for variables … CS 4640

13 Properties of Beans setProperty gives a value to a property in a bean
<jsp:setProperty name=“langBean” property=“language” value=“Java”/> Equivalent to the call: langBean.setLanguage (“Java”); <jsp:setProperty name=“langBean” property=“*” /> Sets all of the properties with values from HTML form getProperty retrieves the value of a property <jsp:getProperty name=“langBean” property=“language”/> Equivalent to the call: langBean.getLanguage(); CS 4640

14 Properties of Beans Case of property name is very important
Property must begin with a lower case letter (“language”) Getters and setters must have the property name start with a capital letter ( setLanguage(), getLanguage() ) CS 4640

15 Note on Java Beans Using Java Beans increases separation between the HTML and Java The Beans / Property pattern provides a convenient standard for implementing standard Java classes JSP’s useBean uses Java reflection to translate property names (for example, “language”) to method calls that are assumed to exist (“setLanguage()” and “getLanguage()”) The bean does not have to have an object with the name of the property, as long as it has a getter or setter CS 4640

16 Summary: Sharing Data with Java Bean
Use the scope attribute in the <jsp:useBean> action <jsp:useBean id=“languageBean” class=“lang.LanguageBean” scope=“session”> <jsp:getProperty name=“languageBean” property=“name”> Scoping keywords: scope=“request” for the request object scope=“session” for the session object scope=“application” for the context object The page scope is default – local variables CS 4640

17 Summary Programmers often get state management wrong
They learned “how” without learning “why” (the theory) They don’t understand the differences in the various scopes They forget to consider which scope to use as part of design State management is very different from traditional programming These scopes are quite powerful New frameworks beyond JEE often add different scopes and different semantics on the same scopes CS 4640


Download ppt "Scope and State Handling in JSPs"

Similar presentations


Ads by Google