what is a cookie.
Cookies are when you browse a Web site, a small text file stored on your machine that records your user ID, password, browsed pages, time of stay, etc. when you come to the site again, the site can make the appropriate moves by reading cookies and learning about your information. such as on the page display welcome your slogan, or let you do not enter the ID, password directly login and so on.
Cookies are generated by the server side and sent to User-agent (typically the browser), and the browser saves the cookie key/value to a text file in a directory. Send the cookie to the server the next time you request the same Web site (provided the browser is set to enable cookies). The cookie name and value can be defined by the server side and can be written directly to Jsessionid for JSP, so that the server can know whether the user is a legitimate user and need to log in again, and the server can set up or read the cookies containing information, Use this to maintain the state in the user's session with the server.
the principle of cookies.
First of all look at a picture from the Internet, I feel good, can help you understand, as follows:
Simply say the principle of cookies:
When the client sends the request to the browser for the first time, it makes a specific action, such as selecting the option to remember the password. When the server is reached, the server responds, sends the requested request to the client, and adds a cookie object to the response response, and the object holds the information to be consumed by Set-cookie. The client can then save its session information with the server, and wait until the client sends the request to the server again, with the information of the cookie object, so the server can respond to the client according to the cookie information.
how to use cookies.
For a small example of how to apply cookies in our program, save the user name.
Create a new dservlet, used as a server, using JSP page as the client, to simulate the working principle of cookies, configuration file what is not said. The service-side code is as follows:
public class Dservlet extends HttpServlet {public
void DoPost (HttpServletRequest request, HttpServletResponse Response)
throws Servletexception, IOException {
//1. Get user name
String userName = Request.getparameter ("UserName");
Cookie cookie = new Cookie ("Remember", userName);
2. Get check box selected state
String remember = Request.getparameter ("Remember");
3. Based on the state of the judgment just now. Create a cookie and set the valid time
if (remember!=null && remember.equals ("yes")) {
//need to remember the password ==> Remember two weeks
cookie.setmaxage (60*60*24*7*2);
} else{
//No need to remember the password or delete the password that has been remembered
cookie.setmaxage (0);
}
4. Send a cookie
response.addcookie (cookie) to the client;
5. Jump Landing Success Page
response.sendredirect ("/day11-cookie/index.jsp");
}
The JSP page code for the login is as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Do not check "Remember Me", we click on login, check the browser cookie information (for example, Firefox), as shown in the following figure:
We see cookies In addition to the program running session Jsessionid, and there is no other cookie information, and when we log in, will remember that I checked, you will see the cookie information we have added content, and the content is the user name we filled out, This way, the user name is automatically filled in at the next landing.
Summary:
The application of cookies, it does bring us a lot of convenience, for example, almost all shopping sites have shopping cart features, if there is no cookie or similar technology, it is impossible to achieve, and we buy tickets, will be prompted in a limited time to complete the payment and so on require the support of cookie technology. Cookies also had their own problems at the time.
First, security issues, as we all know, cookies usually hold sensitive information about the user, so cookies become the object of concern for many hacker, to gain special privileges, and even to conquer the entire website. such as cookie spoofing, but we can also use JavaScript and other technologies to control it.
Second, the computers on many occasions are public, so the settings you make on some sites will remain on this computer, and you may be able to see your message when someone else uses the computer.
Third, when there is a problem with our browser, it may be removed from all Internet temporary files on your computer when it is resolved. In that case, all your cookie files will be lost. When you visit the previous site again, some of the personalization settings you set will no longer exist, and the site will treat you as a new user. Similarly, when you use a different computer to access the same site, the site will also treat you as a few users, because a few computer cookie information is not the same.