Cookie instance, understanding cookies

1、 Understand what cookies are in one sentence

Cookies are small documents sent by the server to the client to record some information (such as user name), customize the home page, focus on advertisements, and finally exist in the form of files under the disk of the client computer.

2、 Use instances to recognize cookies

1. Create a new web project and write a test servlet as follows

2. Deploy the project to Tomcat and test the access. Open IE browser and enter http://ip:port/ Project name / servlet name access

As shown in the figure, the first visit shows wellcom

Access again (and access again after closing the browser) displays:

It indicates that the cookie we set has worked. We can find the cookie directory of IE browser and see that the cookie we set exists as a file, as follows:

After the timeout, the browser will display welcom, indicating visitcookie setMaxAge(60);// Set the cookie expiration time. This code works (ie, Firefox and Google browser can be tested)

3、 Some problems

1. Does the cookie of each browser matter?

There should be no cookies. They should be independent, because after ie access has generated cookies. Firefox and Google browser still show that there are no cookies.

2. Where is the cookie file? Will timeout be deleted?

My computer is win7, and the cookie is found in C: \ users \ administrator \ appdata \ local \ Microsoft \ windows \ temporary Internet files.

After the timeout, the cookie file still exists, but when accessing the browser after the timeout, the situation after there is no cookie is displayed. [test ie]

3. What happens if the browser disables cookies?

After I set [do not accept cookies] in Firefox, the client does not record cookies. Every visit is the same as the first time.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>