• 登录时记住账号密码功能示例代码


    登录JSP页面

     1 <%
     2         request.setCharacterEncoding("utf-8");
     3         String username = "";
     4         String password = "";
     5         Cookie[] cookies =  request.getCookies();
     6         if(cookies!=null && cookies.length>0){
     7             for(Cookie c : cookies){
     8                 System.out.println(c.getName()+":"+URLDecoder.decode(c.getValue(), "utf-8"));
     9                 if(c.getName().equals("username")){
    10                     username =URLDecoder.decode(c.getValue(),"utf-8") ;
    11                 }
    12                 if(c.getName().equals("pwd")){
    13                     password = c.getValue();
    14                 }
    15             }
    16         }
    17     
    18      %>
    19     <form action="login.jsp" method="post">
    20         <input name="action" value="login" type="hidden">
    21         <input name="username" placeholder="用户名" value="<%=username %>" required="" type="text">
    22         <hr class="hr15">
    23         <input name="password" placeholder="密码" value="<%=password %>" required="" type="password">
    24         <hr class="hr15">
    25         <input type="checkbox" name="jizhu" value="jizhu" checked="checked">
    26         十天内记住我的登录状态
    27         <hr class="hr15">
    28         <input value="登录" style="100%;" type="submit">
    29         <hr class="hr20">
    30         <!-- 帮助 <a onClick="alert('请联系管理员')">忘记密码</a> -->
    31     </form>

    处理登录信息的JSP页面

     1 <% request.setCharacterEncoding("utf-8"); %>
     2 <jsp:useBean id="user" class="com.po.User" scope="page"></jsp:useBean>
     3 <jsp:useBean id="userDao" class="com.dao.UserDao" scope="page"></jsp:useBean>
     4 <jsp:setProperty property="*" name="user"/>
     5 <%
     6     
     7     String[] jizhu = request.getParameterValues("jizhu");
     8     if(jizhu!=null && jizhu.length>0){
     9         Cookie nameCookie = new Cookie("username",URLEncoder.encode(user.getUsername(), "utf-8") );
    10         Cookie pwdCookie = new Cookie("pwd",user.getPassword());
    11         nameCookie.setMaxAge(60);
    12         pwdCookie.setMaxAge(60);
    13         response.addCookie(nameCookie);
    14         response.addCookie(pwdCookie);
    15     }else{
    16         Cookie[] cookies =  request.getCookies();
    17         if(cookies!=null && cookies.length>0){
    18             for(Cookie c : cookies){
    19                 if(c.getName().equals("username")||c.getName().equals("pwd")){
    20                     c.setMaxAge(0);
    21                     response.addCookie(c);
    22                 }
    23             }
    24         }
    25     }
    26 
    27     if(userDao.login(user)){
    28         session.setAttribute("name", user.getUsername());
    29         request.getRequestDispatcher("main.jsp").forward(request, response);
    30     }else{
    31         response.sendRedirect("error.jsp");
    32     }
    33 
    34 
    35 %>
  • 相关阅读:
    Kubernetes节点维护
    Kubernetes helm配置国内镜像源
    windows universal app中使用mvvm light
    windows phone 开发常用小技巧
    异步编程中的最佳做法(Async/Await) --转
    windows phone 开发常用小技巧
    windows phone 开发常用小技巧
    windows phone 开发常用小技巧
    #假期归来# 看看国外开发者第一时间用swift写的Flappy Bird (2014.6.3)
    vs2013 TFS如何彻底删除团队项目
  • 原文地址:https://www.cnblogs.com/jpwz/p/6347927.html
Copyright © 2020-2023  润新知