《JavaWeb---利用cookie记录用户的历史浏览》

代码虽少,却遇到了很多问题。感觉也学到了很多东西。

//展示商品,“首页”Servlet
package com.fenghuo.cookie;

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

	/**
	 *  Title:利用cookie技术记录客户的历史浏览
	 *  Copyright: Copyright (c) 2012
	 *  @author: 烽火
	 *  @version 1.0 2012-09-14
	 */
	private static final long serialVersionUID = 4408943836189873374L;

	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.print("我们店有如下商品:(点击查看详情)
"); //得到”数据库“数据 Map map = DB.getAll(); //展示商品,将商品id通过链接带给ShowObject out.print("=========================
"); for (int i = 1; i < map.size()+1; i++){ out.print(""+map.get(i+"").getName()+"
"); } //显示曾经浏览的商品 out.print("您曾经浏览过的商品:
"); //得到用户带来的cookie值,返回值是cookie数组 Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++){ //找到我们想要的cookie if (cookies[i].getName().equals("historyCookie")){ String[] ids = cookies[i].getValue().split("\\,"); //得到cookie中存在的id,展现浏览过的商品 for (String id : ids){ out.print(""+map.get(id).getName()+"
"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } //自定义数据 class DB{ private static LinkedHashMap map = new LinkedHashMap(); static{ map.put("1", new MyObject("1", "杯子", "12.0", "烽火牌杯子就是好!")); map.put("2", new MyObject("2", "毛巾", "5.0", "吸水性刚刚的!")); map.put("3", new MyObject("3", "脸盆", "8.0", "容积大,可放衣物多!")); map.put("4", new MyObject("4", "肥皂", "6.0", "强劲去污能力强!")); map.put("5", new MyObject("5", "暖瓶", "18.0", "不保温退货!")); } public static Map getAll(){ return map; } } class MyObject{ private String id; private String name; private String price; private String describe; public MyObject(String id, String name, String price, String describe) { super(); this.id = id; this.name = name; this.price = price; this.describe = describe; } 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 getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } }

//显示用户的详细信息的Servlet
package com.fenghuo.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
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 ShowObject extends HttpServlet {

	/**
	 *  Title:输出客户要看的商品,同时回写cookie
	 *  Copyright: Copyright (c) 2012
	 *  @author: 烽火
	 *  @version 1.0 2012-09-14
	 */
	private static final long serialVersionUID = 2894544204585860934L;

	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();
		
		//得到Url中的id
		String id = request.getParameter("id");
		
		//获取”数据库“中数据
		Map map = DB.getAll();
		//根据id得到浏览的商品
		MyObject myobj = (MyObject) map.get(id);
		
		out.write("你要看的商品是:"+myobj.getName()+"
"); out.write("价格:"+myobj.getPrice()+"
"); out.write("对该商品的描述是:
"); out.write(myobj.getDescribe()); //给浏览器回写cookie String cookieValue = buildCookie(id, request);//产生想要的cookie中的值 Cookie cookie = new Cookie("historyCookie", cookieValue); cookie.setMaxAge(1*24*3600); cookie.setPath("/myweb"); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { String historyCookie = null; //得到请求中带来的cookie值 Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++){ if (cookies[i].getName().equals("historyCookie") ){ historyCookie = cookies[i].getValue(); } } //如果为空返回当前商品的id if (historyCookie == null){ return id; } LinkedList list = new LinkedList( Arrays.asList((historyCookie.split("\\,")))); //对不同的情况进行分析返回id的值 if (list.contains(id)){ list.remove(id); }else{ if (list.size() >= 5){ list.removeLast(); } } list.addFirst(id); StringBuffer sb = new StringBuffer(); for (String sid : list){ sb.append(sid + ","); } sb.deleteCharAt(sb.length()-1); return sb.toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
第一次浏览商品产生cookie


刷新显示历史浏览


浏览下一商品


刷新显示


再浏览第一次浏览的商品刷新移到前面


浏览多个商品



你可能感兴趣的:(《JavaWeb---利用cookie记录用户的历史浏览》)