简介
cookie的中文意思是小甜饼,然而在互联网上的意思就完全不通了,它和食品完全没有关系.在互联网中,cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器.通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用户等.浏览器将cookie以key/value的形式保存到客户机的某个指定目录中.
通过cookie的getCookies()方法即可获取所有cookie对象的集合:通过cookie对象的getName()方法可以获取指定名称的cookie,通过getValue()方法即可获取cookie对象的值.另外,将一个cookie对象发送到客户端使用了response对象的addCookie()方法.
实现代码
1.创建index.jsp页面文件,在其中创建from表单,用于让用户输入信息:并且从request对象中获取cookie,判断是否含有此服务器发送过的cookie.如果没有,则说明该用户第一次访问本站,如果有,则直接将值读取出来,并赋给对应的表单,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.net.URLDecoder"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>应用cookie跟踪用户</title> </head> <body> <% request.setCharacterEncoding("UTF8"); //设置请求编码 response.setCharacterEncoding("UTF8"); //设置相应编码 String welcome = "第一次访问"; String[] info = new String[] { "", "", "" }; //定义字符串数组 Cookie[] cook = request.getCookies(); //获取所有的cookie对象 if (cook != null) { for (int i = 0; i < cook.length; i++) { //循环遍历cookie对象 if (cook[i].getName().equals("mrsoft")) { //获取指定名称的cookie String cvalue = URLDecoder.decode(cook[i].getValue()); //将cookie对象进行编码转换,避免乱码问题 info = cvalue.split("#"); //将获取的cookie值进行拆分 welcome = ",欢迎回来!"; } } } %> <% String name = info[0]; //页面显示内容. %> <%=name + welcome%> <form action="6.2.6.show.jsp" method="post"> <ul style="line-height: 23"> <li>姓 名:<input name="name" type="text" value="<%=info[0]%>" /></li> <li>出生日期:<input name="birthday" type="text" value="<%=info[1]%>" /></li> <li>邮箱地址:<input name="mail" type="text" value="<%=info[2]%>" /></li> <li><input type="submit" value="提交" /></li> </ul> </form> </body> </html> |
2.創建show.jsp頁面文件,在該頁面中通過request对象将用户输入的表单信息提取出来;创建一个cookie对象,并通过response对象的addCookie()方法将其发送到客户端.代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.net.URLEncoder"%>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>创建cookie对象,发送到客户端</title> </head> <body> <% request.setCharacterEncoding("UTF8"); //设置请求编码,避免乱码. response.setCharacterEncoding("UTF8"); //设置相应编码,避免乱码 String name = request.getParameter("name"); //获取请求页面参数 String birthday = request.getParameter("birthday"); String mail = request.getParameter("mail"); Cookie myCook = new Cookie("mrsoft", URLEncoder.encode(name + "#" + birthday + "#" + mail));//创建cookie对象 myCook.setMaxAge(60 * 60 * 24 * 365);//设置cookie有效期. response.addCookie(myCook); //将cookie发送至客户端 %> 表单提交成功! <ul style="line-height: 24px"> <li>姓名:<%=name%></li> <li>生日:<%=birthday%></li> <li>邮箱地址:<%=mail%></li> <li><a href="6.2.6.index.jsp">返回</a></li> </ul> </body> </html> |