码迷,mamicode.com
首页 > 其他好文 > 详细

Cookie实现记住密码代码

时间:2019-11-28 23:21:28      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:equals   false   ddc   amp   账号   exception   login   javaee   rri   

servlet代码package com.bjsxt.servlet;

 


package com.bjsxt.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @program: JavaEE
* @description
* @author: wuhao
* @create: 2019-11-28 14:15
**/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");

//获取请求数据
String uname = req.getParameter("uname");
String pwd = req.getParameter("pwd");
String ck = req.getParameter("ck");
//处理请求(理应掉业务逻辑层)
boolean flag = false;
if ("张三".equals(uname) && "123".equals(pwd)) {
flag = true;
}
if (flag) {
// 判断是否记住密码
if ("yes".equals(ck)) {
//记录账号密码
Cookie cookie = new Cookie("uname", uname);
Cookie cookie1 = new Cookie("pwd", pwd);
//设置时间
cookie.setMaxAge(3600 * 24 * 3);
cookie1.setMaxAge(3600 * 24 * 3);
//设置路径
cookie.setPath("/day14/login.jsp");
cookie1.setPath("/day14/login.jsp");
//放入响应头中
resp.addCookie(cookie);
resp.addCookie(cookie1);

}
req.getRequestDispatcher("success.jsp").forward(req, resp);
} else {
// resp.sendRedirect("login.jsp");
req.setAttribute("msg", "账号或密码不正确");
req.getRequestDispatcher("login.jsp").forward(req,resp );

}


}
}

Cookie实现记住密码代码

标签:equals   false   ddc   amp   账号   exception   login   javaee   rri   

原文地址:https://www.cnblogs.com/ww103/p/11954212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!