1 我们知道HTTP协议是无状态协议,也就是说每个请求都是独立的!无法记录前一次请求的状态。但HTTP协议中可以使用Cookie来完成会话跟踪!
在JavaWeb中,使用session来完成会话跟踪,session底层依赖Cookie技术。
2 Cookie
Cookie是由服务器创建,然后通过响应发送给客户端的一个键值对。客户端会保存Cookie,并会标注出Cookie的来源(哪个服务器的Cookie)。当客户端向服务器发出请求时会把所有这个服务器Cookie包含在请求中发送给服务器,这样服务器就可以识别客户端了!
3 cookie例子1
客户端访问AServlet,AServlet在响应中添加Cookie,浏览器会自动保存Cookie。然后客户端访问BServlet,这时浏览器会自动在请求中带上Cookie,BServlet获取请求中的Cookie打印出来。
package com.cug.cookie; import java.io.IOException; import java.util.UUID; 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 AServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); String id = UUID.randomUUID().toString(); Cookie ck1 = new Cookie("id",id); resp.addCookie(ck1); resp.getWriter().print("cookie 已经设置好!"); } }
package com.cug.cookie; import java.io.IOException; 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 BServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); Cookie[] cookies = req.getCookies(); for(Cookie c: cookies){ if(c.getName().equals("id")){ resp.getWriter().print(c.getValue()); } } } }
package com.cug.cookie; import java.io.IOException; 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; public class CServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); Cookie ck = new Cookie("lasttime",(new Date()).toString()); resp.addCookie(ck); String s = "您是首次访问本站吧!"; Cookie[] cookies = req.getCookies(); if(cookies != null){ for(Cookie c: cookies){ if(c.getName().equals("lasttime")){ s = "您上次访问的时间为:"+c.getValue(); } } } resp.getWriter().print(s); } }
package com.cug.cookie; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; 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 DServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); String name = URLEncoder.encode("朱","utf-8"); String value = URLEncoder.encode("zhu","utf-8"); Cookie ck = new Cookie(name,value); resp.addCookie(ck); Cookie[] cookies = req.getCookies(); for(Cookie c: cookies){ if(c.getName().equals(name)){ String name1 = URLDecoder.decode(c.getName(),"utf-8"); String value1 = URLDecoder.decode(c.getValue(), "utf-8"); resp.getWriter().print(name1 + ":" + value1); } } } }
原文地址:http://blog.csdn.net/bluzelee2011/article/details/42496715