实现方法:
1.在登录验证的 Controller 里面调用私有函数 addCookie 。
@RequestMapping(value="/loginValidate")
public String loginValidate(HttpServletRequest request, HttpServletResponse response) throws IOException{
String loginAccount = request.getParameter("loginAccount");//登录账号
String loginPassword = request.getParameter("loginPassword");//登录密码
//添加 Cookie
addCookie(loginAccount,loginPassword,response,request);
//下面是自己写的一些登录验证,此处不是重点,省略掉
... ...
}
2.然后在 addCookie 函数实现 Cookie 的存放
private void addCookie(String loginAccount,String loginPassword, HttpServletResponse response,HttpServletRequest request)
throws UnsupportedEncodingException{
if(!loginAccount.equals("") && !loginPassword.equals("")){
//创建 Cookie
Cookie loginAcc