cookie

Posted 跳动de手指

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cookie相关的知识,希望对你有一定的参考价值。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP ‘a.jsp‘ starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
     <%-- 
     cookie演示:
     cookie路径:不是打开每个浏览器页面,都会发送所有浏览器缓存的cookie,而是要看访问URL是否包含cookie保存路径;cookie保存路径是tomcat服务器保存的,默认为/day_11_2/Cookie;
     --%>
     <font color="red">保存Cookie</font>
  <%
  Cookie cookie = new Cookie("name","value");
  //设置cookie存活时间  >0 存活时间以秒计算  <0 表示存活时间为浏览器开启时间,一旦浏览器关闭cookie就灭亡   ;=0 cookie立即死亡
  cookie.setMaxAge(60*60);
  
  //保存cookie
  response.addCookie(cookie);
  %>
  <%-- 
  <% 
  //演示多个项目共享cookie,首先创建cookie对象设置cookie域,匹配2级域名例如:www.baidu.com  zhidao.baidu.com  设置cookie路径为“/”
  Cookie cookie1 = new Cookie("name","value");
  cookie1.setDomain(".baidu.com");
  cookie1.setPath("/");
  %>
  --%>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP ‘b.jsp‘ starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
  <font color="red">获取Cookie</font><hr/>
    <%
    //获取cookie注意返回的是数组
    Cookie[]cookies = request.getCookies();
    //判断cookie数组是否为null,如果不为null就遍历;
    if(cookies!=null){
        for(Cookie cookie : cookies){                            //获取Cookie路径
            out.print(cookie.getName()+":"+cookie.getValue()+":"+cookie.getPath()+"<br/>");
        }
    }
    %>
  </body>
</html>

 

以上是关于cookie的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript单行代码,也就是代码片段

XSS:如何从 C# 中的字符串中删除 JS 片段?

Django cookie 横幅:window.wpcc 未定义

最详细的cookie和浏览隐私之间的关系

c#如何采集需要登录的页面

IE无法获得cookie,ie不支持cookie的解决办法,火狐支持