Javaweb编程中有一种方法称之为Cookie,在百度百科中,这样给cookie下定义:
Cookie,有时也用其复数形式 Cookies。类型为“小型文本文件”,是某些网站为了辨别用户身份,进行Session跟踪而储存在用户本地终端上的数据(通常经过加密),由用户客户端计算机暂时或永久保存的信息。
根据以上内容,也不难理解。cookie原意是“小饼干”,这又跟小饼干有什么关系呢?cookie还有一项含义是“网络饼干”,网络饼干(网络或互联网使用者发给中央服务器信息的计算机文件),这样就好理解了。
也可以将cookie理解为一封信件:某客户端第一次访问服务器时,服务器写了一封信给客户端(信上的内容是访问时间、访问对象等等),等到客户端下一次来访问服务器时,客户端就会带着这封信,这样服务器就知道这个客户端上次来过了,并且很清楚地了解上次访问的具体时间,本次访问结束之后,服务器又把这封信拿过来,更新为本次访问的信息。简单说,就是这样的一个过程。
示例
简单编写一个cookie的应用:
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 java.io.IOException; import java.io.PrintWriter; import java.util.Date; //保存用户上一次访问的时间 public class CookieDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解决中文乱码问题 req.setCharacterEncoding("GBK"); resp.setCharacterEncoding("GBK"); PrintWriter out = resp.getWriter(); //服务器端从客户端获取cookie Cookie[] cookies = req.getCookies(); //判断cookie是否存在 if (cookies!=null){ out.write("您上次访问本网站的时间是:"); for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; String cookieName = cookie.getName(); //获取cookie的名字 if (cookieName.equals("LastLoginTime")){ long llt = Long.parseLong(cookie.getValue()); //获取cookie中的值 Date date = new Date(llt); out.write(date.toLocaleString()); } } }else { out.write("这是您第一次访问本站!"); } //服务器给客户端响应一个cookie,覆盖上次的数据 Cookie lastLoginTime = new Cookie("LastLoginTime", System.currentTimeMillis()+""); resp.addCookie(lastLoginTime); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
用于清除cookie的类(将cookie的有效期设置为0即可):
package psl.wong.servlet; 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 java.io.IOException; import java.io.PrintWriter; import java.util.Date; //保存用户上一次访问的时间 public class CookieDemo02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie lastLoginTime = new Cookie("LastLoginTime", System.currentTimeMillis() + ""); lastLoginTime.setMaxAge(0); //关键代码 PrintWriter writer = resp.getWriter(); writer.write("This cookie has deleted!"); resp.addCookie(lastLoginTime); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
web.xml的注册:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true"> <servlet> <servlet-name>CookieDemo01</servlet-name> <servlet-class>psl.wong.servlet.CookieDemo01</servlet-class> </servlet> <servlet-mapping> <servlet-name>CookieDemo01</servlet-name> <url-pattern>/login1</url-pattern> </servlet-mapping> <servlet> <servlet-name>CookieDemo02</servlet-name> <servlet-class>psl.wong.servlet.CookieDemo02</servlet-class> </servlet> <servlet-mapping> <servlet-name>CookieDemo02</servlet-name> <url-pattern>/login2</url-pattern> </servlet-mapping> </web-app>