利用cookie显示商品的浏览记录

需求介绍:利用cookie技术,在“你曾经看过的商品 ”列表中显示顾客最新浏览的3件商品(最后一次浏览的在最上边,以此类推)。

主页面为:IndexInf.java

package CookieDemo;

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 IndexInf extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		//解决中文乱码问题
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		//输出所有商品
		out.write("网站商品如下 : 
"); Map map = Db.getAll(); for(Map.Entry entry : map.entrySet()) { Book book = entry.getValue(); out.print(""+book.getName()+"
"); } //显示用户看过的商品 out.print("
你曾经看过的商品 :
"); Cookie[] cookies = request.getCookies(); for(int i=0;cookies!=null && i"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } //用类来简单模拟数据库 class Db { private static Map map = new LinkedHashMap(); static { map.put("1",new Book("1","Java开发","赵钱","好书啊。")); map.put("2",new Book("2","JavaWeb开发","孙李","好书啊。")); map.put("3",new Book("3","Jdbc开发","周武","好书啊。")); map.put("4",new Book("4","Spring开发","郑王","好书啊。")); map.put("5",new Book("5","Android开发","马六","好书啊。")); } public static Map getAll() { return map; } } //创建Book类 class Book { private String id; private String name; private String author; private String description; public Book() { super(); } public Book(String id, String name, String author, String description) { super(); 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; } }

显示商品信息页面为ShowGoodsInf.java:

package CookieDemo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

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 ShowGoodsInf extends HttpServlet
{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		//解决中文乱码问题
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		//根据用户传送过来的id显示商品的详细信息
		String id = request.getParameter("id");
		Book book = (Book) Db.getAll().get(id);
		out.write(book.getId()+"
"); out.write(book.getAuthor()+"
"); out.write(book.getName()+"
"); out.write(book.getDescription()+"
"); //构建Cookie,回写给浏览器 String cookieValue = buildCookie(id,request); Cookie cookie = new Cookie("bookHistory",cookieValue); cookie.setMaxAge(30*24*60*60); cookie.setPath("/CookieTest"); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { String bookHistory = null; Cookie[] cookies = request.getCookies(); for(int i=0;cookies!=null && i list = new LinkedList(Arrays.asList(bookHistory.split("\\,"))); if(list.contains(id)) { list.remove(id); } else if(list.size()>=3) //控制显示用户浏览过的商品最多为3个 { list.removeLast(); } list.addFirst(id); StringBuffer sb = new StringBuffer(); for(String bookId : list) { sb.append(bookId+","); } return sb.deleteCharAt(sb.length()-1).toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

两者在web.xml中的配置为:



  
  
    IndexInf
    CookieDemo.IndexInf
  
  
    ShowGoodsInf
    CookieDemo.ShowGoodsInf
  

  
    IndexInf
    /servlet/IndexInf
  
  
    ShowGoodsInf
    /servlet/ShowGoodsInf
  	
  
    index.jsp
  

运行结果:

当浏览主页时:
利用cookie显示商品的浏览记录_第1张图片

点击“Spring开发”后跳转页面至:

利用cookie显示商品的浏览记录_第2张图片

通过一系列浏览后,刷新主显示页面后看到:

 利用cookie显示商品的浏览记录_第3张图片

你可能感兴趣的:(Java)