【Cookie:默认存储在浏览器的内存中】显示上一次访问时间、商品浏览记录
2014-04-29 21:02
537 查看
Cookie的介绍:
Cookie是客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器。当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去。
如果创建了一个cookie,并将他发送到浏览器,默认情况下它是一个会话级别的cookie(即存储在浏览器的内存中),用户退出浏览器之后即被删除。
1、Cookie显示上一次访问时间
cookie.setPath("/HelloJavaWeb/servlet/ServletDemo"); 可以设置访问路径,不设置默认是/HelloJavaWeb/servlet/
保存的位置:
2、删除Cookie的方法、路径必须一致
3、显示商品的浏览记录
记住一条开发原则:有检索数据的需求使用双列集合,没有检索数据的需求使用单列集合。
LinkedHashMap:保持原有的顺序
显示商品和浏览记录的Servlet
package com.xiaozhi.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
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 CookieDemo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//1.输出所有商品
out.write("本站有如下商品:<br/>");
Map<String, Book> map = Db.getAll();
for (Map.Entry<String, Book> entry : map.entrySet())
{
Book book = entry.getValue();
out.print("<a href='" + request.getContextPath()+ "/servlet/CookieDemo2?id=" + book.getId() + "'>"+ book.getName() + "</a><br/>");
}
//4.显示用户看过的商品,这里显示三条浏览记录
out.print("<br/>你曾经看过的商品<br/>");
Cookie cookies[] = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++)
{
if (cookies[i].getName().equals("bookHistory"))
{
String ids[] = cookies[i].getValue().split("\\,");// 2,3,1
for (String id : ids)
{
Book book = (Book) Db.getAll().get(id);
out.print("<a href='" + request.getContextPath()+ "/servlet/CookieDemo2?id=" + book.getId() + "'>"+ book.getName() + "</a><br/>");
}
}
}
}
}
class Db1
{
private static Map<String, Book> map = new LinkedHashMap<String, Book>();
static
{
map.put("1", new Book("1", "JavaWeb开发", "老方", "一本好书"));
map.put("2", new Book("2", "jdbc开发", "老张", "一本好书"));
map.put("3", new Book("3", "spring开发", "老黎", "一本好书"));
map.put("4", new Book("4", "struts开发", "老张", "一本好书"));
map.put("5", new Book("5", "android开发", "老毕", "一本好书"));
}
public static Map getAll()
{
return map;
}
}
class Book1
{
private String id;
private String name;
private String author;
private String description;
public Book1()
{
super();
}
public Book1(String id, String name, String author, String description)
{
this.id = id;
this.name = name;
this.author = author;
this.description = description;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
查看商品详细信息的Servlet
Cookie是客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器。当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去。
如果创建了一个cookie,并将他发送到浏览器,默认情况下它是一个会话级别的cookie(即存储在浏览器的内存中),用户退出浏览器之后即被删除。
1、Cookie显示上一次访问时间
cookie.setPath("/HelloJavaWeb/servlet/ServletDemo"); 可以设置访问路径,不设置默认是/HelloJavaWeb/servlet/
package com.xiaozhi.controller; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; 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 ServletDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); Writer out=response.getWriter(); Cookie cookies[] = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++) { if (cookies[i].getName().equals("lastAccessTime")) { long cookieValue = Long.parseLong(cookies[i].getValue()); Date date = new Date(cookieValue); out.write("上次访问时间:"); out.write(date.toLocaleString()); } } Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+ ""); cookie.setMaxAge(3600);//设置cookie的有效时间长 cookie.setPath("/HelloJavaWeb/servlet/ServletDemo"); System.out.println(cookie.getPath()); response.addCookie(cookie); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
保存的位置:
2、删除Cookie的方法、路径必须一致
package com.xiaozhi.controller; import java.io.IOException; import java.io.OutputStream; 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 ServletDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+ ""); cookie.setMaxAge(0);//覆盖并删除 cookie.setPath("/HelloJavaWeb"); response.addCookie(cookie); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }到cookie文件夹下查看会发现已经删除
3、显示商品的浏览记录
记住一条开发原则:有检索数据的需求使用双列集合,没有检索数据的需求使用单列集合。
LinkedHashMap:保持原有的顺序
显示商品和浏览记录的Servlet
package com.xiaozhi.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
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 CookieDemo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//1.输出所有商品
out.write("本站有如下商品:<br/>");
Map<String, Book> map = Db.getAll();
for (Map.Entry<String, Book> entry : map.entrySet())
{
Book book = entry.getValue();
out.print("<a href='" + request.getContextPath()+ "/servlet/CookieDemo2?id=" + book.getId() + "'>"+ book.getName() + "</a><br/>");
}
//4.显示用户看过的商品,这里显示三条浏览记录
out.print("<br/>你曾经看过的商品<br/>");
Cookie cookies[] = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++)
{
if (cookies[i].getName().equals("bookHistory"))
{
String ids[] = cookies[i].getValue().split("\\,");// 2,3,1
for (String id : ids)
{
Book book = (Book) Db.getAll().get(id);
out.print("<a href='" + request.getContextPath()+ "/servlet/CookieDemo2?id=" + book.getId() + "'>"+ book.getName() + "</a><br/>");
}
}
}
}
}
class Db1
{
private static Map<String, Book> map = new LinkedHashMap<String, Book>();
static
{
map.put("1", new Book("1", "JavaWeb开发", "老方", "一本好书"));
map.put("2", new Book("2", "jdbc开发", "老张", "一本好书"));
map.put("3", new Book("3", "spring开发", "老黎", "一本好书"));
map.put("4", new Book("4", "struts开发", "老张", "一本好书"));
map.put("5", new Book("5", "android开发", "老毕", "一本好书"));
}
public static Map getAll()
{
return map;
}
}
class Book1
{
private String id;
private String name;
private String author;
private String description;
public Book1()
{
super();
}
public Book1(String id, String name, String author, String description)
{
this.id = id;
this.name = name;
this.author = author;
this.description = description;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
查看商品详细信息的Servlet
package com.xiaozhi.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; 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 CookieDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //2.根据用户带过来的id,显示相应的详细信息 response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String id = request.getParameter("id"); Book book = (Book) D 4000 b.getAll().get(id); out.write(book.getId() + "<br/>"); out.write(book.getName() + "<br/>"); out.write(book.getAuthor() + "<br/>"); out.write(book.getDescription() + "<br/>"); //3.构建cookie,回写给浏览器; String cookieValue = buildCookie(id, request); Cookie cookie = new Cookie("bookHistory", cookieValue); cookie.setMaxAge(1 * 30 * 24 * 3600);//1 个月 cookie.setPath(request.getContextPath()+"/servlet/"); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { // bookHistory =null 1 1 // bookHistory=2,5,1 1 1,2,5 // bookHistory=2,5,4 1 1,2,5 // bookHistroy=2,5 1 1,2,5 String bookHistroy = null; Cookie cookies[] = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++) { if (cookies[i].getName().equals("bookHistory")) { bookHistroy = cookies[i].getValue(); } } if (bookHistroy == null) return id; LinkedList<String> linkedlist =new LinkedList<String>(Arrays.asList(bookHistroy.split("\\,"))); if (linkedlist.contains(id)) { linkedlist.remove(id); linkedlist.addFirst(id); } else { if (linkedlist.size() >= 3) { linkedlist.removeLast(); linkedlist.addFirst(id); } else linkedlist.addFirst(id); } StringBuffer sb = new StringBuffer(); for (String bid : linkedlist) { sb.append(bid + ","); } return sb.deleteCharAt(sb.length() - 1).toString(); } }
相关文章推荐
- Servlet—Cookie(显示用户上次访问时间、显示商品浏览历史)
- Servlet—Cookie(显示用户上次访问时间、显示商品浏览历史)
- JavaWeb_02_cookie_显示商品浏览记录
- javaWeb 使用cookie显示商品浏览记录
- Cookie学习总结-显示上一次访问时间
- 利用cookie实现显示上一次访问时间
- Cookie应用之用户上次访问时间、用户浏览记录(转)
- java 使用cookie记录用户上一次访问的时间 记住 用户的 登录名
- Cookie应用之用户上次访问时间、用户浏览记录
- 利用Cookie保存数据,在浏览器页面显示用户上次访问时间
- Cookie学习总结-显示上一次访问时间
- 用Cookie实现显示上一次访问的时间和删除cookie
- 记录上一次访问时间 Cookie的使用
- javaweb之Cookie显示商品的浏览记录和Cookie的常见应用有哪些
- Cookie显示上一次的访问时间
- JavaWeb的Cookie技术显示历史商品( 信息 )浏览记录(学习方立勋老师的教程)
- Cookie学习总结-显示上一次访问时间
- 利用Cookie显示商品浏览历史记录
- cookie显示上次访问时间,显示历史记录(下)
- 利用cookie显示商品的浏览记录