【JSP篇】——cookie之商品浏览记录的实现:4.显示商品的详细信息

                                                       学习上一节:3.显示当前所有的商品效果与功能的实现

                                                学习下一节:5.cookie实现前五条浏览记录

1.功能介绍

   前面我们实现了商品所有信息的显示,接下来要实现某一商品详细信息的显示。那么我们就要思考了,我们如何通过页面跳转将我们对应的商品传递到下一个页面。其实在前面写index.jsp主页面的时候,用户点击对应的商品会跳转到details.jsp这个页面,我们可以在这里的<a>标签中将我们商品的id传进去:

<a href="details.jsp?id=<%=good.getId() %>" class="prod_details">详情</a>  

    这样我们就可以显示该条商品的详细信息了。

    ok,商品的id有了,接下来我们就是根据id从数据库中获取该商品的信息。

2.获取单条数据

   和前面一样,对数据库的操作,我们使用GoodDao这个类实现,接下来是获取单条记录的详细实现:

//根据商品的id获取数据
	public Good getGoodById(int id){
		Connection conn=null;
		PreparedStatement prep=null;
		ResultSet rs=null;
		Good good=new Good();
		
		try {
			conn=DBHelper.getConnection();
			String sql="select * from good where id=?";
			prep=(PreparedStatement) conn.prepareStatement(sql);
			prep.setInt(1, id);
			rs=prep.executeQuery();
			
			if(rs.next())
			{
				good.setId(rs.getInt("id"));
				good.setName(rs.getString("name"));
				good.setPrice(rs.getDouble("price"));
				good.setInfor(rs.getString("infor"));
				good.setImage(rs.getString("image"));
			}
			return good;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

3.详细页面的实现

    ok,这样我们就可以通过这个方法获取一条记录,接下来我们要做的就是将数据显示出来。仍然和前面一样,我们的details.jsp页面很多,现贴出关键代码:

 <!-- 找到你啦,我们要用的东西在这里 -->
   <div class="center_content">
   <%
   		//根据地址里面的id参数,获取到数据库中的商品
   		GoodDao dao=new GoodDao();
   		Good good=new Good();
   		good=dao.getGoodById(Integer.parseInt(request.getParameter("id")));
   %>
   		<!-- 商品详细信息处 -->
   	 	<div class="center_title_bar"><%=good.getName() %></div>
    
    	<div class="prod_box_big">
        	<div class="top_prod_box_big"></div>
            <div class="center_prod_box_big">            
                 
                 <div class="product_img_big">
                 <a href="javascript:popImage('images/big_pic.jpg','Some Title')" title="header=[Zoom] body=[ ] fade=[on]"><img src="images/<%=good.getImage() %>" alt="" title="" border="0" /></a>
                 
                 </div>
                     <div class="details_big_box">
                         <div class="product_title_big"><%=good.getName() %></div>
                         <div class="specifications">
                          	  介绍: <span class="blue"><%=good.getInfor() %></span><br />
                         
                         </div>
                         <div class="prod_price_big"><span class="price">价格:<%=good.getPrice() %></span></div>
                         
                         <a href="#" class="addtocart">加入购物车</a>
                     </div>                        
            </div>
            <div class="bottom_prod_box_big"></div>                                
        </div>
 
 </div>

4.页面效果

    好啦,功能和页面都写好了,我们来看一看点击商品“大哥大”的效果:


这里1处是我们商品的详细信息,通过2处我们可以发现,页面跳转的时候传过来的id=5,<a>传递参数是get方式传递的。

好,这两个页面的功能都实现了,接下来要进入到本工程我们关键的地方了:使用cookie显示最近浏览商品的前5条信息。

                                                       学习上一节:3.显示当前所有的商品效果与功能的实现

                                                学习下一节:5.cookie实现前五条浏览记录


  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现商品浏览记录功能,可以使用JSP中的session对象来存储浏览过的商品信息。 首先,在商品详情页中,可以通过JSP代码获取到当前浏览商品信息,并将其存储到session中。示例代码如下: ``` <% String productId = request.getParameter("productId"); // 获取当前浏览商品ID String productName = "商品名称"; // 根据商品ID查询商品名称等信息 String productPrice = "商品价格"; String productImage = "商品图片路径"; // 将商品信息存储到session中 HttpSession session = request.getSession(); List<String[]> historyList = (List<String[]>) session.getAttribute("historyList"); if (historyList == null) { historyList = new ArrayList<String[]>(); } String[] productInfo = {productId, productName, productPrice, productImage}; historyList.add(0, productInfo); if (historyList.size() > 5) { historyList.remove(5); } session.setAttribute("historyList", historyList); %> ``` 上述代码中,首先获取到当前浏览商品ID,然后查询该商品的名称、价格、图片路径等信息。接着,从session中获取商品浏览记录列表,如果列表不存在则创建一个新的列表。将当前浏览商品信息添加到列表的最前面,如果列表超过了5个则删除最后一个。最后,将更新后的列表重新存储到session中。 在商品浏览记录页面中,可以通过JSP代码获取到session中存储的商品浏览记录列表,并将其展示出来。示例代码如下: ``` <% HttpSession session = request.getSession(); List<String[]> historyList = (List<String[]>) session.getAttribute("historyList"); %> <!DOCTYPE html> <html> <head> <title>商品浏览记录</title> </head> <body> <h1>商品浏览记录</h1> <ul> <% for (String[] productInfo : historyList) { %> <li> <a href="productDetail.jsp?productId=<%=productInfo[0]%>"> <img src="<%=productInfo[3]%>" alt="<%=productInfo[1]%>"> <span><%=productInfo[1]%></span> </a> </li> <% } %> </ul> </body> </html> ``` 上述代码中,首先获取到session中存储的商品浏览记录列表。然后,使用JSP循环语句遍历列表中的每个商品信息,并将其展示出来。 以上就是实现商品浏览记录功能的JSP代码示例。需要注意的是,为了保证用户隐私安全,可能需要对商品浏览记录进行加密处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值