What is a session?
From a user opens a browser start, website browser, close the browser to the whole process is called a session!
Process for each user to interact with the server in each there will be some data, programs, finding ways to save each user's data.
For example: a user clicks on a hyperlink by a servlet to buy a commodity, the program should save the goods purchased by the user, you need to use sessions!
Cookie is a client-side technology
Program to each user's data written to each user's browser in the form of a cookie. When users access the web using a browser to go to the resource server, they will go with their data. In this way, web resources to deal with is the user of the respective data.
Cookie with respect to the session is not particularly safe, but also limit the size and number of Cookie!
A WEB site can give a WEB browser sends multiple Cookie, a WEB browser can also store multiple Cookie WEB site provides.
In Java Cookie
// constructor: Cookie (String name, String value); // Method: String getName (); Cookie name obtained for String getValue (); Cookie value obtained for the void setPath (String path); set a cookie effective path will not set a default path: use - will come with a cookie when accessing resources in the path String getPath (); void setMaxAge (int MAGE); effective time to set a cookie int getMaxAge (); void setDomain (String domain); valid domain name is used to set a cookie: use - will bring a cookie to access resources in the domain over setDomain ( "baidu.com.");
String getDomain();
Cookie classification
Session-level Cookie: cookie to the browser is closed destroy this cookie stored in the browser's memory
Persistent cookie: This cookie is written to a local file, close your browser does not destroy
This cookie can be between different windows of the same browser share, as long as you need to set the effective time setMaxAge (int age); delete cookie setMaxAge (0);
// Create a cookie // Cookie cookie = new new Cookie (String name, String value); // cookie with Chinese deposit of (do not forget to remove when decoding)
// cookie does not support Chinese, we need to take a page encoded with js when using decodeURI (); cookies Cookie = new new cookies ( "Remember", the URLEncoder.encode ( username, "UTF-. 8")); cookie.setMaxAge (24 * 60 * 60); cookie.setPath ( "/"); response.addCookie (Cookie); // remove a Cookie (as long as the survival time of a cover into 0 or -1) cookies Cookie = new new cookies ( "Remember", the URLEncoder.encode ( username, "UTF-. 8")) ; cookie.setMaxAge (0); cookie.setPath ( "/"); response.addCookie (the cookie); / * Note:. cookie.setMaxAge (0); parameter can also be -1 can delete cookie, write difference 0:00 deleted immediately, you can delete the browser is closed after -1 for * /