Cookie in a servlet. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
Advertisements

J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
Servlets. Applets Special Java programs (without a main) callable from HTML and executed in a graphic context. They can be executed by: a Java enabled.
J0 1 Marco Ronchetti - Servlets e JSP.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
6 Copyright © 2005, Oracle. All rights reserved. Using Advanced Techniques in Servlets.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
CookiesPHPMay-2007 : [‹#›] Maintaining State in PHP Part I - Cookies.
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Apache Tomcat as a container for Servlets and JSP
Java Server Pages (JSP)
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 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.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Servlet Session I: Cookie API Road Map  Creating Cookies  Cookie Attributes  Reading Cookies  Example 1: Basic Counter  Example 2: Tracking Multiple.
Servlet Session Tracking. 2 Persistent information A server site typically needs to maintain two kinds of persistent (remembered) information: Information.
1 CS6320 – Servlet Cookies L. Grewe 2 What is a cookie? Name-value bindings sent by a server to a web browser and then sent back unchanged by the browser.
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
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.
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 Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
ASHIMA KALRA.  INTRODUCTION TO JSP INTRODUCTION TO JSP  IMPLICIT OBJECTS IMPLICIT OBJECTS  COOKIES COOKIES.
1 Lecture 10 Server-side Programming: Java Servlets.
Chapter 11 © 2005 by Addison Wesley Longman, Inc Overview of Servlets - A servlet is a compiled Java class - Servlets are executed on the server.
Java Servelets. What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed.
Chapter 8 Handling Cookies. Cookie Cookies are small bits of textual information that a Web server sends to a browser and that the browser later returns.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
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.
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.
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.
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.
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.
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.
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.
Slides © Marty Hall, book © Sun Microsystems Press 1 Handling Cookies Core Servlets & JSP book: More.
Cookies. Cookie A cookie is a piece of textual information Send by the Web server to the client browser Every time the browser visits the Web site again,
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:
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
ORIGINAL BY WENDY LIU CURRENT API: Java Servlet and JSP 1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Cookies in Servlet A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value,
©SoftMooreSlide 1 Cookies. ©SoftMooreSlide 2 Cookies Basic idea –web application sends a simple name/value pair to the client –when the client connects.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
1 Servlets – Part 2 Representation and Management of Data on the Web.
April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading Room 129,
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
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.
Distributed Web Systems Cookies and Session Tracking Lecturer Department University.
COMP9321 Web Application Engineering Semester 2, 2017
Servlets.
Chapter 6 Server-side Programming: Java Servlets
Sessions.
CSE 597B Computational Issues in Ecommerce
CS320 Web and Internet Programming Cookies and Session Tracking
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
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.
Presentation transcript:

Cookie in a servlet. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting the same Web site or domain later i.e cookie is stored on client and contain state information. Cookies are valuable for tracing users activity. » Identifying a user during an e-commerce session, » avoiding username and password, » customizing a site etc.

Providing convenience to the user and added value to the site owner is the purpose behind cookies. cookies are not a serious security threat. Cookies are never interpreted or executed in any way. Browsers generally only accept 20 cookies per site and 300 cookies total, and each cookie is limited to 4KB,

However, even though they don't present a serious security threat, they can present a significant threat to privacy.

The cookies class A servlet can write a cookie to user’s machine via addCookie() method of HttpServletRespoonse interface. Information that is saved for each cookie – Name – Value – Expiration date – Domain and path of cookie

Constructor for cookie Cookie (string name, String value)

String getComment() / void setComment (String s) – Gets/sets a comment associated with this cookie. String getDomain() / void setDomain (String d) – Gets/sets the domain to which cookie applies. Normally, cookies are returned only to the exact hostname that sent them. You can use this method to instruct the browser to return them to other hosts within the same domain. Note that the domain should start with a dot (e.g..prenhall.com), and must contain two dots for non-country domains like.com,.edu, and.gov, and three dots for country domains like.co.uk and.edu.es. Int getMaxAge() / void setMaxAge(int secs) – Gets/sets how much time (in seconds) should elapse before the cookie expires. If you don't set this, the cookie will present only for the current session (i.e. until the user quits the browser), and will not be stored on disk. mycookie.setMaxAge(30*24*60*60 ); String getName() – Gets/sets the name of the cookie. The name and the value are the two pieces you virtually always care about. Since the getCookies method of HttpServletRequest returns an array of Cookie objects, it is common to loop down this array until you have a particular name, then check the value with getValue.

String getPath () / void setPath (string p) – Gets/sets the path to which this cookie applies. If you don't specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories. This method can be used to specify something more general. For example, someCookie.setPath("/") specifies that all pages on the server should receive the cookie. Note that the path specified must include the current directory. Boolean getSecure() – Returns true if cookie must be sent using only secure protocol otherwise false Void setSecure (boolean secure) – That cookies will only send when protocol is secure. String getValue() / void setValue (string v) – Gets/sets the value associated with the cookie. Again, the name and the value are the two parts of a cookie that you almost always care about. getVersion/setVersion

Placing Cookies in the Response Headers The cookie is added to the Set-Cookie response header by means of the addCookie method of HttpServletResponse. Here's an example: Cookie userCookie = new Cookie("user", "uid1234"); response.addCookie(userCookie);

Set cookie import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class CountLogin extends HttpServlet { Protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html”); PrintWriter out = response.getWriter(); String data = request.getParameter(“data”); Cookie c = new Cookie(“mycookie”,data); response. addCookie(c); }

Reading Cookies from the Client read the cookies that come back from the client, you call getCookies on the HttpServletRequest. Cookie[] c = request.getCookies(); if( (c != null) && ( c.lehgth > 0)) { for(int i=0; i<c.length;i++) { Cookie c1 = c[i]; out.println(c1.getName() + c1.getValue()) } }else {out.println (“no cookies found”);}

Get cookies import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; public class CountLogin extends HttpServlet { Protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html”); PrintWriter out = response.getWriter(); Cookie[] c = request.getCookies(); for(i=0;i<c.length;i++) { String name = c[i].getName(); String val = c[i].getValue(); out.println ( name + “ ” + value); }