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>
Cookie 是很方便,了解越多功能越强大