Cookie introduction and usage
1. Cookie allows the site to track the number of visits, the last visit time, and the path of visitors to the site
2. Cookie can tell online advertisers the number of clicks on an advertisement, so that they can deliver the advertisement more accurately.
3. When the Cookie validity period is not reached, the Cookie enables users to access some websites they have browsed without entering their passwords and usernames.
4. Cookies can help websites collect users' personal data for various personalized services.
Create a cookie instance
<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>
String getComment () returns the comments in the cookie. If there is no comments, a null value is returned.
String getDomain () returns the domain name applicable to the cookie in the Cookie. the getDomain () method can be used to instruct the browser to return the Cookie to other servers in the same domain. Generally, the Cookie only returns the server with the same name as the server that sent the Cookie. Note that the domain name must start with a dot
Int getMaxAge () returns the maximum time before the Cookie expires, in seconds.
String getName () returns the Cookie name.
String getPath () returns the applicable Cookie path. If no path is specified, the Cookie will be returned to all the pages in the directory of the current page and its subdirectories.
Cookie reading
<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>
Cookie read/write operations
<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>