java分页详解_javaweb分页原理详解

本文详细介绍了JavaWeb分页的实现,包括Servlet、Service和DAO层的代码实现,利用QueryRunner处理数据库查询,计算总页数并展示指定页码的产品。通过参数传递当前页和类别,实现动态分页效果。
摘要由CSDN通过智能技术生成

本文实例为大家分享了javaweb分页原理的具体实现代码,供大家参考,具体内容如下

public class page {

private int currentpage;

private int totalpage;

private int count;

private int pagesize;

private list list;

private string category;

}

servlet:

package com.learning.web.servlet;

import java.io.ioexception;

import java.util.list;

import javax.enterprise.inject.new;

import javax.servlet.servletexception;

import javax.servlet.annotation.webservlet;

import javax.servlet.http.httpservlet;

import javax.servlet.http.httpservletrequest;

import javax.servlet.http.httpservletresponse;

import com.learning.domain.page;

import com.learning.domain.product;

import com.learning.service.productservice;

@webservlet("/showproductbypage")

public class showproductbypage extends httpservlet {

private static final long serialversionuid = 1l;

protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

int currentpage=1;

int pagesize=4;

//第一次取为空

string currentpagestring=request.getparameter("currentpage");

if (currentpagestring!=null) {

currentpage=integer.parseint(currentpagestring);

}

string category = request.getparameter("category");

if ("".equals(category)) {

category=null;

}

productservice productservice=new productservice();

page page=productservice.showproductbypage(currentpage,pagesize,category);

request.setattribute("page", page);

request.getrequestdispatcher("/product_list.jsp").forward(request, response);

}

protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

doget(request, response);

}

}

service:

public page showproductbypage(int currentpage, int pagesize, string category) {

try {

page page=new page();

int count=productdao.count(category);

page.setcount(count);

page.setlist(productdao.findproductsbypage(currentpage,pagesize,category));

int totalpage=(int) math.ceil(1.0*count/pagesize);

page.setpagesize(pagesize);

page.setcurrentpage(currentpage);

page.settotalpage(totalpage);

page.setcategory(category);

return page;

} catch (sqlexception e) {

e.printstacktrace();

}

return null;

}

dao:

public int count(string category) throws sqlexception {

queryrunner queryrunner=new queryrunner(c3p0util.getdatasource());

string sql=" select count(*) from products ";

if (category!=null) {

sql+=" where category='"+category+"'";

}

long l= (long)queryrunner.query(sql, new scalarhandler(1));

return (int) l;

}

public list findproductsbypage(int currentpage, int pagesize, string category) throws sqlexception {

queryrunner queryrunner=new queryrunner(c3p0util.getdatasource());

string sql=" select * from products ";

if (category!=null) {

sql+=" where category='"+category+"'";

}

sql+=" limit ?,?";

return queryrunner.query(sql, new beanlisthandler(product.class),(currentpage-1)*pagesize,pagesize);

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值