Use cookies to query product details, and use cookies to query product details

Source: Internet
Author: User

Use cookies to query product details, and use cookies to query product details

The yimei.com project is completed, and some new knowledge is recorded for future reference and borrowing by others. Describes how to use cookies to query product details.

Step 1: Create a commodity entity class.

Step 2: connect to the Oracle database.

Step 3: Use a three-tier architecture.

As follows:

When I click products in Xinjiang beef jerky, I can view the product details page.

 

Product details page:

The core code is as follows:

<% // Create the product business logic object productBiz prodctbiz = new productBizImpl (); List <easybuy_product> productlist = prodctbiz. findproductList (); request. setAttribute ("productlist", product); %>
// EL expression
Core shelf package
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
// EL expression: <c: forEach var = "news" items = "$ {requestScope. productlist} "> <li class =" ck "> <dl> <dt> <a href =" addcookie? Id =$ {news. ep_id} "> </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>

Step 2: On the Servlet addcookie. java page, obtain the product id: (Note: The product id must be written in web. xml)

<! -- The product id contains cookies --> <servlet-name> addcookie </servlet-name> <servlet-class> Servlet. addcookie </servlet-class> </servlet> <! -- Map 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, request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setCo NtentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); request. setCharacterEncoding ("UTF-8"); // obtain the product id String id = request. getParameter ("id"); // The forwarded page response. setHeader ("refresh", "0; url =/yimaiWang/product-view.jsp? Id = "+ id); Cookie [] cookies = request. getCookies (); String visitlist = null; if (cookies! = Null) {for (Cookie cookie: cookie) {if (Cookie. getName (). equals ("visitlist") {visitlist = cookie. getValue (); break ;}}if (visitlist = null) {Cookie cookie = new Cookie ("visitlist", id); cookie. setMaxAge (1, 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 (1, 180); response. addCookie (cookie) ;}} else {Cookie cookie = new Cookie ("visitlist", id); cookie. setMaxAge (1, 180); response. addCookie (cookie );}}}

Step 3: Jump to the product details page product-view.jsp (these two query statements are different, one is to query the product id, one is the product List set)

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;}

 

<%
// Obtain the product id
Int id = Integer. parseInt (request. getParameter ("id "));
ProductBiz bizvoid = new productBizImpl ();
Easybuy_product shop = bizvoid. findProductForid (id );
Request. setAttribute ("shop", shop );
%>


<% // Obtain the item idrequest. setCharacterEncoding ("UTF-8"); String a = request. getParameter ("id"); %> <% // create the product information business logic object 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} ">

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.