cookie ? 利用cookie实现 显示上次访问时间?

 

 

 

二、

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ 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>
    <%
        // 获取  所需要格式的当前系统时间
        Date date=new Date();
        SimpleDateFormat  sdf=new SimpleDateFormat("MM:hh:ss");
        String nowTime=sdf.format(date);
        
        
        //设置cookie
        Cookie c=new Cookie("lastTime",nowTime);
        
        response.addCookie(c);//将cookie  写入到响应流 响应给请求的客户端
        
        Cookie []cs=request.getCookies();//创建cookie的数组  因为有很多
    
        boolean  tag=false;  //定义Boolean的变量   记录首次访问
        
        if(cs!=null&&cs.length>0){
            for(Cookie c1:cs){
                if(c1.getName().equals("lastTime")){
                    tag=true;
                    out.write("上次的访问时间 "+c1.getValue());
                    break;
                }
            
            }
        }
        if(!tag){
            out.write("你是首次访问!");
        }
        
    %>

</body>
</html>

 

posted @ 2019-06-30 22:00  送外卖的小菜鸟  阅读(431)  评论(0编辑  收藏  举报