The Internet browser will have a history, log on to a website will remember the password, these historical records and passwords are actually left in the computer cookie, if you change a computer, there will be no previous records. This is different from the session, the record of the session is left on the server side, changed the computer, log in or can see the previous record. Client Record: Cookie
Server records: Session
Small example: remember password at login
Basic structure as pictured
Loginaction.java
Package com.action;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.Cookie;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
@SuppressWarnings ("Serial")
public class Loginaction extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String Username=request.getparameter ("username");
String password=request.getparameter ("Userpass");
String flag=request.getparameter ("flag");
String path= "/error.jsp";
if ("admin". Equals (username) && "123". Equals (password)) {
if (Flag!=null&&flag.equals ("select")) {
Cookies c1=new Cookies ("uname", username);
Cookies c2=new Cookies ("UPass", password);
Set effective time, in milliseconds
c1.setmaxage (60*60*60*24);
c1.setmaxage (60*60*60*24);
response.addcookie (C1);
response.addcookie (C2 );
}else{
cookie c1=new Cookies ("uname", username);
cookie c2=new Cookies ("UPass", password);
c1.setmaxage (0);
c1.setmaxage (0);
response.addcookie (C1);
Response.addcookie (C2);
}
path= "/index.jsp";
}  &NBSP
request.getrequestdispatcher (path). Forward (request,response); }
}
*************************************************************************************************************** ********************************************
Index.xml
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>cookie</title>
<body>
<form action= "login.do" method= "POST" >
User: <input type= "text" name= "username" value= "${cookie.aname.value}" ><br/>
Password: <input type= "password" name= "Userpass" value= "${cookie.apsss.value}" ><br/>
Remember password: <input type= "checkbox" name= "Flag" value= "select" ${cookie.aname.value}!=null? ' Checked ': '/>
<%--? : An expression
If you are not empty, you are chosen, you are not chosen, you do not remember your password.
--%>
<br/>
<input type= "Submit" value= "Login" >
</form>
</body>