Cookies are text files stored on clients' computers that track information. JSP supports HTTP cookies using underlying servlet technology. There are three steps to identifying returning users with cookies: 1) the server sends cookie data to browsers, 2) browsers store this on local machines, 3) browsers send stored cookie data to servers on subsequent requests, allowing servers to identify users. This document discusses how to set, access, and delete cookies using JSP programs.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
68 views6 pages
The Anatomy of A Cookie
Cookies are text files stored on clients' computers that track information. JSP supports HTTP cookies using underlying servlet technology. There are three steps to identifying returning users with cookies: 1) the server sends cookie data to browsers, 2) browsers store this on local machines, 3) browsers send stored cookie data to servers on subsequent requests, allowing servers to identify users. This document discusses how to set, access, and delete cookies using JSP programs.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6
Cookies are text files stored on the client computer and they are kept for various
information tracking purpose. JSP transparently supports HTTP cookies using
underlying servlet technology. There are three steps involved in identifying returning users: Server script sends a set of cookies to the browser. For example name, age, or identification number etc. Browser stores this information on local machine for future use. When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user or may be for some other purpose as well. This chapter will teach you how to set or reset cookies, how to access them and how to delete them using JSP programs. The Anatomy of a Cookie: Cookies are usually set in an HTTP header (although JavaScript can also set a cookie directly on a browser). A JSP that sets a cookie might send headers that look something like this: HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT; path=/; domain=tutorialspoint.com Connection: close Content-Type: text/html As you can see, the Set-Cookie header contains a name value pair, a GMT date, a path and a domain. The name and value will be URL encoded. The expires field is an instruction to the browser to "forget" the cookie after the given time and date. If the browser is configured to store cookies, it will then keep this information until the expiry date. If the user points the browser at any page that matches the path and domain of the cookie, it will resend the cookie to the server. The browser's headers might look something like this: GET / HTTP/1.0 Connection: Keep-Alive User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc) Host: zink.demon.co.uk:1126 Accept: image/gif, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: name=xyz A JSP script will then have access to the cookies through the request method request.getCookies() which returns an array of Cookie objects.
servlet Cookies Methods: Following is the list of useful methods associated with Cookie object which you can use while manipulating cookies in JSP: S.N. Method & Description 1 public void setDomain(String pattern) This method sets the domain to which cookie applies, for example tutorialspoint.com. 2 public String getDomain() This method gets the domain to which cookie applies, for example tutorialspoint.com. 3 public void setMaxAge(int expiry) This method sets how much time (in seconds) should elapse before the cookie expires. If you don't set this, the cookie will last only for the current session. 4 public int getMaxAge() This method returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown. 5 public String getName() This method returns the name of the cookie. The name cannot be changed after creation. 6 public void setValue(String newValue) This method sets the value associated with the cookie. 7 public String getValue() This method gets the value associated with the cookie. 8 public void setPath(String uri) This method 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. 9 public String getPath() This method gets the path to which this cookie applies. 10 public void setSecure(boolean flag) This method sets the boolean value indicating whether the cookie should only be sent over encrypted (i.e. SSL) connections. 11 public void setComment(String purpose) This method specifies a comment that describes a cookie's purpose. The comment is useful if the browser presents the cookie to the user. 12 public String getComment() This method returns the comment describing the purpose of this cookie, or null if the cookie has no comment.
Setting Cookies with JSP: Setting cookies with JSP involves three steps: (1) Creating a Cookie object: You call the Cookie constructor with a cookie name and a cookie value, both of which are strings. Cookie cookie = new Cookie("key","value"); Keep in mind, neither the name nor the value should contain white space or any of the following characters: [ ] ( ) = , " / ? @ : ; (2) Setting the maximum age: You use setMaxAge to specify how long (in seconds) the cookie should be valid. Following would set up a cookie for 24 hours. cookie.setMaxAge(60*60*24); (3) Sending the Cookie into the HTTP response headers: You use response.addCookie to add cookies in the HTTP response header as follows: response.addCookie(cookie); Example: Let us modify our Form Example to set the cookies for first and last name. <% // Create cookies for first and last names. Cookie firstName = new Cookie("first_name", request.getParameter("first_name")); Cookie lastName = new Cookie("last_name", request.getParameter("last_name"));
// Set expiry date after 24 Hrs for both the cookies. firstName.setMaxAge(60*60*24); lastName.setMaxAge(60*60*24);
// Add both the cookies in the response header. response.addCookie( firstName ); response.addCookie( lastName ); %> <html> <head> <title>Setting Cookies</title> </head> <body> <center> <h1>Setting Cookies</h1> </center> <ul> <li><p><b>First Name:</b> <%= request.getParameter("first_name")%> </p></li> <li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </body> </html> Let us put above code in main.jsp file and use it in the following HTML page:
<html> <body> <form action="main.jsp" method="GET"> First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> </body> </html> Keep above HTML content in a file hello.jsp and put hello.jsp and main.jsp in <Tomcat-installation-directory>/webapps/ROOT directory. When you would access http://localhost:8080/hello.jsp, here is the actual output of the above form. First Name: Last Name: Try to enter First Name and Last Name and then click submit button. This would display first name and last name on your screen and same time it would set two cookies firstName and lastName which would be passed back to the server when next time you would press Submit button. Ntsc:
[hide]This article has multiple issues. Please help improve it or discuss these issues on the talk page. This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. (August 2011)
Television encoding systems by nation; countries using the NTSC system are shown in green. NTSC, named after the National Television System Committee, [1] is the analog television system that was used in most of the Americas (except Brazil, Argentina, Paraguay, Uruguay and French Guiana); Myanmar; South Korea; Taiwan; Japan; the Philippines; and some Pacific island nations and territories (see map). NTSC The analog video color format used to broadcast television signals through North America, half of South America and parts of Asia. It operates on a 60Hz power grid, displays 30 picture frames per second and 525 lines of information per picture. PAL The analog video color format used to broadcast television signals through much of Europe, Asia, Oceania, half of South America and parts of Africa. It operates on a 50Hz power grid, displays 25 picture frames per second and ties in 625 lines of information with every frame. SECAM The analog video color format used in France, parts of Africa and much of Eastern Europe (in conjunction with PAL transmissions).