商品浏览记录的处理

获取记录:

servlet代码:

import java.io.IOException;
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 com.huida.utils.CookUtils;

/*
 * 记录商品浏览记录
 * */
public class GetProductByIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码
		//获取当前访问的商品
		String id=request.getParameter("id");
		String ids="";
		//获取指定的cookies ids 1-2-3
		Cookie c=CookUtils.getCookieByName("ids", request.getCookies());
		//判断cookie是否为空
		if(c==null){
			//若为空 需要将当前商品的id放入ids中
			ids=id;
		}else{
			//若不为空,继续判断id是否已在ids中
			//获取值
			ids=c.getValue();
			String[] arr=ids.split("-");
			//数组长度固定     数组与集合间的转换
			List<String> asList=Arrays.asList(arr);
			LinkedList<String> list=new LinkedList<String>(asList);
			if(list.contains(id)){
				//若ids中包含id  将id移除  放到最前面
				list.remove(id);
				list.addFirst(id);
			}else{
				//若ids中不包含id  继续判断长度是否大于2
				if(list.size()>2){
					//移除最后一个  将当前的放入最前面
					list.removeLast();
					list.addFirst(id);
				}else{
					//长度<3 将当前放入最前面
					list.addFirst(id);
				}
			}
			ids="";
			//将list转换成字符串
			for(String s:list){
				ids+=(s+"-");
			}
			ids=ids.substring(0, ids.length()-1);
		}
		//将ids写回去
		c=new Cookie("ids",ids);
		//设置访问路径
		c.setPath(request.getContextPath()+"/");
		//设置存存活时间
		c.setMaxAge(3600);
		//写回浏览器
		response.addCookie(c);
		//跳转到指定的商品页面上
		response.sendRedirect(request.getContextPath()+"/product_info"+id+".htm");
	}

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

}

jsp浏览记录部分代码:

<%
					//获取指定名称的cookie
					Cookie c = CookUtils.getCookieByName("ids", request.getCookies());
					//判断ids是否为空
					if(c==null){

					%>
					<h2>暂无浏览记录</h2>
					<%
					}else{
						
						String[] arr = c.getValue().split("-");
						for(String id:arr){

					%>
					<li style="width: 150px;height: 216;float: left;margin: 0 8px 0 0;padding: 0 18px 15px;text-align: center;"><img src="products/1/cs1000<%=id %>.jpg" width="130px" height="130px" /></li>
					
					<%
						}
						
					}
					
					%>

清除记录:

import java.io.IOException;
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 ClearHistoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//创建一个cookie
		Cookie c=new Cookie("ids","");
		c.setPath(request.getContextPath()+"/");
		c.setMaxAge(0);
		response.addCookie(c);
		response.sendRedirect(request.getContextPath()+"/product_list.jsp");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_42735880/article/details/82949191