Scope and State Handling in JSPs

Slides:



Advertisements
Similar presentations
JSP and Servelets.
Advertisements

Apache Struts Technology
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 91 Scopes of Variables and JavaBeans JavaServer Pages By Xue Bai.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
Internet Technologies 1 Master of Information System Management Java Server Faces Model/View/Controller Design Pattern for Web Development Slides.
Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by.
Q: According to Intel, the Pentium conforms to the IEEE standards 754 and 854 for floating point arithmetic. If you fly in aircraft designed using a Pentium,
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Struts 2.0 an Overview ( )
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans Components in JSP Documents. Contents 1. Why using beans? 2. What are beans? 3. Using Beans: Basic Tasks 4. Sharing Beans 5. Practice.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
What Are Beans? beans are simply Java classes that are written in a standard format. A bean class must have a zero-argument (default) constructor. A bean.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
DAT602 Database Application Development Lecture 16 Java Server Pages Part 2.
Java Server Pages (JSP) Provide a cross-platform framework for creating dynamic web contents JSP Components: Static HTML/XML parts Special JSP Tags Snippets.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
3/6/00jsp00 1 Java Server Pages Nancy McCracken Northeast Parallel Architectures Center at Syracuse University.
OOSSE Week 8 JSP models Format of lecture: Assignment context JSP models JSPs calling other JSPs i.e. breaking up work Parameter passing JSPs with Add.
Slides © Marty Hall, book © Sun Microsystems Press 1 Using JavaBeans with JSP Core Servlets & JSP book:
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 14 Using JavaBeans Components in JSP Documents.
JSP Java Server Pages. Hello, !
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.
Upsorn Praphamontripong CS Design and Implementation of Software for the Web Fall 2016 State Handling on the Web.
JSP Action Elements Lec - 37.
JSP Based on
Introduction to Unique Aspects of Web Applications
Structure of a web application
Java Server Pages By: Tejashri Udavant..
COMP9321 Web Application Engineering Semester 2, 2017
Scripted Page Web App Development (Java Server Pages)
Web Software Model CS 4640 Programming Languages for Web Applications
JavaBeans and JSP CS-422.
Introduction to Java Bean
Design and Maintenance of Web Applications in J2EE
MSIS 655 Advanced Business Applications Programming
Introduction to Java Server Pages
Introduction to Java Bean
Java Server Pages (JSP)
Java Server Pages B.Ramamurthy.
Handling State in Java Servlets
Handling State in Web Applications
CS320 Web and Internet Programming Java Beans
Scope and State Handling in Java Server Pages
State Handling CS 4640 Programming Languages for Web Applications
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Scripted Page Web Application Development (Java Server Pages)
Web Technologies Java Beans & JSP
State Handling CS 4640 Programming Languages for Web Applications
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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