cookie简单案例

@WebServlet("/dlu")
public class Dlu extends HttpServlet {
    static int i=0;
    static String decode=null;
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String pass = request.getParameter("pass");
        if ("sensen".equals(username) && "123".equals(pass)) {
            i++;
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String format = sdf.format(date);
            String encode = URLEncoder.encode(format, "utf-8");
            Cookie cookie = new Cookie("Date", encode);
            response.addCookie(cookie);
            if (i == 1) {
                Cookie[] cookies = request.getCookies();
                if (cookies != null) {
                    System.out.println(cookies.length);
                    for (Cookie cookie1 : cookies) {
                        String name = cookie1.getName();
                        String value = cookie1.getValue();
                        if ("Date".equals(name)){
                            decode = URLDecoder.decode(value, "utf-8");
                            System.out.println("实时登录时间为" + decode);
                            request.setAttribute("de", decode);
                        response.addCookie(cookie);
                }
                    }
//                    for (int i1 = 0; i1 < cookies.length; i1++) {
//                        decode = URLDecoder.decode(cookies[1].getValue(), "utf-8");
//                        System.out.println("第一次登录时间为" + decode);
//                        request.setAttribute("de", decode);
//                    }
                }
                else {
                    System.out.println("cookie数组为空");
                }
                request.setAttribute("first","第一次登录!欢迎!");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Mainym.jsp");
                requestDispatcher.forward(request, response);
            } else{
                System.out.println("你上次登录时间为:"+decode);
                request.setAttribute("de1", decode);

            Cookie[] cookies = request.getCookies();
                if (cookies != null) {
                for (Cookie cookie1 : cookies) {
                    String value = cookie1.getValue();
                    decode = URLDecoder.decode(value, "utf-8");
                    System.out.println("实时登录时间为" + decode);
                    request.setAttribute("de2", decode);
                }

            }
            else {
                System.out.println("cookie数组为空");
            }
            request.setAttribute("after","欢迎回来!");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Mainym.jsp");
            requestDispatcher.forward(request, response);
        }


    }else {
            System.out.println("用户名或密码错误");
            request.setAttribute("tishi","用户名或密码错误!请重新登录!");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Dlu.jsp");
            requestDispatcher.forward(request,response);
        }

    }
}

JSP

登录

<html>
<head>
    <title>登录</title>
</head>
<body>
<div>
    <form action="/dlu" method="post">
        <input type="text" name="username" id="username" placeholder="用户名:">
        <br>
        <input type="password" name="pass" id="pass" placeholder="密码:">
        <button type="submit">登录</button>
    </form>
</div>
<span style="color: red">
    ${requestScope.tishi}
</span>
</body>
</html>

登录成功

<html>
<head>
    <title>欢迎!</title>

</head>
<body>
<span style="color: blue;font-size: 40px">
    ${requestScope.first}
</span>

<span style="color: blue;font-size: 40px">
    ${ requestScope.after}
</span>

<span style="color: green">
    第一次登录的时间为:${requestScope.de}
</span>
<br>
<span style="color: red">
    您上次登录的时间为:${requestScope.de1}
</span>

<span style="color: red">
    实时登录的时间为:${requestScope.de2}
</span>
</body>
</html>

结果

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值