《Cookie4案例》——————记录浏览的商品,并可以清除浏览记录,只有登录之后才可以浏览

本案例是在案例3的基础上进行添加了浏览商品的代码

浏览页面

<%@page import="com.offcn.bean.User"%>
<%@page import="com.offcn.utils.CookieUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

   <%
       User user = (User)session.getAttribute("user");
   
       if(user==null){  // 说明没有登录
    	   
    	   response.sendRedirect("login.jsp?msg=pleaselogin");
       }
   
   %>

   <table>
     <tr>
       <td>
           <img src="img/1.jpg" width="300px" height="200px" />
           <div><a href="FindGoodsServlet?id=1">萝卜</a></div>
       </td>
       <td>
           <img src="img/2.jpg" width="300px" height="200px" />
           <div><a href="FindGoodsServlet?id=2">冬瓜</a></div>
       </td>
       <td>
           <img src="img/3.jpg" width="300px" height="200px" />
           <div><a href="FindGoodsServlet?id=3">西红柿</a></div>
       </td>
     </tr>
     
     <tr>
       <td>
           <img src="img/4.jpg" width="300px" height="200px" />
           <div><a href="FindGoodsServlet?id=4">芹菜</a></div>
       </td>
       <td>
           <img src="img/5.jpg" width="300px" height="200px" />
           <div><a href="FindGoodsServlet?id=5">土豆</a></div>
       </td>
       <td>
           <img src="img/6.jpg" width="300px" height="200px" />
           <div><a href="FindGoodsServlet?id=6">胡萝卜</a></div>
       </td>
     </tr>
   </table>
   <hr/>
   <div>
      浏览记录: <a href="ClearServlet">清除浏览记录</a>
   </div>
   
   <%
       Cookie[] cs = request.getCookies();
       Cookie c = CookieUtils.findCookieByName(cs,"ids");
       
       if(c!=null){
    	   String value = c.getValue();
    	   String[] ss = value.split(",");
    	   for(String s:ss){
    %>
        <img src="img/<%=s %>.jpg" width="150px" height="100px" />
    
    <%  		   
    	   }
       }
   
   %>
   
</body>
</html>

2.商品详细信息以及图片详情

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   <img src="img/${requestScope.id }.jpg" width="400px" height="500px" />
   <div>产地:...</div>
   <div>价格:...</div>
   <div>数量:...</div>
   <div>
     <a href="main.jsp">返回</a>
   </div>
</body>
</html>

主程序的入口

package com.offcn.servlet;

import java.io.IOException;
import java.util.Arrays;
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.offcn.utils.CookieUtils;

public class FindGoodsServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String id = request.getParameter("id");
		
		/*
		 *如果是真实的项目 应该根据id到数据库中查询数据,跳转到展示页面
		 *这里就不真查询数据库了就用id值充当商品
		 * */
		
		// 使用Cookie记录浏览过哪些商品
		//Cookie c = new Cookie("ids","2,1,3,4")
		Cookie[] cs = request.getCookies();
		Cookie c = CookieUtils.findCookieByName(cs,"ids");
		
		if(c==null){  // 说明是第一次浏览商品
			Cookie cookie  = new Cookie("ids",id);
			cookie.setMaxAge(10000);
			response.addCookie(cookie);
		}else{ // 浏览过商品
			
			// 要判断是否浏览过本商品
			String value = c.getValue();
			String[] ss = value.split(",");
			List<String> list = Arrays.asList(ss);
			
			if(!list.contains(id)){
				value=value+","+id;
				Cookie cookie  = new Cookie("ids",value);
				cookie.setMaxAge(10000);
				response.addCookie(cookie);
			}

		}
		request.setAttribute("id", id);
		request.getRequestDispatcher("view.jsp").forward(request, response);
		
	}

}

清除浏览记录:

package com.offcn.servlet;

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;

import com.offcn.utils.CookieUtils;

public class ClearServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		// 第1种解决方案
		Cookie c  = new Cookie("ids","");
		response.addCookie(c);
		*/
		Cookie[] cs = request.getCookies();
		Cookie c = CookieUtils.findCookieByName(cs, "ids");
		c.setMaxAge(0);
		response.addCookie(c);
		
		response.sendRedirect("main.jsp");
		
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值