JAVA中怎么实现记住密码功能
本篇文章为大家展示了JAVA中怎么实现记住密码功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联公司是一家集网站建设,贞丰企业网站建设,贞丰品牌网站建设,网站定制,贞丰网站建设报价,网络营销,网络优化,贞丰网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
实体类:
public class User { /** * 主键id */ private Integer userId; /** * 账号 */ private String username; /** * 密码 */ private String password; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
UserMapper
mapper层
public interface UserMapper { User getUserByname(String username);}
service层
public interface UserService { User getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);}
impl
@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public ResultVO getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { String remember = httpServletRequest.getParameter("remember"); if (username!= null && username!= 0){ User user = userMapper.getUserByname(username); if (user != null && user.getPassword().equals(password)){ session.setAttribute("user", user); if (remember != null){ Cookie cookieUser = new Cookie("username", username); Cookie cookiePass = new Cookie("password", password); cookieUser.setMaxAge(60 * 60 * 24); cookiePass.setMaxAge(60 * 60 * 24); httpServletResponse.addCookie(cookieUser); httpServletResponse.addCookie(cookiePass); return ResultVO.success(user); } return ResultVO.error(1, "用户名或密码错误"); } return ResultVO.error(3, "用户名或密码不能为空"); }}
Controller
@RestControllerpublic class UserController { @Autowired private UserService userService; //登录 @RequestMapping("/getUserByname") public ResultVO getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){ return userService.getUserByname(username, password, session, httpServletRequest, httpServletResponse); }}
前端代码(JSP)
html的自己修改下,样式是BootStrap的
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ page contentType="text/html;charset=UTF-8" language="java" %>
上述内容就是JAVA中怎么实现记住密码功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
名称栏目:JAVA中怎么实现记住密码功能
文章URL:http://scyanting.com/article/jhecjd.html