zoukankan      html  css  js  c++  java
  • jsp-4 用cookie实现记住密码

    用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>
                    &nbsp;&nbsp;<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?

  • 相关阅读:
    DIV+CSS 高手也得看的15个CSS常识
    dl,dt,dd标签 VS 传统table实现数据列表
    弥补Web开发缺陷实战 HTML5 中存储API
    50漂亮的后台管理界面模板
    提升你设计水平的CSS3新技术
    300+Jquery, CSS, MooTools 和 JS的导航菜单资源(总有一个适合你!)
    99款高质量免费HTML/CSS模板(看到了还行,推荐给大家)
    DIV+CSS:如何编写代码才能更有效率
    HTML5和CSS3开发工具资源汇总
    43个实例xHTML+CSS(DIV+CSS)网页及导航布局教程
  • 原文地址:https://www.cnblogs.com/ydymz/p/6287391.html
Copyright © 2011-2022 走看看