Javaweb's Cookie Note

Source: Internet
Author: User

I. Overview of Cookies


The cookie mechanism uses a scheme that maintains HTTP state information on the client.

A cookie is a small text file that is delivered to the browser by the Web server in the HTTP response message header when a browser accesses a resource on the Web server.

Once a cookie has been saved by the Web browser, it will be passed back to the Web server in the HTTP request header each time it accesses the Web server.


Ii. use of cookies in a servlet


A Javax.servlet.http.Cookie class is provided in the Servlet API to encapsulate cookie information, which contains methods for generating cookie information and extracting individual attributes of cookie information.
Methods of the Cookie class:
Construction method: Public Cookie (String name,string value)
GetName method
SetValue and GetValue method
Setmaxage and Getmaxage method
SetPath and GetPath method
A Addcookie method is defined in the HttpServletResponse interface that is used to add a Set-cookie response header field to the HTTP response message sent to the browser.
The HttpServletRequest interface defines a getcookies method that is used to read all cookie entries from the cookie Request header field of an HTTP request message.

Third, session cookies and persistent cookies


If you do not set an expiration time, the cookie disappears when the browser window is closed as long as the cookie's lifetime is the browser session . This cookie, which is the lifetime of the browser session, is referred to as a session cookie. Session cookies are generally not saved on the hard disk but in memory.
If the expiration time is set, the browser will save the cookie to the hard disk, turn it off and open the browser again, and these cookies remain valid until the set expiration time expires.
Cookies stored on the hard disk can be shared between different browser processes, such as two IE windows. For cookies stored in memory, different browsers have different ways of handling them.

Simple example:

① Automatic login with cookies

login.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >②: To achieve the latest reading of the 5 books, requirements, record the history of reading, if more than 5, delete the first reading of the record, for already read the book, and re-view, delete the original record, add record

books.jsp

<% @page import= "java.util.ArrayList"%><% @page import= "java.util.List"%><%@ page language= "Java" Contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >book.jsp

<% @page import= "java.util.ArrayList"%><% @page import= "java.util.List"%><%@ page language= "Java" Contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Cookie storage implementations seem to be like stacks of sensations ....

Iv. Scope of the cookie


Can be used for subdirectories of the current directory and current directory, but not for the top-level directory of the current directory

by Cookie.setpath (Request.getcontextpath ()); You can set the scope of the cookie (the site of the Web project where the current cookie resides), namely:/cookie



Javaweb's Cookie Note

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.