jsp-4 用cookie实现记住密码
Posted lgp20151222
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp-4 用cookie实现记住密码相关的知识,希望对你有一定的参考价值。
jsp-4 用cookie实现记住密码
这次就有点简单了
基本是jsp-3的代码但是有些修改
public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User user=new User(); String username=req.getParameter("username"); user.setUsername(username); String password=req.getParameter("password"); user.setPassword(password); User use=userServive.login(user); if(use!=null){ String [] ischeck=req.getParameterValues("ischeck"); if(ischeck!=null&&"true".equals(ischeck[0])){ //添加cookie信息 Cookie usernameCookie=new Cookie("username", username); Cookie passwordCookie=new Cookie("password",password ); usernameCookie.setMaxAge(60*60*24*1);//一天 passwordCookie.setMaxAge(60*60*24*1); resp.addCookie(usernameCookie); resp.addCookie(passwordCookie); } resp.sendRedirect("loginSuccess.jsp"); }else{ resp.sendRedirect("loginFail.jsp"); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String username=""; String password=""; //取出Cookie Cookie [] cookie=request.getCookies(); for(int i=0;i<cookie.length;i++){ if(cookie[i].getName().equals("username")){ username=cookie[i].getValue(); } if(cookie[i].getName().equals("password")){ password=cookie[i].getValue(); } } %> <form action="<%=basePath%>/login.do" method="post"> <table border=0 cellpadding=0 cellspacing=0 style="margin:auto;border-collapse:separate; border-spacing:10px;"> <tr align="center"> <td colspan="2"> <label>登录</label> </td> </tr> <tr> <td> 用户名: </td> <td> <input type="text" name="username" value="<%=username%>"> </td> </tr> <tr> <td> 密码: </td> <td> <input type="password" name="password" value="<%=password%>"> </td> </tr> <tr> <td> <input type="checkbox" name="ischeck">记住我? </td> <td> <a href="<%=basePath%>/toRegister.do">注册</a> </td> </tr> <tr align="center"> <td> <input type="submit" value="登录"> </td> <td> <input type="reset" value="重置"> </td> </tr> </table> </form> </body> </html>
我就在纠结的是根据mvc的原则,cookie的操作是不是放在jsp?
以上是关于jsp-4 用cookie实现记住密码的主要内容,如果未能解决你的问题,请参考以下文章