Cookie技术案例-——显示曾经浏览过的商品

本博客通过Cookie技术展示网站所有商品,并记录和显示用户曾经浏览过的产品。

CookieDemo1

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 CookieDemo1 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("本站有如下商品:<br/>");
		Map<String, Book> map = Db.getAll();
		for (Map.Entry<String, Book> entry : map.entrySet())
		{
			Book book = entry.getValue();
			out.print("<a target=\"_blank\" href='/day07/servlet/cookieDemo2?id="
							+ book.getId()
							+ "'>"
							+ book.getName()
							+ "</a><br/>");

		}
		// 显示用户看过的商品
		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 target=\"_blank\" href='/day07/servlet/cookieDemo4?id="
									+ book.getId()
									+ "'>"
									+ book.getName()
									+ "</a><br/>");
				}
			}
		}
	}
}
//用Db模拟数据库
class Db
{
	private static Map<String, Book> map = new LinkedHashMap<String, Book>();
	static
	{
		map.put("1", new Book("1", "JavaWeb开发", "老k", "一本好书"));
		map.put("2", new Book("2", "jdbc开发", "老张", "一本好书"));
		map.put("3", new Book("3", "spring开发", "老li", "一本好书"));
		map.put("4", new Book("4", "struts开发", "老张", "一本好书"));
		map.put("5", new Book("5", "android开发", "老bi", "一本好书"));
	}

	public static Map getAll()
	{
		return map;
	}

}

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)
	{
		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;
	}
}


CookieDemo2

 

import java.io.IOException;
import java.io.PrintWriter;
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;
//显示详细信息的servlet
public class CookieDemo2 extends HttpServlet
{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		// 根据用户带过来的id,显示相应的详细信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		Book book = (Book) Db.getAll().get(id);
		out.write(book.getId() + "<br/>");
		out.write(book.getName() + "<br/>");
		out.write(book.getAuthor() + "<br/>");
		out.write(book.getDescription() + "<br/>");
		// 2.构建cookie,回写给浏览器;
		String cookieValue = buildCookie(id, request);
		Cookie cookie = new Cookie("bookHistory", cookieValue);
		cookie.setMaxAge(1 * 30 * 24 * 3600);// 1 个月
		cookie.setPath("/day07");
		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 // 假如列表最多3个
		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;
		// if(bookHistroy.contains(id))不能这样 21,23 也包括1
		List<String> list = Arrays.asList(bookHistroy.split("\\,"));
		LinkedList<String> linkedlist = new LinkedList<String>(list);
		if (list.contains(id))
		{
			linkedlist.remove(id);
			linkedlist.addFirst(id);
		} else
		{
			if (list.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();
	}

}


 

当前,全球经济格局深刻调整,数字化浪潮席卷各行各业,智能物流作为现代物流发展的必然趋势和关键支撑,正迎来前所未有的发展机遇。以人工智能、物联网、大数据、云计算、区块链等前沿信息技术的快速迭代与深度融合为驱动,智能物流不再是传统物流的简单技术叠加,而是正在经历一场从自动化向智能化、从被动响应向主动预测、从信息孤岛向全面互联的深刻变革。展望2025年,智能物流系统将不再局限于提升效率、降低成本的基本目标,而是要构建一个感知更全面、决策更精准、执行更高效、协同更顺畅的智慧运行体系。这要求我们必须超越传统思维定式,以系统化、前瞻性的视角,全面规划和实施智能物流系统的建设。本实施方案正是基于对行业发展趋势的深刻洞察和对未来需求的精准把握而制定。我们的核心目标在于:通过构建一个集成了先进感知技术、大数据分析引擎、智能决策算法和高效协同平台的综合智能物流系统,实现物流全链路的可视化、透明化和智能化管理。这不仅是技术层面的革新,更是管理模式和服务能力的全面提升。本方案旨在明确系统建设的战略方向、关键任务、技术路径和实施步骤,确保通过系统化部署,有效应对日益复杂的供应链环境,提升整体物流韧性,优化资源配置效率,降低运营成本,并最终为客户创造更卓越的价值体验。我们致力于通过本方案的实施,引领智能物流迈向更高水平,为构建现代化经济体系、推动高质量发展提供强有力的物流保障。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值