先来看看效果图:
点击submit后:
点击查看Cookies:
在C:\Documents and Settings\Administrator\Cookies目录下面会有一个 hongten@webproject_cookie[2].txt 的文件;
这就是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>
<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.java
代码:
/**
*
*/
package webbook.chapter6;
*
*/
package webbook.chapter6;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
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 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 {
*
* @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);
}
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中。<br><a href=\"/webproject_cookie/GetCookiesServlet\">查看Cookies</a>";
} else {
output = "用户名为空,请重新输入。<br><a herf=\"/webproject_cookie/cookieInput.htm\">输入用户名</a>";
}
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中。<br><a href=\"/webproject_cookie/GetCookiesServlet\">查看Cookies</a>";
} else {
output = "用户名为空,请重新输入。<br><a herf=\"/webproject_cookie/cookieInput.htm\">输入用户名</a>";
}
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();
}
}
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.java
代码:
/**
*
*/
package webbook.chapter6;
*
*/
package webbook.chapter6;
import java.io.IOException;
import java.io.PrintWriter;
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;
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 {
*
* @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);
}
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("<html>");
out.println("<head><title>display login infomation</title></head>");
out.println("<body>");
out.println("<h2>从Cookie中获得上次登录时间与用户名</h2>");
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>display login infomation</title></head>");
out.println("<body>");
out.println("<h2>从Cookie中获得上次登录时间与用户名</h2>");
//获取所有的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("<br>");
}
if (cookie.getName().equals("lastTime")) {
out.println("上次登录时间:" + cookie.getValue());
out.println("<br>");
}
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
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("<br>");
}
if (cookie.getName().equals("lastTime")) {
out.println("上次登录时间:" + cookie.getValue());
out.println("<br>");
}
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
cookie是web服务器通过浏览器保存在www用户端硬盘上的一个文本文件,这个文件中包含了文本信息
文本信息的内容是以“名/值”对(key/value)的形式进行储存的
可以让web开发者通过程序读写这个文本文件
在Windows XP中保存Cookie的目录是:C:\Documents and Settings\用户名\Cookies
Cookie的作用:
解决浏览器用户与web服务器之间无状态通信
使用cookie的时候注意的事项:
cookie的大小和数量是有限制的
cookie在个人硬盘上所保存的文本信息是以明文格式进行保存的,没有任何的加密措施
浏览器可以设定不使用cookie