Saturday 30 March 2013

Cookies

Cookies !

Cookies are short pieces of data sent by web servers to the client browser. The cookies are saved to clients hard disk in the form of small text file. Cookies helps the web servers to identify web users, by this way server tracks the user. Cookies pay very important role in the session tracking.
Cookie Class
In JSP cookie are the object of the class javax.servlet.http.Cookie. This class is used to creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.
The getCookies() method of the request object returns an array of Cookie objects. Cookies can be constructed using the following code:
Cookie(java.lang.String name, java.lang.String value)

Methods of Cookie objects
getComment()
Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.
getMaxAge()
Returns the maximum specified age of the cookie.
getName()
Returns the name of the cookie.
getPath()
Returns the prefix of all URLs for which this cookie is targeted.
getValue()
Returns the value of the cookie.
setComment(String)
If a web browser presents this cookie to a user, the cookie's purpose will be described using this comment.
setMaxAge(int)
Sets the maximum age of the cookie. The cookie will expire after that many seconds have passed. Negative values indicate the default behavior: the cookie is not stored persistently, and will be deleted when the user web browser exits. A zero value causes the cookie to be deleted
setPath(String)
This cookie should be presented only with requests beginning with this URL.
setValue(String)
Sets the value of the cookie. Values with various special characters (white space, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the "at" sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers.


Creating & Reading Cookies !

Create a Cookie:
<HTML>
<HEAD>
<TITLE>Reading a Cookie</TITLE>
</HEAD> 

<BODY>
<H1>Reading a Cookie</H1> 

<%
Cookie cookie1 = new Cookie("message", "Hello!");
cookie1.setMaxAge(24 * 60 * 60);
response.addCookie(cookie1);
%>
<P>refresh to see the Cookie</p>
<%
Cookie[] cookies = request.getCookies();

for(int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("message")) {
out.println("The cookie says " + cookies[i].getValue());
}

%>
</BODY
</HTML>

Read a Cookie:
<HTML>
<HEAD>
<TITLE>Setting and Reading Cookies</TITLE>
</HEAD>

<BODY
<%
Cookie c = new Cookie("message", "Hello!");
c.setMaxAge(24 * 60 * 60);
response.addCookie(c);
%>

<%
Cookie[] cookies = request.getCookies();
boolean foundCookie = false;

for(int i = 0; i < cookies.length; i++) {
Cookie cookie1 = cookies[i];
if (cookie1.getName().equals("color")) {
out.println("bgcolor = " + cookie1.getValue());
foundCookie = true;
}


if (!foundCookie) {
Cookie cookie1 = new Cookie("color", "cyan");
cookie1.setMaxAge(24*60*60);
response.addCookie(cookie1);
}
%>
>
<H1>Setting and Reading Cookies</H1>
This page will set its background color using a cookie after refreshing.
</BODY>
</HTML>

 
 

No comments:

Post a Comment