初学者的忧伤,创建和获取cookie

创办和获得cookie

初学者的忧伤,创建和获取cookie。制作人:全心全意

cookie:在互连网中,cookie是小段的文书新闻,在互连网服务器上转移,并发送给浏览器。通过选择cookie能够标记用户身份,记录用户名和密码,追踪重复用户等。浏览器将cookie以key/value的方式保留到客户机某些钦定目录中。

getCookies():获取具备cookie对象的聚合
getName():获取钦命名称的cookie
getValue():获取cookie对象的值

 

测试页面存入和获得cookie页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
     <%@ page import="java.net.URLDecoder" %>
<!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>通过cookie保存并读取用户登录信息</title>
</head>
<body>
<%
 Cookie[] cookies = request.getCookies();
 String user = "";
 String date = "";
 if(cookies != null){
  for(int i =0;i<cookies.length;i++){
   if(cookies[i].getName().equals("mrCookie")){
    user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);
    date = cookies[i].getValue().split("#")[1];
   }
  }
 }

 if( "".equals(user) && "".equals(date) ){
%>

  游客您好,欢迎您初次光临!
  <form action="index1.jsp" method="post">
   请输入姓名:<input name="user" type="text" value="">
   <input type="submit" value="确定">
  </form>
<%
 }else{
%>
  欢迎[<b><%=user %></b>]再次光临<br>
  您注册的时间是:<%=date %>
<%
 }
%>
</body>
</html>

 

创建cookies页面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
 <%@ page import="java.net.URLEncoder" %>
<!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>写入cookie</title>
</head>
<body>
 <%
  String user = URLEncoder.encode(new String(request.getParameter("user").getBytes("ISO-8859-1"),"UTF-8"));
  Cookie cookie = new Cookie("mrCookie",user+"#"+new java.util.Date().toLocaleString());
  cookie.setMaxAge(5);
  response.addCookie(cookie);
 %>
<%=user %>
 <script type="text/javascript">window.location.href="index.jsp"</script>
</body>
</html>

 

小技能:在向cookie中保存的音讯中,假使包涵普通话,则要求调用java.net.U汉兰达LEncoder类的encode()方法将要保存到cookie中的新闻举行编码;在读取cookie的剧情时,还索要利用java.netUTiggoLDecoder类的decode()方法进行解码。那样,就可以成功地向cookie中写入汉语新闻。

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    </body>
</html>
<script type="text/javascript">
    var date = new Date();
    date.setDate(date.getDate() + 5);
    //alert(date);
    /*
    document.cookie = "姓名=张三;expires=" + date + ";path=/";
    document.cookie = "url=www.1000phone.com;expires=" + date + ";path=/";
    document.cookie = "email=zhangsan@1000phone.com;expires=" + date + ";path=/";
    alert(document.cookie);
    */



    //封装一个创建cookie的方法
    function createCookie(key,value,expires,domain,secure){
        var cookieText = encodeURIComponent(key) + "=" + encodeURIComponent(value) + ";path=/";
        if(!isNaN(expires)){
            if(typeof expires == "number" && expires > 0){
                var date = new Date();
                date.setDate(date.getDate() + expires);
                cookieText += ";expires=" + date;
            }
        }
        if(domain){
            cookieText += ";domain=" + domain;
        }
        if(secure){
            cookieText += ";secure";
        }
        document.cookie = cookieText;
    }
    createCookie("姓名","张三",10);
    createCookie("url","www.1000phone.com",10);
    createCookie("email","zhangsan@1000phone.com",10);
    //alert(document.cookie);



    //封装一个获取cookie的方法


    //方法一:
  //%E5%A7%93%E5%90%8D=%E5%BC%A0%E4%B8%89; url=www.1000phone.com; email=zhangsan%401000phone.com
    /*
     * 姓名=    0   张三
     * url=    39 www.1000phone.com
     * email zhangsan@1000phone.com
     * 
     * substring(start,end)
     * slice(start,end)
     * substr(start,length)
     */
    //alert(getCookieValue("email"));

    function getCookie(key){
        var keyText = encodeURIComponent(key) + "="
        var start = document.cookie.indexOf(keyText); //找到开始位置
        if(start != -1){
            var end = document.cookie.indexOf(";",start); //找到结束位置
            if(end == -1){
                end = document.cookie.length;
            }
            var cookieValue = decodeURIComponent(document.cookie.substring(start + keyText.length,end));
        }
        return cookieValue;
    }

    //方法二
    function getCookieValue(key){
        var arr = document.cookie.split("; ");
        //["%E5%A7%93%E5%90%8D=%E5%BC%A0%E4%B8%89","url=www.1000phone.com","email=zhangsan%401000phone.com"]
        for(var i = 0; i < arr.length; i ++){
            var list = arr[i].split("=");
            //["%E5%A7%93%E5%90%8D","%E5%BC%A0%E4%B8%89"]
            //["url","www.1000phone.com"]
            //["email","zhangsan%401000phone.com"]
            if(list[0] == encodeURIComponent(key)){
                break;
                return decodeURIComponent(list[1]);

            }
        }
    }

    //封装一个删除cookie的方法
    function removeCookie(key){
        document.cookie = key + "=;expires=" + new Date(0) + ";path=/";
    }
    removeCookie("url");
    alert(getCookieValue("url"));

</script>

js创建cookie与获取cookie
本身在js中写了成立和得到cookie的方法,并且调用了setCookie(“name”,”haha”),那么旁人在中远距离连接小编Computer时候会不会将此cookie存入他的浏览器中。

图片 1

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图