MySQL8cook_使用cookies查询商品详情

这篇博客介绍了如何在易买网项目中利用Cookie查询商品详情。通过创建商品实体类,连接Oracle数据库,使用三层架构,实现了从商品点击到详情页的跳转。在Servlet中创建addcookie.java页面获取商品ID,并在响应中设置Cookie,同时在商品详情页product-view.jsp展示商品信息。
摘要由CSDN通过智能技术生成

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

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

第二步:连接Oracle数据库。

第三步:使用三层架构。

效果图如下:

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

20e81e05eb9844e14d0b3b166d6af59f.png

商品详情页:

b18fe7a3f3b37512444ae08c9954bb77.png

核心代码如下:

//创建商品业务逻辑对象

productBiz prodctbiz = newproductBizImpl();

List productlist =prodctbiz.findproductList();

request.setAttribute("productlist",product);%>

//EL表达式

核心架包

//EL表达式:

%24%7Bnews.ep_file_name%7D
${news.ep_name}
¥${news.ep_price}.00

第二步:在Servlet创建addcookie.java页面,获取商品id:(注意:必须在web.xml写入)

addcookie

Servlet.addcookie

addcookie

/addcookie

packageServlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;public class addcookie extendsHttpServlet {/*** Constructor of the object.*/

publicaddcookie() {super();

}/*** Destruction of the servlet.
*/

public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

doPost(request, response);

}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, 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(intid) {

con=this.getConnection();int i =id;

String sql= "select * from easybuy_product where ep_id =?";

easybuy_product pd= neweasybuy_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);

}returnpd;

}

}

public Listproduct(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= neweasybuy_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);

}returnlistproduct;

}

//获取商品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 = newproductBizImpl();

List list =productbiz.product(a);

request.setAttribute("list",list);%>

%24%7Bproduct.ep_file_name%7D

商品描述:${product.ep_description}

商城价:¥${product.ep_price}.00

缺货

有货

alert("你未登入,请去登入吧!");return false;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值