📄 product.jsp
字号:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="com.qyg.shop.product.*" %>
<%@ page import="com.qyg.shop.sales.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'newestproducts.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="css/common.css" type="text/css"></link>
</head>
<body>
<body bgcolor="D9EAFC">
<p align=center>
<%
out.print("以下是得到的商品列表:<br>");
String action = request.getParameter("action");
int currentPage=1;//当前页数
int pageSize=5;//在此设定每页的大小
int pageCount=0;//页面总数
ProductDAO dao = new ProductManageDAO();
ArrayList<Product> accounts = null;
Iterator<Product> it=null;
//关键字查询结果
if(action!=null&&action.equals("key")){
String orderby=(String)session.getAttribute("orderby");
String keyword=(String)session.getAttribute("keyword");
dao=new ProductManageDAO();
System.out.println(keyword);
accounts=dao.getProdutsByName(keyword,orderby);
it=accounts.iterator();
}
//最新商品列表
else if(action!=null&&action.equals("newest")){
dao=new ProductManageDAO();
ArrayList<Product> all = dao.getAllProducts("product_id",false);
int k=10;
for (int i = 0; i < all.size(); i++) {
k=all.get(i).getProduct_id();
}
if(k>9){
accounts=dao.idBetween(k-9, k,"product_id",true);
}
else{
accounts=dao.idBetween(1,k,"product_id",true);
}
it=accounts.iterator();
}
//ID号查询结果列表
else if(action!=null&&action.equals("id")){
String ID=request.getParameter("ID");
int product_id=Integer.parseInt(ID);
dao=new ProductManageDAO();
accounts = dao.idBetween(product_id,product_id,"product_id",false);
it=accounts.iterator();
}
//相机列表
else if(action!=null&&action.equals("camera")){
dao=new ProductManageDAO();
accounts=dao.getProdutsByCid(10);;
it=accounts.iterator();
}
//价格范围查询列表
else if(action!=null&&action.equals("price")){
String low=(String)session.getAttribute("lowprice");
String top=(String)session.getAttribute("topprice");
String orderby=(String)session.getAttribute("orderby");
double lowprice=Double.parseDouble(low);
double topprice=Double.parseDouble(top);
dao=new ProductManageDAO();
accounts = dao.priceBetween(lowprice, topprice,orderby,false);
it=accounts.iterator();
}
//畅销产品列表
else if(action!=null&&action.equals("briskly")){
dao=new ProductManageDAO();
Sales2ManageDAO sdao = new Sales2ManageDAO();
//根据以往消费记录获取最新商品信息,在此取5条畅销产品
accounts = sdao.getProductsBySals();
it=accounts.iterator();
}
//促销产品列表
else if(action!=null&&action.equals("promotion")){
dao=new ProductManageDAO();
accounts=dao.getProdutsByCid(2);;
it=accounts.iterator();
}
//商品导航产品列表
else if(action!=null&&action.equals("lead")){
dao=new ProductManageDAO();
accounts=dao.getAllProducts("product_cId",false);
it=accounts.iterator();
}
//品牌手机列表
else if(action!=null&&action.equals("mobile")){
dao=new ProductManageDAO();
accounts=dao.getProdutsByCid(11);;
it=accounts.iterator();
}
%>
<%
int number=accounts.size();//得到商品数量
pageCount=number%pageSize==0?number/pageSize:number/pageSize+1;//得到总页数,另一种算法(number+pageSize-1)/pageSize;
/*得到总结束完*/
//得到URL传来的参数
if(request.getParameter("page")!=null) currentPage=Integer.parseInt(request.getParameter("page"));
currentPage=currentPage<1?1:currentPage;
currentPage=currentPage>pageCount?pageCount:currentPage;
%>
<%if(currentPage>1){ %>
<a href="product.jsp?page=1&action=<%=action %>">首 页</a>
<a href="product.jsp?page=<%=currentPage-1 %>&action=<%=action %>">上一页</a>
<%} %>
<%if(currentPage<pageCount){ %>
<a href="product.jsp?page=<%=currentPage+1 %>&action=<%=action %>">下一页</a>
<a href="product.jsp?page=<%=pageCount %>&action=<%=action %>">末 页</a>
<%} %>
第<font color="red"><%=currentPage %></font>页/共<font color="red"><%=pageCount %></font>页
<table width="90%" border="1" class="datalist">
<tbody>
<tr>
<th scope="col">产品id</th>
<th scope="col">产品名称</th>
<th scope="col">产品价格</th>
<th scope="col">产品描述</th>
<th scope="col">产品图像</th>
<th scope="col">操作</th>
</tr>
<%
if(!it.hasNext()){
out.print("没有找到您想要的产品");
}
else{
for(int i=0;i<pageSize*(currentPage-1);i++){
it.next();
}
for(int i=0;i<pageSize&&it.hasNext();i++){
Product p = it.next();
String buyActionStr = "<a href='servlet/ActionServlet?action=buy&productid="
+ p.getProduct_id() + "'>购买</a>";
out.print("<tr>");
out.print("<td>" + p.getProduct_id() + "</td>");
out.print("<td>" + p.getProduct_name() + "</td>");
out.print("<td>" + p.getProduct_price() + "</td>");
out.print("<td>" + p.getProduct_descr() + "</td>");
// out.print("<td>" + p.getProduct_cId() + "</td>");
out.print("<td width='100' height='75'> <image src='img/products/" + p.getProduct_image() + "' width='100' height='75'></td>");
out.print("<td>" + buyActionStr + "</td>");
out.print("</tr>");
}
}
%>
</tbody>
</table>
</body>
<script language="javascript">
var rows = document.getElementsByTagName('tr');
for (var i=0;i<rows.length;i++){
rows[i].onmouseover = function(){ //鼠标在行上面的时候
this.className += 'altrow';
}
rows[i].onmouseout = function(){ //鼠标离开时
this.className = this.className.replace('altrow','');
}
}
</script>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -