摘要:像很多购物网站、淘宝,京东等购物网站,细心的朋友都会发现页面的一侧都会有那种浏览过的商品历史记录信息,之前也只是听说这个是使用编程语言的cookie机制实现的,但是具体怎么实现的还不是很清楚,今天分享一下如何实现,希望对大家的编程语言学习有所帮助。
像很多购物网站、淘宝,京东等购物网站,细心的朋友都会发现页面的一侧都会有那种浏览过的商品历史记录信息,之前也只是听说这个是使用编程语言的cookie机制实现的,但是具体怎么实现的还不是很清楚,今天分享一下如何实现,希望对大家的编程语言学习有所帮助。
查询商品列表servlet如下:
package com.iotek.cookies;
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 BuyGoods extends HttpServlet {
private static final long serialVersionUID = 1L;
public BuyGoods() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println("我们的商城商品信息列表如下:
");
Mapmap=Goods.getAllGoods();
out.println("==========================
");
System.out.println(map.size()+"****");
for(int i=1;i<map.size()+1;i++){
out.println(""+map.get(i+"").getName()+"
");
}
out.println("**************************
");
out.println("您曾经浏览的商品列表如下
");
//查询cookie的值
Cookie []cookies=request.getCookies();
for(int i=0;cookies!=null&&i<cookies.length;i++){
if(cookies[i].getName().equals("history")){
String []ids=cookies[i].getValue().split("\\,");
for(String id:ids){
out.println(""+map.get(id).getName()+"
");
}
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
class Goods{
private static LinkedHashMapmap=new LinkedHashMap();
private String id; //商品ID
private String name;
private double price;
static{
map.put("1", new Goods("1","java",99));
map.put("2", new Goods("2","c++",99));
map.put("3", new Goods("3","jsp",99));
map.put("4", new Goods("4","linux",99));
map.put("5", new Goods("5","springmvc",99));
map.put("6", new Goods("6","hibernate",99));
}
public static MapgetAllGoods(){
return map;
}
public Goods() {
// TODO Auto-generated constructor stub
}
public Goods(String id, String name, double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
显示浏览历史记录servlet:
package com.iotek.cookies;
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 ShowGoods extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
String id=request.getParameter("id"); //查当前ID商品的详细信息;
Mapmap=Goods.getAllGoods();
Goods goods=map.get(id);
out.println("商品名称"+goods.getName()+"
");
out.println("商品价格"+goods.getPrice()+"
");
//写入cookie对象;
String cookieValue=addCookieValue(id,request);
Cookie cookie=new Cookie("history",cookieValue);
cookie.setMaxAge(60*60*24*7);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}
public String addCookieValue(String id,HttpServletRequest request){
String historyCookie=null;
Cookie []cookies=request.getCookies();
for(int i=0;cookies!=null&&i=5){
list.removeLast();
}
}
list.addFirst(id);
StringBuffer sb=new StringBuffer();
for(String ids:list){
sb.append(ids+",");
}
sb.deleteCharAt(sb.length()-1);
return sb.toString();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
希望这篇文章可以帮助到你,总之同学们,IT资讯尽在职坐标。
喜欢 | 2
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号