A brief introduction to cookies

reflection:

HTTP is a stateless protocol. When a client sends a request to the server, the connection is closed after the server returns a response, and the connection information is not retained on the server.

When the client sends multiple requests and needs the same request parameters, what should be done? At this time, we will use our cookies to implement it.

What are cookies

Cookie is a technology that keeps HTTP status information on the client.

Cookies are the data transmitted by the web server to the browser in the response header when the browser accesses a resource of the server.

If the browser saves a cookie, it will pass it to the server in the request header every time it accesses the server in the future. A cookie can only record one kind of information in the form of key value.

A web site can send multiple cookies to a browser, and a browser can also store cookies from multiple sites.

Basic principles of cookies:

Basic operations of cookies:

Create cookie object:

  Cookie cookie = new Cookie(String key,String value);

Set cookie parameters:

  cookie. setMaxAge(int seconds)

  cookie. setPath(String url)

Response cookie to client:

  response. addCookie(Cookie cookie)

Get cookie properties:

  Cookie[] cookies = request. getCookies()

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
分享
二维码
< <上一篇
下一篇>>