1. Introduction to Cookies
What are cookies?
Cookies are small data saved by web projects on the client
Widely used to save usernames and passwords on websites you have logged in, or click the search box to pop up search records on websites with search records
A few notes on cookies:
1. The size of a single cookie does not exceed 4kb
2. The number of cookies under the same domain name generally does not exceed 20 (different browsers may vary)
3. Cookies are used to store small amounts of insensitive data
4. Cookies can be used to distinguish different clients (when not logged in, in conjunction with session)
2. Creation of Cookies
1. Cookie objects are created in the form of key-value pairs:
Cookie cookie = new Cookie(“name”,”value”);
2. Set the cookie survival time (unit: seconds):
cookie.setMaxAge(60);
For different settings of time parameters:
1. When it is a positive number, the cookie exists in the client and will be automatically cleared when the time is up
2. When it is a negative number (the default value of the time parameter of this method is -1), the cookie exists in the memory and disappears when the browser is closed
3. When it is 0, the cookie will be cleared immediately
3. Set the scope of cookies:
cookie.setPath(“/”);//All webapps can see this cookie
cookie.setPath(“/web01”);//Only web01 can see this Cookie
tip:
A little reminder here, if the scope is set to “/project name” and the front-end webpage cannot receive cookies, then try removing “/”,
If that doesn’t work, just use “/”
4. Save Cookie to client:
resp.addCookie(cookie);
Multiple Cookie objects can be created and saved at one time
If there are Chinese characters, they need to be transcoded and stored:
Cookie c = new Cookie(“name”,URLEncoder.encode(“Sichuan Jay Chou”, “utf-8”) );
5. Read Cookie:
Usually reading is performed on the front-end web page:
Cookie[] cookies = request.getCookies();
3. Code
1. Web page code
Image code:
Code:
<html> <body> <% Cookie[] cookies = request.getCookies();//Read Cookie array String username = null; String password = null; for (Cookie cookie : cookies){//Iteratively obtain each Cookie if (cookie.getName().equals("username")){//If there is a cookie with the same name, perform assignment username = cookie.getValue(); } if (cookie.getName().equals("password")){//If there is a cookie with the same name, perform the assignment password = cookie.getValue(); } } %> <form> Username: <input type="text" name="username" placeholder="Please enter username" value="<%=username==null?"":username%>"><br><br> & amp;nbsp; & amp;nbsp; & amp;nbsp;Password: <input type="password" name="password" value="<%=password==null?"":password%>"><br><br> <input type="submit" value="Login" style="text-align: center"><br><br> <input type="checkbox" name="autologin" value="yes" >30-day automatic login //Get the value of the multi-select box to determine whether to use 30-day automatic login </form> </body> </html>
2. Controller code
Image code:
Ideas:
After verifying the validity of the user name and password, verify the login, and then save the cookie data after successful login
3. Cookie code
Image code:
Writing a class to add the Cookie method can increase the readability and maintenance of the code
Code:
public class Cookies { public static void autoLogin(HttpServletRequest req, HttpServletResponse resp,User user) throws IOException { System.out.println(user.getUsername()); Cookie cookie = new Cookie("username",user.getUsername()); Cookie cookie1 = new Cookie("password",user.getPassword()); cookie.setMaxAge(2592000); cookie1.setMaxAge(2592000); cookie.setPath("javaweb_jdbc"); cookie1.setPath("javaweb_jdbc"); resp.addCookie(cookie); resp.addCookie(cookie1); resp.sendRedirect("loginsuccess.jsp?username=" + user.getUsername());//Set the jump address } }
Such a function that uses cookies to realize automatic login has been created
The above is the entire content of using cookies to store login data and realize automatic user login. If you have any different opinions, please feel free to discuss in the comment area. If this article is helpful to you, please support it three times. Your support This is my biggest motivation for updating, 886~
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 137744 people are learning the system