Use jsp to complete the browser login and automatically fill in the account and password function

Description: Use jsp to complete the browser login and automatically fill in the account and password function

技术:pageContext + reqeust + response + cookie + EL

principle:

    1. This program form is submitted to itself

    2. Set the encoding of the reqeust object to prevent garbled characters in Chinese

    3. The request calls the getParameter method to obtain the account number and password. When submitting to itself, you must judge the null value and obtain all cookies.

    4. Scan all cookies of the client to determine whether there is a flag attribute, such as my used in the program, if found, set the flag to false directly, if there is no flag attribute, set the cookie, add the cookie to the response and set the cookie The storage time of the account number and password are separated by #, which is convenient for separation.

    5. In the same way, take out and scan all the cookies of the client again. If the attribute flag is found, take out its value, and then call the split method to separate the account and password and set it in the pageContext built-in object, which is convenient for EL to obtain.

    6. EL takes out the value according to the attribute. The advantage of EL is that if the attribute is empty, it will be replaced with ""

Code:

<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>JSP</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
	<%
		boolean flag = true; // used to record whether to insert
		Cookie[] cookiesTmp = request.getCookies(); // get all cookies
		
		request.setCharacterEncoding("UTF-8"); //Set the encoding to prevent Chinese garbled characters
		String name = request.getParameter("uname"); // Get username
		String pass = request.getParameter("upass"); // get the password
		
		// Note that the form submission itself must be controlled and judged
		if(cookiesTmp != null && name != null & pass!= null){
			for (Cookie i : cookiesTmp) { // the foreach method traverses
				if ("my".equals(i.getName())){ // if god already exists (the cookie read flag for this program)
					flag = false; // set the flag to not insert	
					break; // break out
				}
			}
			
			// set cookie if flag is true
			if (flag) {
				Cookie cookie = new Cookie("my",name + "#" + pass);
				
				// Set the save time to 30 minutes, the unit of second is 30 * 60 to get the number of seconds in half an hour
				cookie.setMaxAge(30 * 60);
				response.addCookie(cookie);
			}
		}
	%>
	
	<!-- If there is, take out the account and password and set the properties-->
	<%
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			for (Cookie i : cookies) {
				if ("my".equals(i.getName())){
					
					// Set the properties, here the string separation method split is used
					// pageContext.setAttribute("Attribute name", "Value");
					pageContext.setAttribute("name", i.getValue().split("#")[0]);
					pageContext.setAttribute("pass", i.getValue().split("#")[1]);
				}
			}
		}
	%>
	
	<!-- action="" means submit to itself; ${name} is an EL expression meaning to take out the attribute value of name -->
	<form action="" method="post">
		<label for="uname">账号:</label>
		<input type="text" id="uname" name="uname"  value="${name}">
		<br>
		<br>
		<label for="upass">密码:</label>
		<input type="password" id="upass" name="upass" value="${pass}">
		<br>
		<br>
		<input type="submit" value="登录">
		<input type="reset" value="重置">
	</form>
</body>
</html>

operation result:

    1. First login


    2. Enter the account and password and select Login


    3. After the login is completed, you will return. At this time, refresh and find that the account number and password are automatically filled in.


Tip: It is not safe to use cookies to save passwords, so it is usually enough to save the user name. Of course, the user can also choose whether to save the account or password.

Guess you like

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