JSP中Cookie案例改造一下

<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>記住訪問時間JSP</title>
</head>
<body>
<%
   /* //設置響應的消息體的數據格式及編碼
    response.setContentType("text/html;charset=utf-8");*/

    //①獲取所有的Cookie
    Cookie[] cookies = request.getCookies();
    boolean flag = false;  //沒有cookie爲lastTime
    //②遍歷Cookie數組
    if ((cookies != null) & (cookies.length > 0)) {
        for (Cookie cookie : cookies) {
            //③獲取cookie的名稱
            String name = cookie.getName();
            //④判斷名稱是否是:lastTime
            if ("lastTime".equals(name)) {
                flag = true; //找到lastTime的cookie爲true
                //有該cookie,不是第一次訪問

                //先獲取前一次訪問的cookie的value值
                //響應數據
                //獲取cookie的value值,時間
                String value = cookie.getValue();

                System.out.println("解碼前:" + value);
                //URL解碼
                value = URLDecoder.decode(value, "utf-8");
                System.out.println("解碼後:" + value);
%>
               <%--out.write("<h1>歡迎回來,你上次訪問時間爲:" + value + "</h1>");--%>
                <%-- 階段代碼片面段 --%>
<h1>歡迎回來,你上次訪問時間爲::<%= value%> </h1>
<form action="#" method="post">
    <table   border="1" width="250">
        <tr>
            <td><label for="username">用戶名</label></td>
            <td><input type="text" name="username" id="username" ></td>
        </tr>

        <tr>
            <td><label for="password">密碼</label></td>
            <td><input type="text" name="username" id="password" ></td>
        </tr>

        <tr>
            <td colspan="2" align="center"><input type="submit" name="登錄" value="登錄"></td>
        </tr>
    </table>
</form>
<%

                //再設置cookie的value值
                //獲取當前時間的字符串,重新設置cookie的值,重新發送cookie
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                String str_date = sdf.format(date); //解析時間格式

                System.out.println("編碼前:" + str_date);
                //URL編碼
                str_date = URLEncoder.encode(str_date, "utf-8");
                System.out.println("編碼後:" + str_date);

                cookie.setValue(str_date);
                //設置cookie的存活時間;
                cookie.setMaxAge(60 * 60 * 24 * 30); //一個月
                response.addCookie(cookie);



                break;
            }


        }
    }
    if (cookies == null || cookies.length == 0 || flag == false) {
        //沒有lastTime 表示第一次訪問


        //設置cookie的value值
        //獲取當前時間的字符串,重新設置cookie的值,重新發送cookie
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String str_date = sdf.format(date); //解析時間格式

        System.out.println("編碼前:" + str_date);
        //URL編碼
        str_date = URLEncoder.encode(str_date, "utf-8");
        System.out.println("編碼後:" + str_date);

        Cookie cookie = new Cookie("lastTime", str_date);
        cookie.setValue(str_date);
        //設置cookie的存活時間;
        cookie.setMaxAge(60 * 60 * 24 * 30); //一個月

        response.addCookie(cookie);
%>

       <%-- out.write("<h1>您好,歡迎您是首次訪問 </h1>");--%>
             <%-- 在這裏使用html標籤可以 新的JSP寫法 ——截斷  --%>
           <h1>您好,歡迎您是首次訪問 </h1>
           <span></span>
<%
    }

%>

</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章