cookie保存最近浏览的商品
2018-01-29 15:21
357 查看
@WebServlet(urlPatterns={"/index"}) (代替了在web中的配置)
public class ProductServlet extends HttpServlet{
ProductDaoImpl productDaoImpl= new ProductDaoImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action=request.getParameter("action");
//显示所有的产品信息
if("list".equals(action)){
//从cookie中读取最近浏览的记录
String cookieValue="";
cookieValue=selectcookieValue(request);
//写一个得到cookieValue的方法
Cookie [] arr= request.getCookies();
if(arr!=null){
for(Cookie c:arr){
if("recentView".equals(c.getName())){
cookieValue=c.getValue();
break;
}
}
}
List<ProductBean> products=new ArrayList<ProductBean>();
if(!"".equals(cookieValue)){
String [] ids=cookieValue.split(",");
for(int i=ids.length-1;i>=0;i--){ //让商品倒着实现
ProductBean product=productDaoImpl.productById(Integer.parseInt(ids[i]));
products.add(product);
}
}
request.setAttribute("retents", products);
List<ProductBean> productlist=productDaoImpl.proList();
request.setAttribute("lists", productlist);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
//显示产品的详细信息(再点击商品浏览详细信息时,把id存到cookie中)
if("view".equals(action)){
String idstr=request.getParameter("id");
addCookie(idstr,response,request);
//获取数据库中的产品详细信息
ProductBean product=productDaoImpl.productById(Integer.parseInt(idstr));
request.setAttribute("product", product);
request.getRequestDispatcher("product-view.jsp").forward(request, response);
}
}
private void addCookie(String idstr,HttpServletResponse response,HttpServletRequest request){
String cookieValue="";
boolean flage=true; //是否是一个新的浏览 是的话为true 否则为false
cookieValue=selectcookieValue(request);
String [] ids=cookieValue.split(",");
//cookieValue为空时 说明是第一个浏览
if("".equals(cookieValue)){
cookieValue=idstr;
}else{
//id不一样就不重新newcookie
for(String id:ids){
if(id.equals(idstr)){
flage=false;
break;
}
}
if(flage==true){
cookieValue=cookieValue+","+idstr;
}
//最近浏览只显示5条
if(ids.length>5){
cookieValue.substring(cookieValue.indexOf(",", 1)+1);
}
}
//向客户端发送一个cookie
Cookie cook=new Cookie("recentView",cookieValue);
response.addCookie(cook);
}
private String selectcookieValue(HttpServletRequest request){
String cookieValue="";
Cookie [] arr= request.getCookies();
//得到cookieValue
if(arr!=null){
for(Cookie c:arr){
if("recentView".equals(c.getName())){
cookieValue=c.getValue();
break;
}
}
}
return cookieValue;
}
}
public class ProductServlet extends HttpServlet{
ProductDaoImpl productDaoImpl= new ProductDaoImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action=request.getParameter("action");
//显示所有的产品信息
if("list".equals(action)){
//从cookie中读取最近浏览的记录
String cookieValue="";
cookieValue=selectcookieValue(request);
//写一个得到cookieValue的方法
Cookie [] arr= request.getCookies();
if(arr!=null){
for(Cookie c:arr){
if("recentView".equals(c.getName())){
cookieValue=c.getValue();
break;
}
}
}
List<ProductBean> products=new ArrayList<ProductBean>();
if(!"".equals(cookieValue)){
String [] ids=cookieValue.split(",");
for(int i=ids.length-1;i>=0;i--){ //让商品倒着实现
ProductBean product=productDaoImpl.productById(Integer.parseInt(ids[i]));
products.add(product);
}
}
request.setAttribute("retents", products);
List<ProductBean> productlist=productDaoImpl.proList();
request.setAttribute("lists", productlist);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
//显示产品的详细信息(再点击商品浏览详细信息时,把id存到cookie中)
if("view".equals(action)){
String idstr=request.getParameter("id");
addCookie(idstr,response,request);
//获取数据库中的产品详细信息
ProductBean product=productDaoImpl.productById(Integer.parseInt(idstr));
request.setAttribute("product", product);
request.getRequestDispatcher("product-view.jsp").forward(request, response);
}
}
private void addCookie(String idstr,HttpServletResponse response,HttpServletRequest request){
String cookieValue="";
boolean flage=true; //是否是一个新的浏览 是的话为true 否则为false
cookieValue=selectcookieValue(request);
String [] ids=cookieValue.split(",");
//cookieValue为空时 说明是第一个浏览
if("".equals(cookieValue)){
cookieValue=idstr;
}else{
//id不一样就不重新newcookie
for(String id:ids){
if(id.equals(idstr)){
flage=false;
break;
}
}
if(flage==true){
cookieValue=cookieValue+","+idstr;
}
//最近浏览只显示5条
if(ids.length>5){
cookieValue.substring(cookieValue.indexOf(",", 1)+1);
}
}
//向客户端发送一个cookie
Cookie cook=new Cookie("recentView",cookieValue);
response.addCookie(cook);
}
private String selectcookieValue(HttpServletRequest request){
String cookieValue="";
Cookie [] arr= request.getCookies();
//得到cookieValue
if(arr!=null){
for(Cookie c:arr){
if("recentView".equals(c.getName())){
cookieValue=c.getValue();
break;
}
}
}
return cookieValue;
}
}
相关文章推荐
- 最近浏览过的商品(保存到cookie)
- cookie记录最近浏览商品记录(取最新的五个浏览记录)
- servlet之利用Cookie显示最近浏览的商品
- 用Cookie实现购物车中只显示最近浏览的五件商品
- jquery+cookie+json保存浏览商品的记录
- 使用Cookie保存商品浏览记录
- 利用COOKIE实现最近浏览商品
- jQuery.cookie.js实现记录最近浏览过的商品功能示例
- 利用Cookie显示最近浏览的商品
- redis实战之使用redis保存最近浏览的商品数据
- cookie实现最近浏览商品
- laravel5.4 cookie商品最近浏览功能
- 使用Cookie实现保存商品浏览记录
- javaWEB总结(19):利用Cookie显示最近浏览的商品
- php 最近浏览过的商品及cookie的跨目录
- JQuery.cookie.js实现最近浏览过的商品
- xdocument cookie 最近浏览
- 用Cookie实现商品浏览记录(No.39)
- java之session--获取最近浏览商品(获取cookie值)算法思想及代码
- 用js操作cookie保存浏览记录