一、基本概念
cookie是web服务器通过浏览器保存在www用户端硬盘上的一个文本文件,这个文件中包含了文本信息文本信息的内容是以“名/值”对的形式进行储存。可以让web开发者通过程序读写这个文本文件在Windows XP中保存Cookie的目录是:C:\Documents and Settings\用户名\Cookies。
Cookie的作用是解决浏览器用户与web服务器之间无状态通信。
二、注意点
cookie的大小和数量是有限制的。
cookie在个人硬盘上所保存的文本信息是以明文格式进行保存的,没有任何的加密措施。
浏览器可以设定不使用cookie。
三、代码
cookiesInput.html
<html>
<head>
<title>cookie input page</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
请输入用户名<br>
<form name="form1" action="SetCookiesServlet" method="post" >
<table border="0">
<tr><td>用户名:</td>
<td><input type="text" name="username">
</td></tr>
<tr><td colspan="2" align="center">
<input name="submit" value="submit" type="submit">
</td></tr>
</table>
</form>
</body>
</html>
SetCookiesServlet
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import webbook.util.StringUtil;
public class SetCookiesServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String output = null;
String username = request.getParameter("username");
if (!StringUtil.validateNull(username)) {
Cookie cookie1 = new Cookie("username", StringUtil.filterHtml(username));
// cookie的有效期为1个月
cookie1.setMaxAge(24 * 60 * 60 * 30);
response.addCookie(cookie1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Cookie cookie2 = new Cookie("lastTime", sdf.format(new Date()));
cookie2.setMaxAge(24 * 60 * 60 * 30);
response.addCookie(cookie2);
output = "本次登录时间与用户名已经写到Cookie中";
}
else {
output = "用户名为空,请重新输入";
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>set cookies </title></head>");
out.println("<body>");
out.println("<h2>" + output + "</h2>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
GetCookiesServlet
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 GetCookiesServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 获取所有的cookie值
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
if (cookie.getName().equals("username")) {
out.println("用户名:" + cookie.getValue());
}
if (cookie.getName().equals("lastTime")) {
out.println("上次登录时间:" + cookie.getValue());
}
}
out.flush();
out.close();
}
}
源代码的作者是XHW,日期是2011-7-22
四、销毁Cookie
Cookie cookies[] = request.getCookies();
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
}
}
原帖地址:http://www.cnblogs.com/hongten/archive/2011/07/22/2114448.html