Jsp cookie usage

Source: Internet
Author: User
Tags comments

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.