Index.jsp页面:
<%@ page language="java" import="java.util.*,xuyan.model.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
int pageNowok=1;
String sendpagenow=request.getParameter("pagenow");
if(sendpagenow==null)
{
pageNowok=1;
}
else
{
pageNowok= Integer.parseInt(sendpagenow) ;
}
GoodsBeanBO gbb=new GoodsBeanBO();
ArrayList al= gbb.GetPageById(4,pageNowok);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>index.jsp</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" type="text/css" href="styles.css">
-->
</head>
<body>
<table border="2" borderColor="blue" style="width: 260px; height: 200px; border=2; border-color:Blue" bgColor="#ffff80">
<tr>
<%
for(int i=0;i<1;i++)
{
//循环两次 打印出 表格的两行
%>
<%
for(int ii=0;ii<al.size();ii++)
{ GoodsBean gb=(GoodsBean)al.get(ii);
%>
<td>
<td align="left" style="width: 180px">
<a href="GoodsServlet?flag=showdetail&GoodsID=<%=gb.getGoodsID() %>">名称:<%=gb.getGoodsName() %></a>
</td>
<td align="left" style="width:80px">
价格:<%=gb.getGoodsPrice() %>
</td>
</td>
<td>
<td style="width: 180px">
<img src="images/<%=gb.getGoodsImage() %>" />
</td>
<td align="left" style="width: 80px">
评价:<%=gb.getGoodsPingjia() %>
</td>
</td>
<%
}
%>
<%
}
%>
</tr>
</table>
<br/>
<%
GoodsBeanBO gbb2=new GoodsBeanBO();
int a= gbb2.PageCount(4);
%>
共有页数: <%=a %>
当前页数:<%=pageNowok %>
<div style="text-align: center">
<table style="width: 658px; height: 23px" borderColor="blue" border="2">
<tr>
<td style="width: 100px">
</td>
<td style="width: 400px" style="text-align: center">
<%
for(int i=1;i<=a;i++)
{
%>
<a href=GoodsServlet?flag=fenye&pagenow=<%=i%>>【<%=i%>】</a>
<%
}
%>
</td>
<td style="width: 100px">
</td>
</tr>
</table>
</div>
</body>
</html>
ShowDetail.jsp 页面:
<%@ page language="java" import="java.util.*,xuyan.model.*" pageEncoding="gb2312"%>
<%
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>ShowDetail.jsp</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" type="text/css" href="styles.css">
-->
<script type="text/javascript">
//响应点击购买事件 传goodsID
function addgoods(goodsid)
{
window.open("ShopServlet?goodsid="+goodsid,"_self");
// window.alert(goodsid);
}
</script>
</head>
<body>
<%
GoodsBean gb=new GoodsBean();
gb=(GoodsBean)request.getAttribute("Detail");
%>
<form >
<table border="2" style="width: 330px; height: 239px" bgColor="#ffff80">
<tr>
<td style="width: 100px">
<img src="images/<%=gb.getGoodsImage() %>" /></td>
<td align="left" style="width: 100px">
电影名称:<%=gb.getGoodsName() %></td>
</tr>
<tr>
<td align="left" style="width: 100px">
评价:<%=gb.getGoodsPingjia() %></td>
<td align="left" style="width: 100px">
<br />
价格:<%=gb.getGoodsPrice() %><br />
</td>
</tr>
<tr>
<td style="width: 100px">
<input type="button" οnclick="addgoods(<%=gb.getGoodsID() %>)" value=购买>
</td>
<td style="width: 100px">
</td>
</tr>
</table>
</form>
</body>
</html>
GoodsBeanBO.java: (Servlet)
package xuyan.model;
import java.sql.*;
import java.util.ArrayList;
public class GoodsBeanBO {
private Connection con=null;
private Statement sm =null;
private ResultSet rs=null;
public GoodsBean GetGoodsInfo( String id){
GoodsBean gb=new GoodsBean();
con=new ConnDB().GetConn();
try {
sm=con.createStatement();
rs=sm.executeQuery("select * from GoodsInfo where ID in ('"+id+"')");
while(rs.next())
{
gb.setGoodsID(rs.getInt(1));
gb.setGoodsName(rs.getString(2));
gb.setGoodsPrice(rs.getString(3));
gb.setGoodsPingjia(rs.getString(4));
gb.setGoodsImage(rs.getString(5));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
closeConn();
}
return gb;
}
//分页
public ArrayList GetPageById(int pageSize,int pageNow)
{ ArrayList al=new ArrayList();
con=new ConnDB().GetConn();
try {
sm=con.createStatement();
//rs=sm.executeQuery("select top "+pageSize+"* from GoodsInfo where ID not in (select top '"+pageSize*(pageNow-1)+"'* ID from GoodsInfo )");
rs=sm.executeQuery("select top "+pageSize+"* from GoodsInfo where ID not in(select top "+pageSize*(pageNow-1)+" ID from GoodsInfo)");
while(rs.next())
{
GoodsBean gb=new GoodsBean();
gb.setGoodsID(rs.getInt(1));
gb.setGoodsName(rs.getString(2));
gb.setGoodsPrice(rs.getString(3));
gb.setGoodsPingjia(rs.getString(4));
gb.setGoodsImage(rs.getString(5));
al.add(gb);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
closeConn();
}
return al;
}
//利用GoodsID查询具体购买数量
//计算共有多少页
public int PageCount(int pageSize)
{
int yeshu=0;
con=new ConnDB().GetConn();
try {
sm=con.createStatement();
rs=sm.executeQuery("select count(*) from GoodsInfo");
if(rs.next())
{
yeshu=rs.getInt(1);
if(yeshu%pageSize==0)
{
yeshu=yeshu/pageSize;
}
else
{
yeshu=yeshu/pageSize+1;
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
closeConn();
}
return yeshu;
}
//关闭资源
public void closeConn(){
try {
if(rs!=null)
{
rs.close();
rs=null;
}
if(sm!=null)
{
sm.close();
sm=null;
}
if(con!=null)
{
con.close();
con=null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}