在C:\Documents and Settings\Administrator\Cookies目录下面会有一个
的文件;
这就是cookie的文件;
再来看看代码是怎样实现的:
cookiesInput.html
代码:
cookie input page请输入用户名(英文或者数字)
用户名: | |
SetCookiesServlet.java
代码:
/**
*
*/
package webbook.chapter6;
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;
/**
*
*
@author XHW
*
@date 2011-7-22
*
*/
public class SetCookiesServlet extends HttpServlet {
private static final long serialVersionUID = 6849802930688070121L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
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);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Cookie cookie2 = new Cookie("lastTime", sdf.format(new Date()));
cookie2.setMaxAge(24 * 60 * 60 * 30);
response.addCookie(cookie1);
response.addCookie(cookie2);
output = "本次登录时间与用户名已经写到Cookie中。查看Cookies";
} else {
output = "用户名为空,请重新输入。输入用户名";
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("
set cookies ");out.println("
");out.println("
" + output + "
");out.println("");
out.println("");
out.flush();
out.close();
}
}
GetCookiesServlet.java
代码:
/**
*
*/
package webbook.chapter6;
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;
/**
*
*
@author XHW
* @date 2011-7-22
*
*/
public class GetCookiesServlet extends HttpServlet {
private static final long serialVersionUID = -156960358689563625L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("
display login infomation");out.println("
");out.println("
从Cookie中获得上次登录时间与用户名
");//获取所有的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());
out.println("
");
}
if (cookie.getName().equals("lastTime")) {
out.println("上次登录时间:" + cookie.getValue());
out.println("
");
}
}
out.println("");
out.println("");
out.flush();
out.close();
}
}
cookie是web服务器通过浏览器保存在www用户端硬盘上的一个文本文件,这个文件中包含了文本信息
文本信息的内容是以“名/值”对(key/value)的形式进行储存的
可以让web开发者通过程序读写这个文本文件
在Windows XP中保存Cookie的目录是:C:\Documents and Settings\用户名\Cookies
Cookie的作用:
解决浏览器用户与web服务器之间无状态通信
使用cookie的时候注意的事项:
cookie的大小和数量是有限制的
cookie在个人硬盘上所保存的文本信息是以明文格式进行保存的,没有任何的加密措施
浏览器可以设定不使用cookie