1。什么是 Cookie?
Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递。用户每次访问站点时,Web 应用程序都可以读取 Cookie 包含的信息。
假设在用户请求访问网站上的某个页面时,服务器端应用程序发送给该用户的不仅仅是一个页面,还有一个包含日期和时间的 Cookie。用户的浏览器在获得页面的同时还得到了这个 Cookie,并且将它保存在用户硬盘上的某个文件夹中。
以后,如果该用户再次访问您站点上的页面,当该用户输入这个网站网址时,浏览器就会在本地硬盘上查找与该 URL 相关联的 Cookie。如果该 Cookie 存在,浏览器就将它与页面请求一起发送到您的站点,服务器应用程序就能确定该用户上一次访问站点的日期和时间。这样就可以根据这些信息向用户发送一条消息,也可以检查过期时间或执行其他有用的功能。
2。cookie的限制
大多数浏览器只允许每个站点保存 20 个 Cookie。如果试图保存更多的 Cookie,则最先保存的 Cookie 就会被删除。还有些浏览器会对来自所有站点的 Cookie 总数作出限制,这个限制通常为 300 个。
3。创建Cookie
eclipse打开,创建一servlet类,名称为CookieTest.java
package test;
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 CookieTest extends HttpServlet ... {
/** *//**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
Cookie cookieName = new Cookie("name","tom");
cookieName.setMaxAge(3600);
Cookie cookiePassword = new Cookie("password","123456");
cookiePassword.setMaxAge(3600);
response.addCookie(cookieName);
response.addCookie(cookiePassword);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/** *//**
* 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
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
this.doGet(request, response);
}
}
真正自己编写的只有几句话:
Cookie cookieName = new Cookie("name","tom");
cookieName.setMaxAge(3600);
Cookie cookiePassword = new Cookie("password","123456");
cookiePassword.setMaxAge(3600);
response.addCookie(cookieName);
response.addCookie(cookiePassword);
cookie创建完毕,当用IE打开这个网页时,会创建一个cookie,在系统默认的ie临时文件夹中可以找到。
4。取出Cookie值
创建一个servlet类:GetCookie
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 GetCookie extends HttpServlet ... {
/** *//**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
Cookie[] cookies = request.getCookies();
String name = "";
String password = "";
for (int i = 0; i < cookies.length; i++) ...{
if (cookies[i].getName().equals("name")) ...{
name = cookies[i].getValue();
}
if (cookies[i].getName().equals("password")) ...{
password = cookies[i].getValue();
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("<br>");
out.println("name:" + name);
out.println("<br>");
out.println("password:" + password);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/** *//**
* 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
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
this.doGet(request, response);
}
}
主要语句:
Cookie[] cookies = request.getCookies();
String name = "";
String password = "";
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("name")) {
name = cookies[i].getValue();
}
if (cookies[i].getName().equals("password")) {
password = cookies[i].getValue();
}
}
5。在eclipse中将这个项目和Tomcat关联,打开ie就会看到结果。注意地址栏中要输入
http://localhost:8080/ + 项目名 + /servlet/ +servlet类名。