Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Advertisements

1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
Servlets and a little bit of Web Services Russell Beale.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
JSP Java Server Pages Reference:
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
Servlet Session Tracking. 2 Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information: Information.
COMP201 Java Programming Part III: Advanced Features Topic 15: Servlets Materials Based on The Java Tutorial
Servlet Session Tracking II Session API All material and examples are from
COMP201 Java Programming Part III: Advanced Features Topic 14: Servlets Servlets and JavaServer Pages (JSP) 1.0: A Tutorial
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
HTTP HyperText Transfer Protocol Stateless request/response client-server protocol Requests: Method: GET, POST, HEAD, TRACE, OPTIONS, PUT, DELETE.
Chapter 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Comp2513 Java Servlets and Sessions Daniel L. Silver, Ph.D.
Servlets, Sessions, and Cookies Lecture 8 cs193i – Internet Technologies Summer 2004 Kelly Shaw, Stanford University.
Java Servlet Technology. Introduction Servlets are Java programs that run on a Web server, handle HTTP requests and build Web pages Servlet specification.
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
COMP201 Java Programming Part III: Advanced Features Topic 14: Servlets Servlets and JavaServer Pages (JSP) 1.0: A Tutorial
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Session tracking There are a number of problems that arise from the fact that HTTP is a "stateless" protocol. In particular, when you are doing on- line.
Session Tracking - 2 Lec 32. Last Lecture Review  Session Tracking – why?  Need to store state – typical solutions Cookies – already learned URL Rewriting.
Running Servlets JSDK2.1 default.cfg : Web Server configuration information batch files to start and stop server Servlet properties in /webpages/WEB-INF.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Web Application Development * These slides have been adapted and modified from CoreServlets course material (Marty Hall) and LUMS cs391 (Umair Javed).
Java Servlets & Java Server Pages Lecture July 2013.
Threading Servlets Can handle multiple clients concurrently Shared resources must be synchronized or create a servlet that handles one request at a time.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
S ERVLETS Cookies Handling 5-Dec-15. S ERVLETS - C OOKIES H ANDLING Cookies are text files stored on the client computer and they are kept for various.
Li Tak Sing COMPS311F. A web page that counts the number of times that you have visited the page. You can try the page at:
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Slides © Marty Hall, book © Sun Microsystems Press 1 Session Tracking Core Servlets & JSP book: More.
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.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
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.
8-Mar-16 More About Servlets Session Tracking. Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information:
Introduction to Servlets
Session Tracking in Servlets
Chapter 6 Server-side Programming: Java Servlets
Java Servlets 9/18/2018.
Sessions.
Java Servlets II: Session Tracking
Servlet Session Tracking
Servlet.
Chapter 26 Servlets.
Distributed Computing, M. L. Liu
Servlets and Java Server Pages
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Session Tracking Techniques
Web Search Interfaces.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlet Session Tracking: Session API
Presentation transcript:

Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their state information. Servlets use information in cookie

Session Tracking Bookstore uses to keep track of books ordered Obtain session object for a user ( HttpSession ) (boolean for creation) Store or get data from object Invalidate session Session properties (session identifier) Key-value pairs for application data

Steps Obtain Session for a user Store or get data from session object Invalidate the session (manual or automatic) Bookstore uses session to keep track of books a user orders Shared by all servlets in application

Handling All Browsers Session Tracking uses cookies by default to associate session id with user If browser doesn’t support cookies, must use URL rewriting (not supported by servletrunner) Session Id included in links: session id sent as part of URL

public class CatalogServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the user's session and shopping cart, the Writer, etc.... // then write the data of the response out.println(" " +...);... // Get the catalog and send it, nicely formatted BookDetails[] books = database.getBooksSortedByTitle();... for(int i=0; i < numBooks; i++) {... //Print out info on each book in its own two rows out.println(" " +... "<a href=\"" + response.encodeUrl("/servlet/bookdetails?bookId=" + bookId) + "\"> " + books[i].getTitle() + " " +... "<a href=\"" + response.encodeUrl("/servlet/catalog?Buy=" + bookId) + "\"> Add to Cart " + }

Using Cookies Key-value pair Way for server to store information on client Server appends to HTTP response headers Client appends to HTTP request headers Cookies are single-valued

Procedure To send Cookie –Instantiate Cookie Object –Set attributes –send the cookie Get information from Cookie –Retrieve all cookies from the user’s request –Find cookie with name interested in –Get values from cookies

Cookie Drawbacks Can only be strings take up client disk space browsers limit their number and size

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BookDBServlet database = (BookDBServlet) getServletConfig().getServletContext().getServlet("bookdb"); // Check for pending adds to the shopping cart String bookId = request.getParameter("Buy"); //If the user wants to add a book, remember it by adding a cookie if (bookId != null) { Cookie getBook = new Cookie("Buy", bookId);... } // set content-type header before accessing the Writer response.setContentType("text/html"); // now get the writer and write the data of the response PrintWriter out = response.getWriter(); out.println(" " + " Book Catalog " +...);... }

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... //If the user wants to add a book, remember it by adding a cookie if (values != null) { bookId = values[0]; Cookie getBook = new Cookie("Buy", bookId); getBook.setComment("User wants to buy this book " + "from the bookstore."); }... }

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... /* Handle any pending deletes from the shopping cart */ String bookId = request.getParameter("Remove");... if (bookId != null) { // Find the cookie that pertains to the book to remove... // Delete the cookie by setting its maximum age to zero thisCookie.setMaxAge(0);... } // also set content type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); //Print out the response out.println(" " + " Your Shopping Cart " +...);

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... //If the user wants to add a book, remember it by adding a cookie if (values != null) { bookId = values[0]; Cookie getBook = new Cookie("Buy", bookId); getBook.setComment("User has indicated a desire " + "to buy this book from the bookstore."); response.addCookie(getBook); }... }

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... /* Handle any pending deletes from the shopping cart */ String bookId = request.getParameter("Remove");... if (bookId != null) { // Find the cookie that pertains to the book to remove Cookie[] cookies = request.getCookies();... // Delete the book's cookie by setting its maximum age to zero thisCookie.setMaxAge(0); } // also set content type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); //Print out the response out.println(" " + " Your Shopping Cart " +...);

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... /* Handle any pending deletes from the shopping cart */ String bookId = request.getParameter("Remove");... if (bookId != null) { // Find the cookie that pertains to that book Cookie[] cookies = request.getCookies(); for(i=0; i < cookies.length; i++) { Cookie thisCookie = cookie[i]; if (thisCookie.getName().equals("Buy") && thisCookie.getValue().equals(bookId)) { // Delete the cookie by setting its maximum age to zero thisCookie.setMaxAge(0); } // also set content type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); //Print out the response out.println(" " + " Your Shopping Cart " +...);

HTTP 1.0 Tokens Tokens don’t contain special characters reserved by RFC2068 Alphanumeric OK URLEncoder: Converts string to MIME format x-www-form-urlencoded Converts spaces to “+” Other characters to 3 character hex number “%xy”

Applets and Servlets Applet programming problem not servlet Let applet use http to communicate to servlet servlet can communicate with applet in text, binary, or html

Eliza