cookie實現登錄時間記錄


<%@ page import="java.util.SimpleTimeZone" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/6/27
  Time: 8:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  $END$
  <form action="#" method="post">
      <input type="text" name="admin" ><hr/>
      <input type="password" name="password" ><hr/>
      <input type="submit" value="LinkStart">
  </form>
    <%
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
        //獲得當前系統時間
        String nowTime=sdf.format(date);//將此時間轉換為String類型
        //設置一個cookie,其名字為lasttime,值為String類型的系統時間
        Cookie c=new Cookie("lastTime",nowTime);
        //將此cookie添加到response中
        response.addCookie(c);
        //將response中所有cookie一股腦全tm搞出來進一個名為cs的cookie數組中
        Cookie cs[]=request.getCookies();
        boolean tag=false;
        //
        //遍歷這個cookie數組,注意其判斷條件
        if(null!=cs&&cs.length>0){
          for (Cookie c1:cs){
              //一旦這個名為lastTime的cookie被發現,則
              if(c1.getName().equals("lastTime")){
                  tag=true;
                  //輸出上次訪問的時間
                  out.write("我們上次訪問的時間是:" + c1.getValue());
                  break;
              }
          }
        }
        if(!tag){
            out.write("您是首次訪問本網站");
        }

    %>
  </body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM