Cookie案例(判断是否首次访问)
发布日期:2021-05-07 02:43:19 浏览次数:17 分类:原创文章

本文共 3492 字,大约阅读时间需要 11 分钟。

#需求:
*访问一个Servlet,如果是第一次访问,则提示:您好,欢迎首次访问。
*如果不是第一次访问,则提示:欢迎回来,您上次访问的时间为:显示时间字符串。

#代码实现:

package cn.itcast.cookie;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.net.URLDecoder;import java.net.URLEncoder;import java.text.SimpleDateFormat;import java.util.Date;/** *     #需求: *      访问一个Servlet,如果是第一次访问,则提示:您好,欢迎首次访问。 *      如果不是第一次访问,则提示:欢迎回来,您上次访问的时间为:显示时间字符串。 */@WebServlet("/cookieTest")public class CookieTest extends HttpServlet {       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           //设置响应的消息体的数据格式及编码        response.setContentType("text/html;charset=utf-8");        //1.获取Cookie        Cookie [] cookies = request.getCookies();        //是否找到lastTime这个cookie        boolean lastTime_flag = false;        //2.遍历cookie数组        if(cookies != null && cookies.length > 0){               for (Cookie cookie:cookies) {                   //3.获取cookie的名称                String name = cookie.getName();                //4.判断名称是否为lastTime                if(name.equals("lastTime")){                       //有Cooike不是第一次访问 提示:欢迎回来,您上次访问的时间为:显示时间字符串。                    lastTime_flag = true;//有lastTime这个cookie                    //响应数据                    //获取cookie的value,时间                    String value = cookie.getValue();                    //URL解码                    value = URLDecoder.decode(value,"utf-8");                    response.getWriter().write("<h1>欢迎回来,您上次访问的时间为:</h1>"+value);                    //设置cookie的value                    //获取当前时间的字符串,重新设置cookie的值,重新发送cookie                    Date date = new Date();                    /**                     * //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");此处会报错                     * cookie能不能存中文?                     * 在tomcat8之前cookie中不能直接存储中文数据,需要将中文数据转码,一般采用URL编码                     * 在tomcat8之后,cookie支持中文数据,但特殊字符还是不支持,建议使用URL编码存储,URL解码解析                     */                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");                    String str_date = sdf.format(date);                    //URL编码                    str_date = URLEncoder.encode(str_date,"utf-8");                    cookie.setValue(str_date);                    //设置cookie的存活时间                    cookie.setMaxAge(60 * 60 * 24 * 30);//一个月                    response.addCookie(cookie);                    break;                }            }        }        //第一次访问,则提示:您好,欢迎首次访问。        if(cookies == null || cookies.length == 0 || lastTime_flag == false){               response.getWriter().write("<h1>您好,欢迎首次访问。</h1>");            //设置cookie的value            //获取当前时间的字符串,重新设置cookie的值,重新发送cookie            Date date = new Date();            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");            String str_date = sdf.format(date);            str_date = URLEncoder.encode(str_date,"utf-8");            Cookie cookie = new Cookie("lastTime",str_date);            //设置cookie的存活时间            cookie.setMaxAge(60 * 60 * 24 * 30);//一个月            response.addCookie(cookie);        }    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           this.doPost(request, response);    }}
上一篇:Linux学习笔记
下一篇:JDBCUtils代码

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月03日 17时40分17秒