使用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 ...
随机推荐
- Python基础-while奇数和
利用while循环计算100*座位号 以内奇数的和. n = 1 s = 0 SeatID=1 while n <= 100*SeatID: s += n n += 2 print '奇数的和: ...
- DFA最小化实例
原始DFA如下图所示 最小化的定义:1.没有多余的状态(死状态):2.没有两个状态是相互等价的: 两个状态等价的含义:1.兼容性(一致性)——同是终态或同是非终态:2.传播性(蔓延性)——从s出发读入 ...
- unigui的菜单树补习【2】treeview
Treeview用于显示按照树形结构进行组织的数据. Treeview控件中一个树形图由节点(TreeNode)和连接线组成.TtreeNode是TTreeview的基本组成单元. ...
- CodeForces - 284C - Cows and Sequence
先上题目: C. Cows and Sequence time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- sencha touch 2中判断游览器是否包含webkit的方法
<script type="text/javascript"> if (!Ext.browser.is.WebKit) { alert("The curren ...
- SRPING MVC基本配置
作下记录,WEB.XML的配置及DispatcherServlet-context.xml的配置. 而后者的配置,有不同的形式,不要被迷惑. WEB.XML <servlet> <s ...
- N天学习一个linux命令之yum
yum命令 用途 yum(Yellowdog Updater Modified),RedHat系Linux操作系统包管理器,基于rpm,从源远程仓库下载rpm包安装,同时解决依赖关系,使用Python ...
- Qt移动应用开发(一):适配不同的屏幕
Qt移动应用开发(一):适配不同的屏幕 到眼下为止.Qt5.3已经出现非常长一段时间了.而且已经有一些应用使用Qt进行构建了.我自己也完毕了第一款使用Qt构建的手机游戏<吃药了>.那么接下 ...
- JAVA模拟登录实例
近期在做公司一个web项目.要求在我们的系统上,可以显示其它站点上的数据. 刚開始接到这个任务时,还在想.简单的非常.直接用UrlConection直接进入该网页,然后获取该网页的html,取到想要的 ...
- QMessageBox 的四种用法
void MainWindow::on_info_clicked() { //info QMessageBox::information(this, "Title", " ...