易买网项目完工,把一些新知识记录下来,以便以后查阅,也方便他人借阅。介绍使用cookies查询商品详情。
第一步:建立商品实体类。
第二步:连接Oracle数据库。
第三步:使用三层架构。
效果图如下:
当我看中新疆牛肉干,商品点击时,进入查看商品详情页。
商品详情页:
核心代码如下:
//创建商品业务逻辑对象
productBiz prodctbiz = newproductBizImpl();
List productlist =prodctbiz.findproductList();
request.setAttribute("productlist",product);%>
//EL表达式
核心架包
//EL表达式:
- ${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);%>
商品描述:${product.ep_description}
商城价:¥${product.ep_price}.00
缺货
有货
alert("你未登入,请去登入吧!");return false;
}