JavaWeb study notes 10--Cookie realizes the function of remembering passwords

【statement】 

Welcome to reprint, but please keep the original source of the article →_→ 

Life One: http://www.cnblogs.com/smyhvae/

Article source: http://www.cnblogs.com/smyhvae/p/4096807.html

 

【text】

The main content of this article:

•1. What is Cookie

• 2. The benefits of cookies

• 3. The main methods of cookies

 

1. What is Cookie

A cookie is a means by which a web server stores information on a visitor's hard drive through a browser . The purpose of cookies is to bring convenience to users and add value to the website. Despite many misinformation, cookies do not actually pose a serious security threat. Cookies are never executed in any way, so they can't bring viruses or attack your system. In addition, because browsers generally only allow 300 cookies to be stored, each site can store up to 20 cookies, and the size of each cookie is limited to 4KB, so cookies will not fill your hard disk.

For example, when we visit the website for the first time and enter the username and password, we can choose to let the system remember the username and password, so that we don't need to re-enter it next time. This is a typical application of cookies.

 

Second, the benefits of cookies:

The benefits cookies bring to websites and users are numerous:

  • 1. Cookies enable a site to track the number of visits, last visit time, etc. of a particular visitor
  • 2. Cookies can tell online advertisers how many times their advertisements have been clicked, so that they can more accurately deliver advertisements
  • 3. When the validity period of the cookie has not expired, the cookie can enable the user to enter some sites that have been browsed without entering the password and user name.
  • 4. Cookies can help the site to collect personal data of users to realize various personalized services. In JSP, we can also use cookies to write some powerful applications.

Some browsers can disable cookies, so you cannot use cookies for core business purposes.

 

3. The main methods of the Cookie class:

 
int getMaxAge() Returns the maximum time in seconds before the cookie expires.
int setMaxAge() Set the cookie expiration time in seconds.

String getName() returns the name of the cookie

String getValue() returns the value of the cookie.
void setValue(String newValue) Set a new value after the cookie is created. 
 

Note: Name and value are the two parts we always care about. GetName/setName, getValue/setValue will be described in detail later.

 

Fourth, the code example:

[Example] Realize the function of remembering password and automatic login

Create a new JavaWeb project Test06.

login.jsp:

 
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8
 9 <%
10     String username = "";
11     String password = "";
12 //Get all cookies of the current site
13     Cookie[] cookies = request.getCookies();
14 for (int i = 0; i < cookies.length; i++) {// Traverse the data in cookies to find the data of username and password
15         if ("username".equals(cookies[i].getName())) {
16             username = cookies[i].getValue();
17         } else if ("password".equals(cookies[i].getName())) {
18             password = cookies[i].getValue();
19         }
20     }
21 %>
22
23 </head>
24 <body>
25     <form action="login_handler.jsp" method="post">
26         username:<input type="text" name="name" value="<%=username%>" /><br/>
27         password:<input type="password" name="pwd" value="<%=password%>" /><br/>
28         <input type="checkbox" value="y" name="isLogin">自动登录<br/>
29         <input type="submit" value="登录" />
30     </form>
31 </body>
32 </html>
 

13 lines of code: Get all cookies on the current site. Note: This Cookie class is defined in the next login_handler.jsp file.

The value="<%=username%>" in line 26, and the value="<%=password%>" in line 27 are the values ​​obtained in lines 16 and 18.

login_handler.jsp:

 
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4     String name = request.getParameter("name");
 5     String pwd = request.getParameter("pwd");
 6     String flag = request.getParameter("isLogin");
 7
 8     if (!"admin".equals(name) && !"123".equals(pwd)) {
 9         response.sendRedirect("error.jsp");
10     } else {
11         if ("y".equals(flag)) {
12 //Create two Cookie objects
13             Cookie nameCookie = new Cookie("username", name);
14 //Set the validity period of the cookie to 3 days
15             nameCookie.setMaxAge(60 * 60 * 24 * 3);
16             Cookie pwdCookie = new Cookie("password", pwd);
17             pwdCookie.setMaxAge(60 * 60 * 24 * 3);
18             response.addCookie(nameCookie);
19             response.addCookie(pwdCookie);
20         }
21         response.sendRedirect("success.jsp");
22     }
23 %>
 

The core code is 11 to 20 lines.

Line 11: If the user has checked "Automatic login", put the user name and password information into the cookie.

The 15th and 17th lines of code are to set the storage time of the cookie . If the storage time of the cookie is not set, the default storage time is 0. At this time, the data saved by the cookie is stored in the memory. When the browser is closed, the cookie disappears and becomes invalid.

After the storage time is set, the data set at this time will be saved in the hard disk, and the specific location of different browsers is different.

Where Google Chrome looks at cookies:

"Menu - Settings - Display Advanced Settings - Content Settings":

Click "Content Settings" in the above figure, and the following interface will pop up:

Click the red box in the figure above, and the following interface will pop up:

success.jsp:

 
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>success.
10 </body>
11 </html> 
 

如果登陆成功,就调到这个页面。

error.jsp:

 
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>error.
10 </body>
11 </html>
 

Run the program, when you enter the correct username and password to log in, and check "Remember password", the next time you return to the login interface, it will look like this:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325241459&siteId=291194637