测试向前台传输cookie 标记

前台代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%@ include file="/WEB-INF/jsp/include.jsp"%>
<head>
    <title>Title</title>

    <script>
        var tempKey = 'tempKey' ;
        var interval ;
        var dd = new Date().getTime() ;
        $(function () {
            $('#b1').click(function () {
                console.log(dd);
                $.get("<%=request.getContextPath()%>/test/testInterVal?"+tempKey+"="+dd,{},function () {});
                interval = self.setInterval("scan()",1000);
            });
        });

        function scan() {
            var cookie = getCookie(tempKey);
            if(null != cookie && undefined != cookie && dd==cookie){
                window.clearInterval(interval);
            } else {
                addDiv();
            }
        }

        function addDiv() {
            var div = $('#div1');
            div.append('<div>后台正在处理中<div>');
        }

        function getCookie(name) {
            var prefix = name + "="
            var start = document.cookie.indexOf(prefix)

            if (start == -1) {
                return null;
            }

            var end = document.cookie.indexOf(";", start + prefix.length)
            if (end == -1) {
                end = document.cookie.length;
            }

            var value = document.cookie.substring(start + prefix.length, end)
            return unescape(value);
        }

    </script>
</head>
<body>
<input type="button" id="b1" value="按钮">
<div id="div1" style="border: 1px ;color: red"></div>
</body>
</html>


后台代码

注意根据需要设置cookie 的path

    @RequestMapping("/testInterVal")
    public void testInterVal(HttpServletRequest request , HttpServletResponse response)throws Exception{
        String tempKey = request.getParameter("tempKey");
        System.out.println(tempKey);
        Thread.sleep(10000);
        System.out.println("线程睡眠结束。。。");
        Cookie cookie = new Cookie("tempKey",tempKey);
        cookie.setPath(request.getContextPath());
        cookie.setMaxAge(10);
        response.addCookie(cookie);
        //response.getWriter().write("ok");
        System.out.println("响应前台");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值