使用cookies查询商品详情

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

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

第二步:连接Oracle数据库。

第三步:使用三层架构。

效果图如下:

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

使用cookies查询商品详情_第1张图片

 

商品详情页:

使用cookies查询商品详情_第2张图片

核心代码如下:

 <%
    //创建商品业务逻辑对象
    productBiz prodctbiz = new productBizImpl();

List productlist = prodctbiz.findproductList();
 request.setAttribute("productlist",product);
%>
//EL表达式
核心架包
<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
//EL表达式:
  • class="ck">
    class="title">${news.ep_name}
    class="price">¥${news.ep_price}.00
  • 第二步:在Servlet创建addcookie.java页面,获取商品id:(注意:必须在web.xml写入)

            
        
        addcookie
        class>Servlet.addcookieclass>
        
        
      
      
          addcookie
          /addcookie
      

     

    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. 
    */ 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 product(String id) {
        List listproduct=new ArrayList();
        // 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 list =productbiz.product(a); request.setAttribute("list",list); %>
    class="main">

    <%=shop.getEp_name() %>

    class="infos">
    class="thumb">
    class="buy">

    商品描述:class="price">${product.ep_description}

    商城价:class="price">¥${product.ep_price}.00

    if test="${product.ep_stock==null}">

    class="w1 c">缺货

    if> if test="${product.ep_stock!=null}">

    class="w1 c">有货

    if> if test="${name==null}"> if>

     

    转载于:https://www.cnblogs.com/wlx520/p/4578836.html

    你可能感兴趣的:(java,数据库,javascript)