Brief introduction and case analysis of 1--cookie

Source: Internet
Author: User

1. What is a conversation.

In daily life, the process of asking me to answer a series of questions from dialing a phone to hanging up the phone is a conversation. The session can be easily understood as: users open a browser, click on multiple hyperlinks, access the server multiple Web resources, and then close the browser, the entire process called a session.

some problems to be solved in the process of conversation.

• Each user in the browser with the server in the process of conversation, inevitably will produce some data, the server to find ways for each user to save the data.

• For example: Multiple users click the hyperlink through a servlet each purchase a product, the server should find a way to each user to buy goods to protect their own place, so that when these user point checkout servlet, checkout servlet can get the user to buy the goods for the user checkout.

• This data is not stored in request or ServletContext. If the Content field is the context object, the second request overrides the first, and if it is the request field, two different step requests are issued

stateful session: A classmate came to the classroom, the next time to come to the classroom, we will know that this classmate has come, this is called a state session.

2. Two techniques for saving session data:

L Cookies

Cookie is the client technology that the server writes each user's data to the user's browser in the form of a cookie. When users use the browser to access Web resources in the server, they will go with their own data. In this way, Web resources deal with the user's own data.

L Session

session is a server-side technology that allows a server to create a unique session object for each user's browser at run time, because the session is unique to the user's browser, so when the user accesses the server's Web resources, You can put their own data in the session, when the user to access other Web resources in the server, other Web resources from the user's session to take out the data for the User Service.

3. What is a cookie

L cookies are a kind of technology to keep HTTP status information on the client, it is like the discount card issued by the store, the consumption record is written on the discount card, the second time take the discount card to go shopping, do not ask.

The L cookie is a piece of data that is sent to the browser by the Web server in the HTTP response message header when the browser accesses a resource on the Web server, and the data that the Web server sends to each client browser can vary.

L Once a Web browser saves a cookie, it should pass the cookie back to the Web server on the HTTP request header each time it accesses the Web server.

The Web server sends cookie information to the browser by adding the Set-cookie response Header field in the HTTP response message, and the browser passes the cookie back to the Web server by increasing the cookie Request header field in the HTTP request message.

3.Cookie API

The L Javax.servlet.http.Cookie class is used to create a Cookie,response interface also defines a Addcookie method, which is used to add a corresponding Set-cookie header field to its response header. Similarly, the request interface defines a GetCookies method that is used to obtain a client-submitted cookie. Method of Cookie class:

Üpublic Cookie (String name,string value)

Üsetvalue and GetValue methods: getting cookie values

Üsetmaxage and Getmaxage methods: Save time for cookies

Üsetpath and GetPath methods: Setting the cookie Path

Üsetdomain and GetDomain methods: scopes

Ügetname method

set-cookie:username=qiujy;domain=localhost;path=/

cookie:username=qiujy; $Path =/jsp_04_servlet

SetPath: A valid directory path for setting cookies. If you create a cookie without setting its Path property, the cookie is valid only for the directory and its subdirectories that the current access path belongs to. If you want a cookie to be valid for all access paths under all directories in the site, it should be set to "/".

domain is used to specify the domain in which the cookie is valid, and the browser will return the cookie information when it accesses all hosts in the domain.

such as path=/;D omain=.sina.com

4. Introduction and application of cookies

5.Cookie Application

L Displays the user's last access time

Example Analysis:

Packagecom.csdn.Demo;

Importjava.io.IOException;

Importjava.io.PrintWriter;

Importjava.util.Date;

Importjavax.servlet.ServletException;

Importjavax.servlet.http.Cookie;

Importjavax.servlet.http.HttpServlet;

Importjavax.servlet.http.HttpServletRequest;

Importjavax.servlet.http.HttpServletResponse;

Publicclass Cookie3 extends HttpServlet {

public void doget (httpservletrequest request,httpservletresponse response)

Throws Servletexception, IOException {

Response.setcontenttype ("Text/html;charset=utf-8");

PrintWriter pw = Response.getwriter ();

Pw.write ("Last Created cookie Time:");

cookie[] Cookie1 = Request.getcookies ();

for (int i=0;cookie1!=null &&i<cookie1.length;i++) {

if (Cookie1[i].getname (). Equals ("LastAccessTime")) {

Long Value =long.parselong (Cookie1[i].getvalue ());

Pw.print (Newdate (value). toLocaleString ());

}

}

Cookie cookie = Newcookie ("LastAccessTime", System.currenttimemillis () + "");

Cookie.setmaxage (7*24*3600);

Cookie.setpath ("/cookie");

Response.addcookie (cookie);

}

public void DoPost (Httpservletrequestrequest, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

6.Cookie Details

L A cookie can only identify one type of information, it contains at least one name and set value (value) that identifies the information.

L A Web site can send multiple cookies to a Web browser, and a Web browser can store cookies provided by multiple Web sites.

L browsers generally allow only 300 cookies, each site holds up to 20 cookies, and the size of each cookie is limited to 4KB.

L If a cookie is created and sent to the browser, by default it is a session-level cookie (that is, stored in the browser's memory) and is deleted after the user exits the browser. If you want the browser to store the cookie on disk, you need to use maxage and give a time in seconds. Setting the maximum limitation to 0 is the command browser to delete the cookie.

Note that when deleting cookies, path must be consistent or not deleted

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.