//商品浏览详细信息记录商品id,存到cookie中 package com.kt.servlet_qt; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; 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 com.kt.bean.Easybuy_Shangpin; import com.kt.bean.Easybuy_ShangpinFenlei; import com.kt.dao.impl.Easybuyimpl; public class merchandisetype extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String uid = request.getParameter("uid"); String uname = request.getParameter("uname"); Easybuyimpl e = new Easybuyimpl(); Easybuy_Shangpin all_ID_Shangpin = e.all_ID_Shangpin(Integer .parseInt(uid)); // 拿到商品id int id = all_ID_Shangpin.getEp_id(); // 保存商品id Cookie cookie1 = null; // 初始创建cookie cookie1 = new Cookie("cookis_id", ""); cookie1.setValue(id + ","); // 多少秒过期 cookie1.setMaxAge(7 * 24 * 60 * 60); // 添加到客户端 response.addCookie(cookie1); // 先获取cookie是否有值 Cookie[] ck = request.getCookies(); // 进行判断 if (ck != null) { // 定义累加值的变量为空 String value = ""; // 循环cookie拿值 for (Cookie cookie : ck) { // 判断是否存在cookis_id名称 if (cookie.getName().equals("cookis_id")) { // 拿到cookie最新值 value = cookie.getValue(); // 将cookie分割以(",")分割 String[] split = value.split(","); // 创建set集合,("因为set能自动识别重复值") Set<String> set = new HashSet<String>(); // 将拿到的值循环放到set中进行判断是否存在相同的数 for (int i = 0; i < split.length; i++) { set.add(split[split.length - (i + 1)]); } // 清空所定义的值,进行下一轮拼接 value = ""; // 创建一个数组 String[] array = new String[] {}; // 将set集合的值转换到数组中 String[] toArray = (String[]) set.toArray(array); // 这里进行你要判断你要显示多少条最新记录 if (toArray.length < 4) { // 创建集合 List<String> list = new ArrayList<String>(); // 循环toArray数组将值添加到list集合中 for (int i = 0; i < toArray.length; i++) { list.add(toArray[i]); } // 循环判断拿到的值是否有相同的如果有就移除 for (int i = 0; i < list.size(); i++) { if (Integer.parseInt(list.get(i)) == id) { list.remove(i); } } // 最后进行拿到list集合的数据进行拼接 for (String string : list) { value += string + ","; } // 然后添加最新的数据到cookie中 cookie1 = new Cookie("cookis_id", value + id + ","); } else {// 如果大于你要显示的数据条数 // 创建集合 List<String> list = new ArrayList<String>(); // 循环toArray数组将值添加到list集合中 for (int i = 0; i < toArray.length; i++) { list.add(toArray[toArray.length - (i + 1)]); } // 定义一个接收被移除的 String li = list.get(0); // 移除 list.remove(0); // 循环判断是否有相同的、和进行value拼接最新的数据 for (String string : list) { if (Integer.parseInt(string) == id) { string = li; } if (Integer.parseInt(string) != id) { value += string + ","; } } // 添加最新数据cookis_id cookie1 = new Cookie("cookis_id", value + id + ","); } // 多少秒过期 cookie1.setMaxAge(7 * 24 * 60 * 60); // 添加到客户端 response.addCookie(cookie1); } } } // // 商品分类查询所有 List<Easybuy_ShangpinFenlei> all_ShangpinFenlei = e .all_ShangpinFenlei(); request.getSession().setAttribute("all_ShangpinFenlei", all_ShangpinFenlei); request.getSession().setAttribute("uname", uname); request.getSession().setAttribute("all_ID_Shangpin", all_ID_Shangpin); request.getRequestDispatcher("product-view.jsp").forward(request, response); out.flush(); out.close(); } } //获取cookie拿到值,传给jsp界面 package com.kt.servlet_qt; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; 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 com.kt.bean.Easybuy_Shangpin; import com.kt.bean.Easybuy_ShangpinFenlei; import com.kt.bean.Easybuy_xinwen; import com.kt.dao.impl.Easybuyimpl; public class blank extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); // 获取cookie所存放的值 Cookie[] cookies = request.getCookies(); if (cookies != null) {// 判断cookie是否为空 // 创建集合 ArrayList<Easybuy_Shangpin> all_cookle_Shangpin = new ArrayList<Easybuy_Shangpin>(); // 定义一个用分解cookie的变量 String value = ""; // 循环cookie判断是否存在cookis_id for (Cookie cookie : cookies) { if (cookie.getName().equals("cookis_id")) { // 接收cookie的值 value = cookie.getValue(); // 然后以(",")分解拿到数据ID String[] split = value.split(","); // 循环添加到 all_cookle_Shangpin集合中将最新数据传递给jsp页面 for (int i = 0; i < split.length; i++) { all_cookle_Shangpin.add(e .all_cookle_Shangpin( Integer.parseInt(split[split.length - (i + 1)])).get(0));// split[split.length- // (i + // 1)]指的是从最新的开始拿数据 } // 存到会话中 request.getSession().setAttribute("all_cookle_Shangpin", all_cookle_Shangpin); } } } // 使用转发 request.getRequestDispatcher("index.jsp").forward(request, response); out.flush(); out.close(); } }
时间: 2024-09-30 22:48:18