zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    2.2 建立示例数据库
    2.1.4 基本概念
    8. 在NOARCHIVELOG模式下用户管理的备份与恢复
    2.1.2 系统全局区
    考过042
    ORA1157错误解决手册(转)
    4. 用户管理的备份
    Kohana之LOG使用
    jq插件之easing
    Svn服务器之必须提交更改日志篇
  • 原文地址:https://www.cnblogs.com/softmans/p/3434592.html
Copyright © 2011-2022 走看看