Cookie: records the user's login status. Cookies can be created on the client side, allowing users to log in to the homepage without entering their username and password when logging in for the second time.
Main code:
In LoginView: (get cookie)
//Get cookies
Cookie [] cs=request.getCookies();
String name="";
String value="";
if(null!=cs)
{
System.out.println("cs.length:"+cs.length);
for(int i=0;i<cs.length;i++)
{
Cookie c=cs[i];
name=c.getName();
value=c.getValue();
}
}
if(null!=cs&&!(name.trim().equals("JSESSIONID")))
{
request.setAttribute("username",name);
request.setAttribute("userpass",value);
request.getRequestDispatcher("/servlet/Controller1").forward(request,response);
}
Controller: (Create cookie)
if(sflag) // if the login is successful (sflag indicates that the user verification is successful)
{
if(usercheckbox==null)
{
//System.out.println("You have not selected!!!"); //No selection, no operation
}
else
{
//System.out.println("You are selected!"); // If selected, create a cookie
//Create cookies
Cookie cookie=new Cookie(username,userpass);
//Set cookie expiration date
cookie.setMaxAge(60*60*24*7*2);
//Set the cookie usage path
cookie.setPath("/");
//Send cookie
response.addCookie(cookie);
}
HttpSession session=request.getSession();
session.setAttribute("userinfo",username); //Bind username
response.sendRedirect("/hygj0331/servlet/Controller3");
//Send to controller Controller3 to query all data and display data