MIT AITI 2004 JSP – Lecture 4 Sessions and Cookies.

Slides:



Advertisements
Similar presentations
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Advertisements

Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
DT228/3 Web Development multi page applications/ sharing data.
XP Tutorial 9 New Perspectives on JavaScript, Comprehensive1 Working with Cookies Managing Data in a Web Site Using JavaScript Cookies.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
Chapter 25 Utilizing Web Storage.
MIT AITI 2004 JSP – Lecture 2 Get and Post Requests.
Chapter 16 The World Wide Web Chapter Goals Compare and contrast the Internet and the World Wide Web Describe general Web processing Describe several.
About Dynamic Sites (Front End / Back End Implementations) by Janssen & Associates Affordable Website Solutions for Individuals and Small Businesses.
Lecture 7 Interaction. Topics Implementing data flows An internet solution Transactions in MySQL 4-tier systems – business rule/presentation separation.
Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Mark Dixon Page 1 3 – Web applications: Server-side code (JSP)
Server-Side Scripting with Java Server Page, JSP ISYS 350.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
Maintaining State MacDonald Ch. 9 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
1 Maryland ColdFusion User Group Session Management December 2001 Michael Schuler
Web Application Development * These slides have been adapted and modified from CoreServlets course material (Marty Hall) and LUMS cs391 (Umair Javed).
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Chapter 6 Server-side Programming: Java Servlets
Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Session Management.
DAT602 Database Application Development Lecture 16 Java Server Pages Part 2.
7 Chapter Seven Client-side Scripts. 7 Chapter Objectives Create HTML forms Learn about client-side scripting languages Create a client-side script using.
COOKIES and SESSIONS. COOKIES A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each.
Web Database Programming Week 7 Session Management & Authentication.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
CP476 Internet Computing CGI1 Cookie –Cookie is a mechanism for a web server recall info of accessing of a client browser –A cookie is an object sent by.
Session Tracking Lec 31. Revisiting Session Tracking HTTP is a stateless protocol  Every request is considered independent of every other request Many.
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.
WebDev Essential Skills BCIS 3680 Enterprise Programming.
1 Web Programming with Servlets & JSP ASSIGNMENT GUIDELINE.
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
CSC 2720 Building Web Applications Managing Users' States – Cookies, URL-Rewriting, Hidden Fields and Session Management APIs.
Servlets 4 Lec 30 Web Design and Development. Looking Back… Response Redirection  Sending a standard redirect  Sending a redirect to an error page Request.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
Session Tracking Parts of this presentation was provided by SSE.
©SoftMooreSlide 1 Cookies. ©SoftMooreSlide 2 Cookies Basic idea –web application sends a simple name/value pair to the client –when the client connects.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
1 Chapter 22 World Wide Web (HTTP) Chapter 22 World Wide Web (HTTP) Mi-Jung Choi Dept. of Computer Science and Engineering
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
Net-centric Computing Servlets & JSP. Lecture Outline  Tracking Sessions  Cookies  Examples  JSP  Differences between JSP and Servlets  JSP Constructs.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
Distributed Web Systems Cookies and Session Tracking Lecturer Department University.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Java Servlets.
Web Design and Development
ITM 352 Cookies.
Chapter 6 Server-side Programming: Java Servlets
Sessions.
MIS Professor Sandvig MIS 324 Professor Sandvig
Web Systems Development (CSC-215)
Handling State in Java Servlets
Cookies Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when later visiting the.
CSc 337 Lecture 27: Cookies.
Pre-assessment Questions
Introduction to JavaScript
Servlet Session Tracking: Session API
CSc 337 Lecture 25: Cookies.
Presentation transcript:

MIT AITI 2004 JSP – Lecture 4 Sessions and Cookies

What we can do... Build a static Web page –HTML Add dynamic content to a Web page –JSP Expressions, Scriptlets, Declarations, Directives Enable user interaction –Send parameters in request with GET and POST Reduce code duplication –Includes, forwards, request attributes

URL, name = Greg, x = 5,... Request-Response Cycle Web Browser request response HTML, images, files Web Server How do we remember information across requests? For example, how do you recognize later that a user is logged in? Another example: How do you remember what the user has added to an online shopping cart?

What we can’t do... Associate data with a visitor Remember info across requests Remember info across visits Examples –Keep a user logged in –Store a user’s shopping cart –Remember user’s name for next visit

Sessions Data associated with a given visitor Maintained on the Web server across requests Times out if user is idle for a while Perfect for... –Keeping a user logged in –Storing a user’s shopping cart

Session Object Implicit session object Maps Strings to Objects Add data to the session with setAttribute <% Data d = new Data(); session.setAttribute("dataname", d); %> Retrieve data from session with getAttribute Remove data from session with removeAttribute

Session Example 1 Home page home.jsp <% String username = (String)session.getAttribute("name"); if (username == null) { %> <% } %> Welcome, ! Logout

Session Example 2 Login page login.html Login Page Username: Password:

Login Page Snapshot

Session Example 3 Check login page checklogin.jsp <% String username = request.getParameter("username"); String password = request.getParameter("passwd"); if (!password.equals("myPassword")) { %> Error: incorrect password. <% } else if (username.length() == 0) { %> Error: must provide a username. <% } else { session.setAttribute("name", username); %> <% } %>

Incorrect Password Snapshot

Empty Username Snapshot

Correct Password Snapshot

Session Example 4 Logout Page logout.jsp <% session.removeAttribute("name"); %> Logout Page You have been logged out.

Logout Snapshot

Session Recap Request parameters and attributes allow us to send data from page to page across a single request Sessions allow us to maintain data for a given visitor across multiple requests But we still can not maintain data for a given visitor across visits.

Cookies Cookie sits on the client computer Persists, unless specifically deleted by user JSP page can get, set, and delete cookie data Easy way to recognize user across visits without login

Using Cookies Create new Cookie object Add cookie to implicit response object <% Cookie c = new Cookie("site.com", "Greg"); response.addCookie(c); %> Get cookies in request object Delete cookies by setting their max age to zero

Cookie Example 1 home.jsp with cookies <% String username = (String)session.getAttribute("name"); if (username == null) { Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("site.com-name")) { username = cookies[i].getValue(); session.setAttribute("name", username); } if (username == null) { %> <% }... Welcome, !...

Cookie Example 2 checklogin.jsp with cookies <% String password = request.getParameter("passwd"); if (!password.equals("myPassword")) { %> Error: incorrect password. <% } String username = request.getParameter("username"); if (username.length() == 0) { %> Error: must provide a username. <% } else { session.addAttribute("name", username); response.addCookie("site.com-name", username); %> <% } %>

logout.jsp with cookies <% session.removeAttribute("name"); Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("site.com-name")) cookies[i].setMaxAge(0); } %> Logout Page You have been logged out. Cookie Example 3

In Summary Send data between pages within a single request –Request parameters and attributes Maintain data for a visitor across requests –Sessions (temporary data on the server) Maintain data for a visitor across visits from a single client computer –Cookies (data on the client) We cannot maintain data for across visits from multiple computers that is guaranteed to persist –Need a database (permanent data on the server)