网页的分页功能的实现比较简单,实现方法也多种多样。
今天总结一个简单的jsp真分页实例。
首先,提到分页就要先明确一个概念,何为真分页何谓假分页。
假分页:一次性从数据库读出表的所有数据一次性的返回给客户端,由js来控制每一页的显示。
真分页:由程序控制,每一次只返回一页大小的数据,显示到客户端。
由此可以很清楚的分辨出真假分页各自的优缺点:
假分页:由于一次性读出所有数据并返回给客户端,如果数据量庞大,所以这一次的动作可能是非常消耗服务器资源和带宽的,
但是返回给客户端以后就非常轻松了,客户在一段时间内不会再像服务器端请求资源。但不代表可能出现一些意外情况,
比如说客户将浏览器关闭,重新访问网站等。所以,如果数据量相当庞大,不建议使用用真分页。
真分页:假分页每次只取需要的数据返回给客户端,比起真分页没有那么大的数据库压力。但也因为这个工作特性,所以假分页
的方法需要频繁和服务器端进行交互。既然频繁交互,自然也会给服务器带来负担。
综上:如果数据量较小,使用假分页的效果会更优,如果数据量庞大,使用真分页的效果更优。
分析完特性,下面就来列举一个简单的真分页实例。
真分页是通过程序来控制的,每次向数据库请求需要的数据。
简述实现思路业务流程:
首先:客户端带着page参数请求客户端,若没有带page参数,说明是第一次访问,则page参数默认为0;
其次:服务端根据page参数,调用相关函数,从数据库中取出表中数据,封装成相关对象,返回给客户端,并且返回新page参数及总页数;
最后:再客户端显示请求的相关数据,并根据page参数及总页数两个参数,决定上一页下一页的按钮是否可用。
数据库操作类:
public class dbbean {
private connection con;
private preparedstatement pstmt;
private resultset rs;
private string dbname ="test";
private string dbuser = "root";
private string dbpass ="******";
static{
try{
class.forname("com.mysql.jdbc.driver");
}catch(classnotfoundexception e){
system.out.println(e);
}
}
public void prepareconnection(){
try{
con=drivermanager.getconnection("jdbc:mysql://localhost:3306/"+dbname,dbuser,dbpass);
}catch(sqlexception e){
system.out.println(e);
}
}
//关闭连接
public void close(){
try {
if(con!=null)
con.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
con = null;
try {
if(pstmt!=null)
pstmt.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
pstmt = null;
}
//设置参数
private void setparems(string[] parems){
if(parems!=null){
for(int i=0;i<parems.length;i++){
try {
pstmt.setstring(i+1, parems[i]);
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
}
}
public resultset executequery(string sql,string[] parems){
resultset res = null;
prepareconnection();
try {
pstmt = con.preparestatement(sql);
setparems(parems);
res = pstmt.executequery();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}finally{
}
return res;
}
}
学生类:
public class studentbean {
private long id;
private string name;
private string phone;
private int age;
private int score;
public long getid() {
return id;
}
public void setid(long id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public string getphone() {
return phone;
}
public void setphone(string phone) {
this.phone = phone;
}
public int getage() {
return age;
}
public void setage(int age) {
this.age = age;
}
public int getscore() {
return score;
}
public void setscore(int score) {
this.score = score;
}
}
学生数据操作类
public class studentdao implements studentdaoin {
@override
public arraylist<studentbean> findbypage(int page){
dbbean db = new dbbean();
int begin = (page-1) * 5;
string sql = "select * from t_student limit "+begin+",5";
resultset rs = db.executequery(sql,null);
arraylist<studentbean> list = new arraylist<studentbean>();
try {
while(rs.next()){
studentbean st = new studentbean();
st.setname(rs.getstring("name"));
st.setage(rs.getint("age"));
st.setid(rs.getint("id"));
st.setphone(rs.getstring("phnoe"));
st.setscore(rs.getint("score"));
list.add(st);
}
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return list;
}
@override
public int usercount(){
dbbean db = new dbbean();
string sql = "select count(*) from t_student";
resultset rs = db.executequery(sql, null);
int count = 0;
try {
rs.next();
count = rs.getint(1);
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return count;
}
}
相关业务逻辑
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
// todo auto-generated method stub
string page = null;
page = request.getparameter("page");
if(page == null || page=="")
page = "1";
studentdao studao = new studentdao();
request.setattribute("student",studao.findbypage(integer.parseint(page)));
request.setattribute("pagenum",studao.usercount()/5+1);//总页数
request.setattribute("page", page);//当前页
request.getrequestdispatcher("student.jsp").forward(request, response);
}
前台jsp代码:
<table id="t_stu" border="1" cellpadding="2" cellspacing="0">
<thead>
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>电话</th>
<th>成绩</th>
</tr>
</thead>
<c:foreach items="${student}" var="st">
<tr>
<td>${st.getid()}</td>
<td>${st.getname()}</td>
<td>${st.getage()}</td>
<td>${st.getphone()}</td>
<td>${st.getscore()}</td>
</tr>
</c:foreach>
</table>
<br>
共 ${pagenum}页 当前 第${page}页
<c:choose>
<c:when test="${page>1}">
<a href="getsutent?page=${page-1}" rel="external nofollow" ><input type="button" value="上一页" ></a>
</c:when>
<c:otherwise>
<input type="button" value="上一页" disabled="disabled" />
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page!=pagenum}">
<a href="getsutent?page=${page+1}" rel="external nofollow" ><input type="button" value="下一页"></a>
</c:when>
<c:otherwise>
<input type="button" value="下一页" disabled="disabled" />
</c:otherwise>
</c:choose>
本例是真分页的一个简单实现,有着明显的缺点。
例如:
1.在后台相关业务逻辑处,只对page做了简单的判断,因为查询相关page时,参数是写入前台a标签中的,所以懂技术的用户,可以随意改动其值
由此查询数据库可能带来意想不到的错误。
2.功能不够完善,仅提供了上一页下一页按钮的简单功能。
另外:实现假分页时可以结合ajax和json。以此可实现无刷新翻页,看起来功能和真分页一样。。。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!