zoukankan      html  css  js  c++  java
  • jsp利用cookie记住用户名,下次登录时显示在文本框中(仅仅一个Cookie就整了将近三个小时,⊙﹏⊙b汗)

    <%@page import="java.net.URLDecoder"%>
    <%@page import="sun.security.util.Length"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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 cardid = null ;
    Cookie[] cks = request.getCookies() ;
    
    if(cks != null)
    { 
         // 如果已经设置了cookie , 则得到它的值,将该值放在卡号文本框的value中
            for(int i=0; i<cks.length; i++)
            {
                   if(cks[i].getName().equals("cardid"))
    
                cardid = cks[i].getValue();
            }
         }
    %>
    <form action="Cookie2.jsp" method="post">
    卡号<input type="text" name="cardid" value="<% if(cardid != null) out.println(cardid); %>"><br>
    密码<input type="password" name="password"><br>
    <input type="submit" value="提交">
    </form>
    
    </body>
    </html>
    <%@page import="java.net.URLEncoder"%>
    <%@page import="com.shuyinghengxie.bank.CardDAO"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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 kahao = request.getParameter("cardid") ;
    
    String password = request.getParameter("password") ;
    
    if(kahao==null || password==null ||
            kahao == "" || password == "" )
    {
        out.write("请正确登录") ;
    }
    else
    {
        CardDAO cd = new CardDAO() ;
        
        if(cd.checkLogin(kahao, password))
        {
            //out.write("登陆成功") ;
            response.getWriter().write("验证通过") ;
            
            //创建Cookie
            Cookie ck = new Cookie("cardid",kahao) ;
            
            //设置过期时间
            ck.setMaxAge(10*24*60*60)  ;
            
            //发送
            response.addCookie(ck) ;
            
            response.sendRedirect("a.jsp") ;
            
        }
        else
        {
            out.write("登录失败") ;
        }
    }
    %>
    </body>
    </html>
    <%@page import="java.net.URLDecoder"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    登陆成功!
    </body>
    </html>

    这时候点击返回,发现文本框中存在刚刚登陆过的账号,即使刷新依然存在

  • 相关阅读:
    报错:/usr/lib/gcc/x86_64-linux-gnu/5/include/avx512vlintrin.h(11269): error: argument of type "void *" is incompatible with parameter of type "long long *"
    docker跨平台
    [转载]启发式算法 (Heuristic Algorithms)
    linux软链接的创建、修改和删除
    使用docker部署tomcat|tomcat基础使用第二篇
    Tomat服务器学习
    使用秘钥登录AWS
    Maven基础
    [转载]什么是消融实验
    [转载]基于机器学习的专业级摄影照片处理器
  • 原文地址:https://www.cnblogs.com/20gg-com/p/6011970.html
Copyright © 2011-2022 走看看