Scope and State Handling in JSP

Similar documents
Java Server Page (JSP)

Introduction to Java Server Pages. Enabling Technologies - Plug-ins Scripted Pages

JSP MOCK TEST JSP MOCK TEST IV

A JavaBean is a class file that stores Java code for a JSP

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM Advanced Internet Technology Lab.

JavaServer Pages. What is JavaServer Pages?

112. Introduction to JSP

112-WL. Introduction to JSP with WebLogic

COMP201 Java Programming

Java Server Pages. JSP Part II

JSP. Basic Elements. For a Tutorial, see:

COMP9321 Web Application Engineering

JSP - ACTIONS. There is only one syntax for the Action element, as it conforms to the XML standard:

Java E-Commerce Martin Cooke,

COMP9321 Web Application Engineering

Advanced Java Programming

COMP9321 Web Application Engineering

CE212 Web Application Programming Part 3

directive attribute1= value1 attribute2= value2... attributen= valuen %>

A Gentle Introduction to Java Server Pages

Presentation and content are not always well separated. Most developers are not good at establishing levels of abstraction in JSPs

Fast Track to Java EE 5 with Servlets, JSP & JDBC

20/08/56. Java Technology, Faculty of Computer Engineering, KMITL 1

PES INSTITUTE OF TECHNOLOGY, SOUTH CAMPUS DEPARTMENT OF MCA INTERNAL TEST (SCHEME AND SOLUTION) II

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

CISH-6510 Web Application Design and Development. JSP and Beans. Overview

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

1. Introduction. 2. Life Cycle Why JSP is preferred over Servlets? 2.1. Translation. Java Server Pages (JSP) THETOPPERSWAY.

Using JavaBeans with JSP

Web Presentation Patterns (controller) SWEN-343 From Fowler, Patterns of Enterprise Application Architecture

Developing Applications with Java EE 6 on WebLogic Server 12c

ADVANCED JAVA TRAINING IN BANGALORE

JavaServer Pages (JSP)

Model View Controller (MVC)

Experiment No: Group B_2

Module 5 Developing with JavaServer Pages Technology

Courses For Event Java Advanced Summer Training 2018

Module 3 Web Component

GUJARAT TECHNOLOGICAL UNIVERSITY

Fast Track to Java EE

JavaServer Pages. Juan Cruz Kevin Hessels Ian Moon

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

About the Authors. Who Should Read This Book. How This Book Is Organized

JSP source code runs on the web server via JSP Servlet Engine. JSP files are HTML files with special Tags

More JSP. Advanced Topics in Java. Khalid Azim Mughal Version date: ATIJ More JSP 1/42

ADVANCED JAVA COURSE CURRICULUM

Notes on Chapter Three

Java.. servlets and. murach's TRAINING & REFERENCE 2ND EDITION. Joel Murach Andrea Steelman. IlB MIKE MURACH & ASSOCIATES, INC.

LTBP INDUSTRIAL TRAINING INSTITUTE

SSC - Web development Model-View-Controller for Java Servlet

Introduction to JSP and Servlets Training 5-days

Jeff Offutt. SWE 432 Design and Implementation of Software for the Web. Web Applications

Component Based Software Engineering

FINALTERM EXAMINATION Spring 2009 CS506- Web Design and Development Solved by Tahseen Anwar

JSP - SYNTAX. Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP:

JSP (Java Server Page)

01KPS BF Progettazione di applicazioni web

Principles and Techniques of DBMS 6 JSP & Servlet

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

Component-Based Software Engineering. ECE493-Topic 5 Winter Lecture 26 Java Enterprise (Part D)

Contents at a Glance

Struts Lab 3: Creating the View

Integrating Servlets and JavaServer Pages Lecture 13

Session 20 Data Sharing Session 20 Data Sharing & Cookies

ddfffddd CS506 FINAL TERMS SOLVED BY MCQS GHAZAL FROM IEMS CAMPUS SMD CS506 Mcqs file solved by ghazal

JSP: Servlets Turned Inside Out

J2EE Development. Course Detail: Audience. Duration. Course Abstract. Course Objectives. Course Topics. Class Format.

JAVA. Duration: 2 Months

Exercise. (1) Which of the following can not be used as the scope when using a JavaBean with JSP? a. application b. session c. request d.

Session 11. Expression Language (EL) Reading

Test Composition. Performance Summary

Session 21. Expression Languages. Reading. Java EE 7 Chapter 9 in the Tutorial. Session 21 Expression Languages 11/7/ Robert Kelly,

Java SE7 Fundamentals

Université du Québec à Montréal

Basic Principles of JSPs

a. Jdbc:ids://localhost:12/conn?dsn=dbsysdsn 21. What is the Type IV Driver URL? a. 22.

AN ISO 9001:2008 CERTIFIED COMPANY ADVANCED. Java TRAINING.

First Simple Interactive JSP example

CSE 336. Introduction to Programming. for Electronic Commerce. Why You Need CSE336

Java Server Pages, JSP

SUMMARY: MODEL DRIVEN SECURITY

LTBP INDUSTRIAL TRAINING INSTITUTE

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

Oracle 10g: Build J2EE Applications

Oracle - Developing Applications for the Java EE 7 Platform Ed 1 (Training On Demand)

COMP9321 Web Application Engineering

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

11.1 Introduction to Servlets

Call: Core&Advanced Java Springframeworks Course Content:35-40hours Course Outline

SPRING MOCK TEST SPRING MOCK TEST I

Unit 5 JSP (Java Server Pages)

MARATHWADA INSTITUTE OF TECHNOLOGY, AURANGABAD DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS ADVANCE JAVA QUESTION BANK

Six Things Groovy Can Do For You

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, JSPs 1

UNIT -I PART-A Q.No Question Competence BTL

J2EE - Version: 25. Developing Enterprise Applications with J2EE Enterprise Technologies

Chapter 10 Servlets and Java Server Pages

JAVA 2 ENTERPRISE EDITION (J2EE)

Ch04 JavaServer Pages (JSP)

Transcription:

Scope and State Handling in JSP CS 4640 Programming Languages for Web Applications [Based in part on SWE432 and SWE632 materials by Jeff Offutt] [Robert W. Sebesta, Programming the World Wide Web] 1

Session State Information review The initial versions of the web suffered from a lack of state HTML Form State data Server response HTML Page 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 In reality, we may want to keep track of the information (i.e., state) Form1 Form2 Form3 S1+S2 S1 S1 S1+S2 S1+S2+S3 S1+S2+S3 Form4 S1+S2+S3+S4 Server Server Server Server 2

Session Tracking review 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 3

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

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

JSP: Sharing Data with Session and Context Objects Using JSP Scriptlets getparameter(); // retrieves client form data session.getattribute() and session.setattribute(); context.getattribute() and context.setattribute(); For example : Application scope <% session.setattribute ( ID, request.getparameter ( ID )); %> Predefined variable 6

Sharing Data with Scope 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: 1. public class 2. public constructor with no arguments 3. 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) 7

JSP Scope and State Management JSPs formalize this with four separate scopes 1. Page : Within the same program component (web page) 2. Request : Within the same request 3. Session : Within all requests from the same session 4. 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 8

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

usebean Action Tag usebean Causes a JavaBean object to be instantiated Gives a name to the new object (id=) Defines the scope (scope=) Declares the location (bean details) 10

Java Bean Example Syntax for using a bean Converts to Java import statement Java bean should always be in a package as required by most Web servers <%@ page import= jspexamples.* %> <jsp:usebean id= lettercolor class= AlphabetCode scope= page /> ID name to use for object AlphabetCode lettercolor = new Name of class Defines accessibility JSPs offer several useful scopes for variables (page, request, session, application) Note: scope= application allows Beans to be shared among different servlets à lead to interactions among each other 11

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(); Property begin with a lower case letter Getters and setters property name start with a capital letter 12

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 13

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 14

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 Java EE often add different scopes and different semantics on the same scopes 15