展示浏览 java_java利用 cookie 进行展示你所浏览过的的商品

最近好烦~ 不过没什么大不了的

一个商品的展示界面,可以看到你以前浏览过的的界面。利用cookie

/**

* 商品的列表界面

*/

public class Servlet_products extends HttpServlet {

public static final String bookHistory="bookHistory";

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//1、展示商品的列表

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

out.write("商品的信息如下"+"
");

HashMap map=(HashMap) DB.getAll();

Set> set = map.entrySet();

for (Entry entry : set) {

Book book=entry.getValue();

//展示商品的数据

out.write(""+book.getName()+"");

out.write("
");

}

//2.展示以前浏览过的数据

out.write("
您曾经浏览过的商品:
" );

Cookie[] cookies = request.getCookies();

for (int i = 0;cookies!=null&& i < cookies.length; i++) {

Cookie c=cookies[i];

if(c.getName().equals(bookHistory))

{

//是这个界面给浏览器的

String value=c.getValue();

//我们存入用户浏览过商品的id 是 1_2_3 这种格式进行存储的

String ids[] = value.split("\\_");

for(String id: ids){

Book book = (Book) DB.getAll().get(id);

out.write(book.getName() + "
");

}

}

}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

//模拟数据库

public static class DB {

//有序的 map

public static LinkedHashMap map=new LinkedHashMap();

static {

//静态代码块 用于初始化 map的信息

map.put("1", new Book("1","javaweb开发","老张","一本好书"));

map.put("2", new Book("2","spring开发","老黎","一本好书"));

map.put("3", new Book("3","hibernate开发","老佟","一本好书"));

map.put("4", new Book("4","struts开发","老毕","一本好书"));

map.put("5", new Book("5","ajax开发","老张","一本好书"));

}

public static Map getAll(){

return map;

}

}

//商品的信息

public static class Book {

private String id;

private String name;

private String author;//作者

private String description; //描述

public Book() {

super();

// TODO Auto-generated constructor stub

}

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;

}

}

}

商品的详情界面

package jemo.cookie.demo;

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;

import jemo.cookie.demo.Servlet_products.Book;

import jemo.cookie.demo.Servlet_products.DB;

/**

* 点击具体的商品挑转到界面

*/

public class Servlet_productInfo extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

//1.根据用户带过来的id号,显示商品的详细信息

String id = request.getParameter("id");

Book book = (Book) DB.getAll().get(id);

out.write("您要查看的书的详细信息如下:
");

out.write(book.getId() + "
");

out.write(book.getName() + "
");

out.write(book.getAuthor() + "
");

out.write(book.getDescription() + "
");

//2.给用户回送包含当前商品id的cookie

String bookHistory = makeHistory(request,id); // 3_2

Cookie cookie = new Cookie(Servlet_products.bookHistory,bookHistory);

cookie.setMaxAge(1*30*24*3600);

response.addCookie(cookie);

}

/**

* 得到要给浏览器cookie

* @param request

* @param id

* @return

*/

private String makeHistory(HttpServletRequest request, String id) {

String bookHistory = null;

Cookie cookies[] = request.getCookies();

for(int i=0;cookies!=null&&i

if(cookies[i].getName().equals(Servlet_products.bookHistory)){

bookHistory = cookies[i].getValue();

}

}

// bookHistory=null 1 bookHistory=1

// bookHistory=3_1_5 1 bookHistory=1_3_5

// bookHistory=3_2_5 1 bookHistory=1_3_2

// bookHistory=3_2 1 bookHistory=1_3_2

// bookHistory=null 1 bookHistory=1

if(bookHistory==null){

return id;

}

List l = Arrays.asList(bookHistory.split("\\_")); //[3,4] //数组 链接

LinkedList list = new LinkedList();//对数据增删更有效

list.addAll(l);

if(list.contains(id)){

// bookHistory=3_1_5 1 bookHistory=1_3_5

list.remove(id);

list.addFirst(id);

}else{

if(list.size()>=3){

// bookHistory=3_2_5 1 bookHistory=1_3_2

list.removeLast();

list.addFirst(id);

}else{

// bookHistory=3_2 1 bookHistory=1_3_2

list.addFirst(id);

}

}

StringBuffer sb = new StringBuffer(); //2_3_4

for(String lid: list){

sb.append(lid + "_");

}

return sb.deleteCharAt(sb.length()-1).toString();//删除最后的_

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值