星空网 > 软件开发 > Java

使用cookies查询商品详情

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

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

第二步:连接Oracle数据库

第三步:使用三层架构

效果图如下:

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

使用cookies查询商品详情

 

商品详情页:

使用cookies查询商品详情

核心代码如下:

 <%  //创建商品业务逻辑对象  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='/images/loading.gif' data-original="${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.

    <!--商品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);
%>


<% //获取商品idrequest.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='/images/loading.gif' data-original="${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查询商品详情

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

龙吉国际供应链:https://www.ikjzd.com/w/1573202301670563841
长帆国际:https://www.ikjzd.com/w/1573202302128087042
龙岩综试区:https://www.ikjzd.com/w/1573202311938220034
LongYuan:https://www.ikjzd.com/w/1573202312609308673
出海通:https://www.ikjzd.com/w/1573202320372965377
爱竞(LoveAd):https://www.ikjzd.com/w/1573202327016742914
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流