--------------------------页面-----------------------
<%@ 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>My JSP 'userCookie.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form name="form1" method="post" action="ServletCookie" οnsubmit="return checkSubmit()">
<label>
<input type="submit" name="Submit" value="访问cookie">
</label>
</form>
</body>
</html>
--------------------servlet--------------------
package cookie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletCookie extends HttpServlet {
public ServletCookie() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
/*《====Cookie的用法与session的用法极类似 只是session是在服务器用的 而cookie是在客户端用的====》
* @author-何桂坤-2010-9-28
* String getComment() 返回cookie中注释,如果没有注释的话将返回空值.
* String getDomain() 返回cookie中Cookie适用的域名
* int getMaxAge() 返回Cookie过期之前的最大时间,以秒计算。
* String getName() 返回Cookie的名字。名字和值是我们始终关心的两个部分
* String getValue() 返回Cookie的值
* String getPath() 返回Cookie适用的路径
* boolean getSecure() 如果浏览器通过安全协议发送cookies将返回true值,如果浏览器使用标准协议则返回false值。
* int getVersion() 返回Cookie所遵从的协议版本。
* void setComment(String purpose) 设置cookie中注释。
*void setDomain(String pattern) 设置cookie中Cookie适用的域名
*void setMaxAge(int expiry) 以秒计算,设置Cookie过期时间。
*void setPath(String uri) 指定Cookie适用的路径。
*void setSecure(boolean flag) 指出浏览器使用的安全协议,例如HTTPS或SSL。
*void setValue(String newValue) cookie创建后设置一个新的值。
*void setVersion(int v) 设置Cookie所遵从的协议版本
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");// 设置编码
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();// 获得cookies数组
int count = 0;
boolean bool = true;// 第一次标识假值
Cookie cookie = null;// 声明单个cookie
if (cookies != null) {
// 循环数组
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];// 获得数组里面的值
// 判断cookie的名称是否为heguikun
if ("heguikun".equals(cookie.getName())) {
// 取出次数并累加一
count = Integer.parseInt(cookie.getValue()) + 1;
cookie.setValue(count + "");// 重新设置cookie累加后的值
cookie.setMaxAge(60 * 60 * 24);// 有效时间 60s*60s*24 = 一天
response.addCookie(cookie);// 返回到客户端
out.println("你是第" + count + "次登录该网页。");// 页面显示登录该网页的次数
bool = false;// 不是第一次后标识真值
}
if ("useName".equals(cookie.getName())) {
out.println("<br/>");
out.println("本次登录的用户为:" + cookie.getValue());// 页面显示登录该网页的次数
out.println("<br/>");
out.println("<br/>");
response.addCookie(cookie);// 返回到客户端
}
}
}
if (bool) {// 第一次访问是进入
out.println("你是第 1 次登录该网页。在此之前没有相关Cookie信息。");
cookie = new Cookie("heguikun", "1");// 把访问次数放到cookie中
// 默认该cookie是存储在浏览器的内在中,用户关闭浏览器则被删除,下面的方法是将cookie存储在硬盘上。
//默认存到C盘下 在Intent选项中可以打开这个文件夹
cookie.setMaxAge(60 * 60 * 24);// 设置最大时效一天,如果设置为0则是删除该cookie
response.addCookie(cookie);// 返回到客户端
Cookie cookieUser = new Cookie("useName", "heguikun");// 把登录用户放到cookie中,不能存储中文,
cookieUser.setMaxAge(60 * 60 * 24);
response.addCookie(cookieUser);// 返回到客户端
}
out.println("<a href='cookie/userCookie.jsp'>返回首页 </a>");
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}