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。

posted @ 2013-11-20 23:14  代号 11  阅读(523)  评论(0编辑  收藏  举报