使用cookies查询商品详情
易买网项目完工,把一些新知识记录下来,以便以后查阅,也方便他人借阅。介绍使用cookies查询商品详情。
第一步:建立商品实体类。
第二步:连接Oracle数据库。
第三步:使用三层架构。
效果图如下:
当我看中新疆牛肉干,商品点击时,进入查看商品详情页。
商品详情页:
核心代码如下:
- <%
- //创建商品业务逻辑对象
- productBiz prodctbiz = new productBizImpl();
- List<easybuy_product> productlist = prodctbiz.findproductList();
- request.setAttribute("productlist",product);
- %>
//EL表达式
- 核心架包
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
//EL表达式:- <c:forEach var="news" items="${requestScope.productlist}" >
- <li class="ck">
- <dl>
- <dt><a href="addcookie?id=${news.ep_id}"><img src="${news.ep_file_name}" /></a></dt>
- <dd class="title"><a href="addcookie?id=${news.ep_id}">${news.ep_name}</a></dd>
- <dd class="price">¥${news.ep_price}.00</dd>
- </dl>
- </li>
- </c:forEach>
第二步:在Servlet创建addcookie.java页面,获取商品id:(注意:必须在web.xml写入)
- <!--商品id存在cookies-->
- <servlet>
- <servlet-name>addcookie</servlet-name>
- <servlet-class>Servlet.addcookie</servlet-class>
- </servlet>
- <!-- 映射servlet -->
- <servlet-mapping>
- <servlet-name>addcookie</servlet-name>
- <url-pattern>/addcookie</url-pattern>
- </servlet-mapping>
- package Servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- 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 addcookie extends HttpServlet {
- /**
- * Constructor of the object.
- */
- public addcookie() {
- super();
- }
- /**
- * Destruction of the servlet. <br>
- */
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- request.setCharacterEncoding("utf-8");
- //获取商品id
- String id = request.getParameter("id");
- //转发的页面
- response.setHeader("refresh", "0;url=/yimaiWang/product-view.jsp?id="+id);
- Cookie[] cookies = request.getCookies();
- String visitlist = null;
- if (cookies != null) {
- for (Cookie cookie : cookies) {
- if (cookie.getName().equals("visitlist")) {
- visitlist = cookie.getValue();
- break;
- }
- }
- if (visitlist == null) {
- Cookie cookie = new Cookie("visitlist", id);
- cookie.setMaxAge(180);
- response.addCookie(cookie);
- } else {
- String[] existIds = visitlist.split(",");
- for (String exsitId : existIds) {
- if (exsitId.equals(id)) {
- return;
- }
- }
- Cookie cookie = new Cookie("visitlist", visitlist + "," + id);
- cookie.setMaxAge(180);
- response.addCookie(cookie);
- }
- } else {
- Cookie cookie = new Cookie("visitlist", id);
- cookie.setMaxAge(180);
- response.addCookie(cookie);
- }
- }
- }
第三步:跳转商品详情页product-view.jsp(这俩个查询语句不同,一个是查询商品id,一个是商品List集合)
- public easybuy_product findProductForid(int id) {
- con=this.getConnection();
- int i =id;
- String sql = "select * from easybuy_product where ep_id =?";
- easybuy_product pd = new easybuy_product();
- try
- {
- st=con.prepareStatement(sql);
- st.setInt(1,id);
- rs=st.executeQuery();
- while(rs.next())
- {
- pd.setEp_id(rs.getInt(1));
- pd.setEp_name(rs.getString(2));
- pd.setEp_description(rs.getString(3));
- pd.setEp_price(rs.getInt(4));
- pd.setEp_stock(rs.getInt(5));
- pd.setEpc_id(rs.getInt(6));
- pd.setEpc_child_id(rs.getInt(7));
- pd.setEp_file_name(rs.getString(8));
- }
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- }finally{
- this.ShiFang(rs, st, con);
- }
- return pd;
- }
- }
- public List<easybuy_product> product(String id) {
- List<easybuy_product> listproduct=new ArrayList<easybuy_product>();
- // TODO Auto-generated method stub
- con = this.getConnection();
- String sql="select * from easybuy_product where ep_id=?";
- try {
- st=con.prepareStatement(sql);
- st.setString(1,id);
- rs=st.executeQuery();
- while(rs.next()){
- easybuy_product product = new easybuy_product();
- product.setEp_id(rs.getInt(1));
- product.setEp_name(rs.getString(2));
- product.setEp_description(rs.getString(3));
- product.setEp_price(rs.getInt(4));
- product.setEp_stock(rs.getInt(5));
- product.setEpc_id(rs.getInt(6));
- product.setEpc_child_id(rs.getInt(7));
- product.setEp_file_name(rs.getString(8));
- listproduct.add(product);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- this.ShiFang(rs, st, con);
- }
- return listproduct;
- }
<%
//获取商品id
int id = Integer.parseInt(request.getParameter("id"));
productBiz bizvoid = new productBizImpl();
easybuy_product shop = bizvoid.findProductForid(id);
request.setAttribute("shop",shop);
%>
- <%
- //获取商品id
- request.setCharacterEncoding("utf-8");
- String a = request.getParameter("id");
- %>
- <%
- //创建商品信息业务逻辑对象
- productBiz productbiz = new productBizImpl();
- List<easybuy_product> list =productbiz.product(a);
- request.setAttribute("list",list);
- %>
- <div id="product" class="main">
- <c:forEach var="product" items="${requestScope.list}" >
- <h1><%=shop.getEp_name() %></h1>
- </c:forEach>
- <div class="infos">
- <c:forEach var="product" items="${requestScope.list}" >
- <div class="thumb"><img src="${product.ep_file_name}" width="300px" /></div>
- <div class="buy">
- <p>商品描述:<span class="price">${product.ep_description}</span></p>
- <p>商城价:<span class="price">¥${product.ep_price}.00</span></p>
- <c:if test="${product.ep_stock==null}">
- <p class="w1 c">缺货</p>
- </c:if>
- <c:if test="${product.ep_stock!=null}">
- <p class="w1 c">有货</p>
- </c:if>
- <c:if test="${name==null}">
- <script type="text/javascript">
- function ck(){
- alert("你未登入,请去登入吧!");
- return false;
- }
- </script>
- </c:if>
使用cookies查询商品详情的更多相关文章
- 使用cookies查询商品浏览记录
经历了俩个星期,易买网项目如期完工,现在总结一下如何使用cookies实现浏览商品的历史记录. 第一步:创建商品实体类. 第二步:连接oracle数据库. 第三步:创建商品三层架构. 效果图: 在要显 ...
- Vue框架H5商城类项目商品详情点击返回弹出推荐商品弹窗的实现方案
需求场景: 非推荐商品详情页返回的时候弹出弹窗推荐商品,点击弹窗按钮可以直接访问推荐商品: 只有直接进入商品详情页返回才会弹出推荐商品弹窗: 每个用户访问只能弹一次(除非清除缓存). 需求分析: 1. ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- SSH网上商城---商品详情页的制作
在前面的博文中,小编分别简单的介绍了邮件的发送以及邮件的激活,逛淘宝的小伙伴都有这样的体会,比如在搜索框中输入连衣裙这个商品的时候,会出现多种多样各种款式的连衣裙,连衣裙的信息包括价格,多少人购买,商 ...
- 如何用Baas快速在腾讯云上开发小程序-系列4:实现客户侧商品列表、商品详情页程序
版权声明:本文由贺嘉 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/431172001487671163 来源:腾云阁 h ...
- JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存
1. 学习计划 1.Activemq整合spring的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. 使用方法 ...
- [springboot 开发单体web shop] 8. 商品详情&评价展示
上文回顾 上节 我们实现了根据搜索关键词查询商品列表和根据商品分类查询,并且使用到了mybatis-pagehelper插件,讲解了如何使用插件来帮助我们快速实现分页数据查询.本文我们将继续开发商品详 ...
- Day13_商品详情及静态化
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 0.学习 ...
- Android开发案例 - 淘宝商品详情
所有电商APP的商品详情页面几乎都是和淘宝的一模一样(见下图): 采用上下分页的模式 商品基本参数 & 选购参数在上页展示 商品图文详情等其他信息放在下页展示 知识要点 垂直方向的ViewPa ...
随机推荐
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor
There is a tree with nn nodes. For each node, there is an integer value a_iai, (1 \le a_i \le 1,0 ...
- Android第三方文件选择器:aFileChooser
Android第三方文件选择器:aFileChooser aFileChooser是Android平台上的一个第三方文件选择器,其在github上的项目主页是:https://github.co ...
- [转]十五天精通WCF——第七天 Close和Abort到底该怎么用才对得起观众
一:文起缘由 写这一篇的目的源自于最近看同事在写wcf的时候,用特别感觉繁琐而且云里雾里的嵌套try catch来防止client抛出异常,特别感觉奇怪,就比如下面的代码. public void S ...
- 解决ubuntu上opengl的问题
装完ubuntu之后,对于opengl的程序总是出现问题,先将解决方案列出如下: http://www.linuxforums.org/forum/ubuntu-linux/175490-graphi ...
- MVC.Net:Razor指定模板
在MVC.Net开发中,我们通常会在_ViewStart.cshtml中指定一个默认的模板,在文件开头输入如下代码: @{ Layout = "~/Views/Shared/[自己定义的模板 ...
- jQuery事件整理回想
一.事件 1.载入DOM $(document).ready() 这个第一节里具体介绍了 2.事件绑定 jQuery定义了bind()方法作为统一的接口.用来为每个匹配元素绑定事件处理程序. 其基本的 ...
- 【IOS 开发】Object - C 入门 之 数据类型具体解释
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/38544659 1. 数据类型简单介绍及输出 (1) 数据类型 ...
- C/C++大小端模式与位域
一.大端小端: 1.大端:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中 例如:0x12345678 在内存中的存储为 : 0x0000 0x0001 0x0002 0x00 ...
- oc39-- 类的内存存储
虚线是isa的指向,实线是继承关系. // // main.m // 类的本质 #import <Foundation/Foundation.h> #import "Person ...
- 请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码。
请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码. 2013-06-18 11:07会飞的鱼儿18 | 分类:C#/.NET | 浏览101次 不能有空 ...