Cookie
瀏覽器與WEB服務器之間是使用HTTP協議進行通信的,當某個用戶發出頁面請求時,WEB服務器只是簡單的進行響應,然後就關閉與該用戶的連接。因此當一個請求發送到WEB服務器時,無論其是否是第一次來訪,服務器都會把它當作第一次來對待,這樣的不好之處可想而知。爲了彌補這個缺陷,Netscape開發出了cookie這個有效的工具來保存某個用戶的識別信息。cookies是一種WEB服務器通過瀏覽器在訪問者的硬盤上存儲信息的手段:Netscape Navigator使用一個名爲cookies.txt本地文件保存從所有站點接收的Cookie信息;而IE瀏覽器把Cookie信息保存在類似於 C://windows//cookies的目錄下。當用戶再次訪問某個站點時,服務端將要求瀏覽器查找並返回先前發送的Cookie信息,來識別這個用戶。 jsp是通過以下語法格式來創建Cookie的
Cookie cookie_name =new Cookie("Parameter","Value"); //例如 Cookie username_Cookie = new Cookie("username","zhangsan"); response.addCookie(username_Cookie);
JSP是調用Cookie對象相應的構造函數Cookie(name,value)用合適的名字和值來創建Cookie,然後Cookie可以通過response的addCookie方法加入到Set-Cookie應答頭
Cookie的各種方法
- String getComment():返回cookie中註釋,如果沒有註釋的話將返回空值
- String getDomain():返回cookie中Cookie適用的域名 使用getDomain() 方法可以指示瀏覽器把Cookie返回給同 一域內的其他服務器,而通常Cookie只返回給與發送它的服務器名字完全相同的服務器。注意域名必須以點開始(例如.wmathor.com)
- int getMaxAge():返回Cookie過期之前的最大時間,以秒計算
- String getName():返回Cookie的名字
- String getPath():返回Cookie適用的路徑。如果不指定路徑,Cookie將返回給當前頁面所在目錄及其子目錄下的所有頁面
- boolean getSecure():如果瀏覽器通過安全協議發送cookies將返回true值,如果瀏覽器使用標準協議則返回false值
- String getValue():返回Cookie的值
- int getVersion():返回Cookie所遵從的協議版本
- void setComment(String purpose):設置cookie註釋
- void setDomain(String pattern):設置cookie中Cookie適用的域名
- void setMaxAge(int expiry):以秒計算,設置Cookie過期時間
- void setValue(String newValue):Cookie創建後設置一個新的值
- void setVersion(int v):設置Cookie遵從的協議版本
- void setSecure(boolean flag):指出瀏覽器使用的安全協議,例如HTTPS或SSL
寫入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>Cookie Write 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"> </head> <body> <% Cookie cookie_a = new Cookie("a_name","a_value");//name,value response.addCookie(cookie_a); Cookie cookie_b = new Cookie("b_name","b_value"); cookie_b.setMaxAge(30 * 24 * 60 * 60);//單位是秒 response.addCookie(cookie_b); Cookie cookie_c = new Cookie("c_name","c_value"); cookie_c.setMaxAge(-1); //-1是一個session Cookie,瀏覽器打開有用,關閉時刪除 //cookie_c.setPath(當前路徑);默認有一個調用 response.addCookie(cookie_c); Cookie cookie_d = new Cookie("d_name","d_value"); cookie_d.setMaxAge(30 * 24 * 60 * 60);//單位是秒 cookie_d.setPath("/jsp"); response.addCookie(cookie_d); Cookie cookie_e = new Cookie("e_name","e_value"); cookie_e.setMaxAge(30 * 24 * 60 * 60);//單位是秒 cookie_e.setPath("/");//服務器的根目錄 response.addCookie(cookie_e); %> <!--HttpSession 設置Session的最大呆滯時間 銷燬Session invalidate 獲取SessionID SessionId Object getAttribute(String name) void setAttribute(String name,Object value); application ServerletContext Object getAttribute(String name) void setAttribute(String name,Object value); --> <a href = "/jsp/c03/cookie.jsp">返回</a> </body> </html>
讀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>Cookie Read 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"> <jsp:include page = "../res.jsp"></jsp:include> </head> <body class = "container"> <% Cookie[] cookies = request.getCookies(); if(cookies == null || cookies.length == 0) { out.print("沒有Cookie"); return; } %> <table class = "table table-bordered"> <% for(Cookie cookie : cookies) { %> <tr> <td><%= cookie.getName() %></td> <td><%= cookie.getValue() %></td> <td><%= cookie.getMaxAge() %></td> <td><%= cookie.getDomain() %></td> <td><%= cookie.getPath() %></td> </tr> <% } %> </table> <% %> <a href = "/jsp/c03/cookie.jsp">返回</a> </body> </html>