JavaBean+Servlet+jsp真分页实现

JavaBean

packageBean;
//物品类
publicclassGoods
{
privateintgoodsid;//物品编号
publicStringgoodsname;//物品名称
privatefloatprice;//物品单件

publicvoidsetGoodsid(intgoodsid)
{
this.goodsid=goodsid;
}
publicintgetGoodsid()
{
returngoodsid;
}
//物品名称
publicvoidsetGoodsname(Stringgoodsname)
{
this.goodsname=goodsname;
}
publicStringgetGoodsname()
{
returngoodsname;
}
//物品价格
publicvoidsetPrice(floatprice)
{
this.price=price;
}
publicfloatgetPrice()
{
returnprice;
}
}

单例类

packageutil;

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;

publicclassDbConnection{
privatestaticConnectionconn=null;
privateStringdriver="com.mysql.jdbc.Driver";
privateStringurl="jdbc:mysql://localhost:3306/mydata";
privateStringuser="root";
privateStringpassword="1234567";

privateDbConnection(){
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,user,password);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(SQLExceptione){
e.printStackTrace();
}
}

publicstaticConnectiongetConnection(){
if(conn==null){
newDbConnection();
}
returnconn;
}
}

数据库访问实现

packageDAO;

importjava.sql.Connection;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ArrayList;

importBean.Goods;

importutil.DbConnection;

publicclassDao{
privatestaticConnectionconn;
privatestaticResultSetrs;
privatestaticStatementstmt;
privatestaticintpagesize=5;
static{
conn=DbConnection.getConnection();
}

//通用的查询方法
publicstaticResultSetExecuteQuery(Stringsql){
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLExceptione){
e.printStackTrace();
}
returnrs;
}

//分页逻辑
publicstaticArrayList<Goods>getGoodsList(intcurrentpageno){
ArrayList<Goods>GoodsList=newArrayList<Goods>();
intBeginRecord=(currentpageno-1)*pagesize;
intEndRecord=currentpageno*pagesize;
rs=ExecuteQuery("select*fromgoodslimit"+BeginRecord+","+EndRecord);
try{
while(rs.next()){
Goodsgoods=newGoods();
goods.setGoodsid(rs.getInt(1));
goods.setGoodsname(rs.getString(2));
goods.setPrice(rs.getFloat(3));
GoodsList.add(goods);
}
}catch(SQLExceptione){
e.printStackTrace();
}
returnGoodsList;
}

//统计记录数
publicstaticintgetPageCount(){
inttotal=0;
intPageCount=0;
rs=ExecuteQuery("selectcount(*)fromgoods");
try{
if(rs.next()){
total=rs.getInt(1);
PageCount=(total-1)/pagesize+1;
}
}catch(SQLExceptione){
e.printStackTrace();
}
returnPageCount;
}
}

servlet控制器

packageservlet;

importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.ArrayList;

importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;

importBean.Goods;
importDAO.Dao;

publicclassGetLimiteGoodsextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html");
Stringpageno=request.getParameter("currentpageno");
intcurrentpageno=1;
if(pageno!=null){
currentpageno=Integer.parseInt(pageno);
}
ArrayList<Goods>GoodsList=Dao.getGoodsList(currentpageno);
request.setAttribute("GoodsList",GoodsList);
request.setAttribute("currentpageno",currentpageno);
request.setAttribute("PageCount",Dao.getPageCount());
request.getRequestDispatcher("/LimiteGoods.jsp").forward(request,response);
}

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{

response.setContentType("text/html");
PrintWriterout=response.getWriter();
out
.println("<!DOCTYPEHTMLPUBLIC\"-//W3C//DTDHTML4.01Transitional//EN\">");
out.println("<HTML>");
out.println("<HEAD><TITLE>AServlet</TITLE></HEAD>");
out.println("<BODY>");
out.print("Thisis");
out.print(this.getClass());
out.println(",usingthePOSTmethod");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}

xml配置:

<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>ThisisthedescriptionofmyJ2EEcomponent</description>
<display-name>ThisisthedisplaynameofmyJ2EEcomponent</display-name>
<servlet-name>GetLimiteGoods</servlet-name>
<servlet-class>servlet.GetLimiteGoods</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>GetLimiteGoods</servlet-name>
<url-pattern>/servlet/GetLimiteGoods</url-pattern>
</servlet-mapping>
</web-app>


表现层jsp

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>Inserttitlehere</title>
</head>
<body>
<center>
<c:iftest="${currentpageno>=1}">
<ahref="GetLimiteGoods?currentpageno=1">首页</a>
<ahref="GetLimiteGoods?currentpageno=${currentpageno-1}">上一页</a>
</c:if>
<c:iftest="${currentpageno==1}">
<ahref="GetLimiteGoods?currentpageno=${currentpageno+1}">下一页</a>
<ahref="GetLimiteGoods?currentpageno=${PageCount}">尾页</a>
</c:if>
<tablewidth="80%"border="1"height="56">
<tralign="center">
<td>
商品编号
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
</tr>
<c:forEachvar="goods"items="${GoodsList}">
<tralign="center">
<td>
${goods.goodsid}
</td>
<td>
${goods.goodsname}
</td>
<td>
${goods.price}
</td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值