• JAVA中对Cookie的操作


    (1)往 Cookie 中存值:

    <%@page import="javax.xml.ws.Response"%>
    <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <%
    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>Cookie的使用案例</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 c1 =new Cookie("wth","wutiaohui");  //定义Cookie对象
        Cookie c2 =new Cookie("soso","www.baidu.com");  //定义Cookie对象  
        
        response.addCookie(c1);//向客户端添加Cookie
        response.addCookie(c2);
         %>
      </body>
    </html>


    (2)取出 Cookie 中的值 :

    <%@page import="javax.xml.ws.Response"%>
    <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <%
    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>Cookie的使用案例</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 c[]=request.getCookies();  //获取全部的Cookie
        for(int i = 0 ;i <c.length;i++){
         %>
         
         <h2><%=c[i].getName() %>  ---->  <%=c[i].getValue() %></h2>
         <%
        }
         %>
      </body>
    </html>

    (2)对 Cookie 中的值 进行 时间设置 :

    <%@page import="javax.xml.ws.Response"%>
    <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <%
    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>Cookie的使用案例</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 c1 =new Cookie("wth","wutiaohui");  //定义Cookie对象
        Cookie c2 =new Cookie("soso","www.baidu.com");  //定义Cookie对象  
        c1.setMaxAge(15); //设置Cookie的有效时间为    15 秒
        response.addCookie(c1);//向客户端添加Cookie
        response.addCookie(c2);
         %>
      </body>
    </html>

     注意

            虽然,Cookie 中可以存放信息,但是并不是可以无限的保持,一般一个客户端最多只能保存300个 Cookie 所以数据量太大时 将无法使用 Cookie。

  • 相关阅读:
    面试常见问题汇总
    java static变量及函数
    java自定义注解及其信息提取
    testNG 注释实例
    让我欲罢不能的node.js
    利用html 5 websocket做个山寨版web聊天室(手写C#服务器)
    html5 Web Workers
    html5 postMessage解决跨域、跨窗口消息传递
    C# socket编程实践——支持广播的简单socket服务器
    简单理解Socket
  • 原文地址:https://www.cnblogs.com/softmans/p/3434592.html
Copyright © 2020-2023  润新知