易买网项目完工,把一些新知识记录下来,以便以后查阅,也方便他人借阅。介绍使用cookies查询商品详情。

第一步:建立商品实体类。

第二步:连接Oracle数据库。

第三步:使用三层架构。

效果图如下:

当我看中新疆牛肉干,商品点击时,进入查看商品详情页。

商品详情页:

核心代码如下:

  1. <%
  2. //创建商品业务逻辑对象
  3. productBiz prodctbiz = new productBizImpl();
  4.  
  5. List<easybuy_product> productlist = prodctbiz.findproductList();
  6. request.setAttribute("productlist",product);
  7. %>
    //EL表达式
  1. 核心架包
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    //EL表达式:
  2. <c:forEach var="news" items="${requestScope.productlist}" >
  3.  
  4. <li class="ck">
  5. <dl>
  6. <dt><a href="addcookie?id=${news.ep_id}"><img src="${news.ep_file_name}" /></a></dt>
  7. <dd class="title"><a href="addcookie?id=${news.ep_id}">${news.ep_name}</a></dd>
  8. <dd class="price">¥${news.ep_price}.00</dd>
  9. </dl>
  10.  
  11. </li>
  12.  
  13. </c:forEach>

第二步:在Servlet创建addcookie.java页面,获取商品id:(注意:必须在web.xml写入)

  1. <!--商品id存在cookies-->
  2. <servlet>
  3. <servlet-name>addcookie</servlet-name>
  4. <servlet-class>Servlet.addcookie</servlet-class>
  5. </servlet>
  6.  
  7. <!-- 映射servlet -->
  8. <servlet-mapping>
  9. <servlet-name>addcookie</servlet-name>
  10. <url-pattern>/addcookie</url-pattern>
  11. </servlet-mapping>
  1. package Servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.Cookie;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. public class addcookie extends HttpServlet {
  13.  
  14. /**
  15. * Constructor of the object.
  16. */
  17. public addcookie() {
  18. super();
  19. }
  20.  
  21. /**
  22. * Destruction of the servlet. <br>
  23. */
  24. public void destroy() {
  25. super.destroy(); // Just puts "destroy" string in log
  26. // Put your code here
  27. }
  28.  
  29. public void doGet(HttpServletRequest request, HttpServletResponse response)
  30. throws ServletException, IOException {
  31.  
  32. doPost(request, response);
  33. }
  34.  
  35. public void doPost(HttpServletRequest request, HttpServletResponse response)
  36. throws ServletException, IOException {
  37.  
  38. response.setContentType("text/html;charset=utf-8");
  39. PrintWriter out = response.getWriter();
  40. request.setCharacterEncoding("utf-8");
  41. //获取商品id
  42. String id = request.getParameter("id");
  43. //转发的页面
  44. response.setHeader("refresh", "0;url=/yimaiWang/product-view.jsp?id="+id);
  45.  
  46. Cookie[] cookies = request.getCookies();
  47.  
  48. String visitlist = null;
  49. if (cookies != null) {
  50. for (Cookie cookie : cookies) {
  51. if (cookie.getName().equals("visitlist")) {
  52. visitlist = cookie.getValue();
  53. break;
  54. }
  55. }
  56. if (visitlist == null) {
  57.  
  58. Cookie cookie = new Cookie("visitlist", id);
  59. cookie.setMaxAge(180);
  60. response.addCookie(cookie);
  61.  
  62. } else {
  63.  
  64. String[] existIds = visitlist.split(",");
  65. for (String exsitId : existIds) {
  66. if (exsitId.equals(id)) {
  67.  
  68. return;
  69. }
  70. }
  71.  
  72. Cookie cookie = new Cookie("visitlist", visitlist + "," + id);
  73. cookie.setMaxAge(180);
  74. response.addCookie(cookie);
  75.  
  76. }
  77. } else {
  78.  
  79. Cookie cookie = new Cookie("visitlist", id);
  80. cookie.setMaxAge(180);
  81. response.addCookie(cookie);
  82.  
  83. }
  84. }
  85.  
  86. }

第三步:跳转商品详情页product-view.jsp(这俩个查询语句不同,一个是查询商品id,一个是商品List集合)

  1. public easybuy_product findProductForid(int id) {
  2. con=this.getConnection();
  3. int i =id;
  4. String sql = "select * from easybuy_product where ep_id =?";
  5.  
  6. easybuy_product pd = new easybuy_product();
  7.  
  8. try
  9. {
  10. st=con.prepareStatement(sql);
  11. st.setInt(1,id);
  12. rs=st.executeQuery();
  13.  
  14. while(rs.next())
  15. {
  16.  
  17. pd.setEp_id(rs.getInt(1));
  18. pd.setEp_name(rs.getString(2));
  19. pd.setEp_description(rs.getString(3));
  20. pd.setEp_price(rs.getInt(4));
  21. pd.setEp_stock(rs.getInt(5));
  22. pd.setEpc_id(rs.getInt(6));
  23. pd.setEpc_child_id(rs.getInt(7));
  24. pd.setEp_file_name(rs.getString(8));
  25. }
  26. } catch (SQLException e)
  27. {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. return null;
  31. }finally{
  32. this.ShiFang(rs, st, con);
  33.  
  34. }
  35.  
  36. return pd;
  37. }
  38. }
  1. public List<easybuy_product> product(String id) {
  2. List<easybuy_product> listproduct=new ArrayList<easybuy_product>();
  3. // TODO Auto-generated method stub
  4.  
  5. con = this.getConnection();
  6.  
  7. String sql="select * from easybuy_product where ep_id=?";
  8. try {
  9. st=con.prepareStatement(sql);
  10. st.setString(1,id);
  11. rs=st.executeQuery();
  12. while(rs.next()){
  13. easybuy_product product = new easybuy_product();
  14. product.setEp_id(rs.getInt(1));
  15. product.setEp_name(rs.getString(2));
  16. product.setEp_description(rs.getString(3));
  17. product.setEp_price(rs.getInt(4));
  18. product.setEp_stock(rs.getInt(5));
  19. product.setEpc_id(rs.getInt(6));
  20. product.setEpc_child_id(rs.getInt(7));
  21. product.setEp_file_name(rs.getString(8));
  22.  
  23. listproduct.add(product);
  24. }
  25. } catch (SQLException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28.  
  29. this.ShiFang(rs, st, con);
  30. }
  31.  
  32. return listproduct;
  33. }

<%
//获取商品id
int id = Integer.parseInt(request.getParameter("id"));
productBiz bizvoid = new productBizImpl();
easybuy_product shop = bizvoid.findProductForid(id);
request.setAttribute("shop",shop);
%>

  1. <%
  2. //获取商品id
  3. request.setCharacterEncoding("utf-8");
  4. String a = request.getParameter("id");
  5. %>
  6. <%
  7.  
  8. //创建商品信息业务逻辑对象
  9. productBiz productbiz = new productBizImpl();
  10.  
  11. List<easybuy_product> list =productbiz.product(a);
  12. request.setAttribute("list",list);
  13.  
  14. %>
  15. <div id="product" class="main">
  16. <c:forEach var="product" items="${requestScope.list}" >
  17. <h1><%=shop.getEp_name() %></h1>
  18. </c:forEach>
  19. <div class="infos">
  20. <c:forEach var="product" items="${requestScope.list}" >
  21. <div class="thumb"><img src="${product.ep_file_name}" width="300px" /></div>
  22. <div class="buy">
  23. <p>商品描述:<span class="price">${product.ep_description}</span></p>
  24. <p>商城价:<span class="price">¥${product.ep_price}.00</span></p>
  25. <c:if test="${product.ep_stock==null}">
  26. <p class="w1 c">缺货</p>
  27. </c:if>
  28. <c:if test="${product.ep_stock!=null}">
  29. <p class="w1 c">有货</p>
  30. </c:if>
  31. <c:if test="${name==null}">
  32. <script type="text/javascript">
  33. function ck(){
  34. alert("你未登入,请去登入吧!");
  35. return false;
  36. }
  37. </script>
  38.  
  39. </c:if>

使用cookies查询商品详情的更多相关文章

  1. 使用cookies查询商品浏览记录

    经历了俩个星期,易买网项目如期完工,现在总结一下如何使用cookies实现浏览商品的历史记录. 第一步:创建商品实体类. 第二步:连接oracle数据库. 第三步:创建商品三层架构. 效果图: 在要显 ...

  2. Vue框架H5商城类项目商品详情点击返回弹出推荐商品弹窗的实现方案

    需求场景: 非推荐商品详情页返回的时候弹出弹窗推荐商品,点击弹窗按钮可以直接访问推荐商品: 只有直接进入商品详情页返回才会弹出推荐商品弹窗: 每个用户访问只能弹一次(除非清除缓存). 需求分析: 1. ...

  3. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  4. SSH网上商城---商品详情页的制作

    在前面的博文中,小编分别简单的介绍了邮件的发送以及邮件的激活,逛淘宝的小伙伴都有这样的体会,比如在搜索框中输入连衣裙这个商品的时候,会出现多种多样各种款式的连衣裙,连衣裙的信息包括价格,多少人购买,商 ...

  5. 如何用Baas快速在腾讯云上开发小程序-系列4:实现客户侧商品列表、商品详情页程序

    版权声明:本文由贺嘉 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/431172001487671163 来源:腾云阁 h ...

  6. JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存

    1. 学习计划 1.Activemq整合spring的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. 使用方法 ...

  7. [springboot 开发单体web shop] 8. 商品详情&评价展示

    上文回顾 上节 我们实现了根据搜索关键词查询商品列表和根据商品分类查询,并且使用到了mybatis-pagehelper插件,讲解了如何使用插件来帮助我们快速实现分页数据查询.本文我们将继续开发商品详 ...

  8. Day13_商品详情及静态化

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 0.学习 ...

  9. Android开发案例 - 淘宝商品详情

    所有电商APP的商品详情页面几乎都是和淘宝的一模一样(见下图): 采用上下分页的模式 商品基本参数 & 选购参数在上页展示 商品图文详情等其他信息放在下页展示 知识要点 垂直方向的ViewPa ...

随机推荐

  1. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor

    There is a tree with nn nodes. For each node, there is an integer value a_ia​i​​, (1 \le a_i \le 1,0 ...

  2. Android第三方文件选择器:aFileChooser

     Android第三方文件选择器:aFileChooser aFileChooser是Android平台上的一个第三方文件选择器,其在github上的项目主页是:https://github.co ...

  3. [转]十五天精通WCF——第七天 Close和Abort到底该怎么用才对得起观众

    一:文起缘由 写这一篇的目的源自于最近看同事在写wcf的时候,用特别感觉繁琐而且云里雾里的嵌套try catch来防止client抛出异常,特别感觉奇怪,就比如下面的代码. public void S ...

  4. 解决ubuntu上opengl的问题

    装完ubuntu之后,对于opengl的程序总是出现问题,先将解决方案列出如下: http://www.linuxforums.org/forum/ubuntu-linux/175490-graphi ...

  5. MVC.Net:Razor指定模板

    在MVC.Net开发中,我们通常会在_ViewStart.cshtml中指定一个默认的模板,在文件开头输入如下代码: @{ Layout = "~/Views/Shared/[自己定义的模板 ...

  6. jQuery事件整理回想

    一.事件 1.载入DOM $(document).ready() 这个第一节里具体介绍了 2.事件绑定 jQuery定义了bind()方法作为统一的接口.用来为每个匹配元素绑定事件处理程序. 其基本的 ...

  7. 【IOS 开发】Object - C 入门 之 数据类型具体解释

    作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/38544659 1. 数据类型简单介绍及输出 (1) 数据类型 ...

  8. C/C++大小端模式与位域

    一.大端小端: 1.大端:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中 例如:0x12345678 在内存中的存储为  : 0x0000 0x0001 0x0002 0x00 ...

  9. oc39-- 类的内存存储

    虚线是isa的指向,实线是继承关系. // // main.m // 类的本质 #import <Foundation/Foundation.h> #import "Person ...

  10. 请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码。

    请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码. 2013-06-18 11:07会飞的鱼儿18 | 分类:C#/.NET | 浏览101次 不能有空 ...