小谈分页技术的实现

前几天看了有关分页技术的视频,个人觉得分页技术还是挺好实现的。

分页技术是现在大部分网站都需使用的技术,即将信息进行分页显示。

1.先讲讲分页的算法:

      需定义4个变量

    int pageNow------希望显示第几页

    int pageSize------每页显示多少条信息

    int rowCount------共有多少条记录

    int pageCount-------共有几页

说明:pageSize是指定的,pageNow是用户选择的,rowCount是从表中查询到的,pageCount是计算得到的,计算式为:

                if(rowCount%pageSize==0){

                    pageCount=rowCount/pageSize;

                }else{

                    pageCount=rowCountpageSize+1;

                }

2.显示一页的信息的查询方式:

      select * from use3 limit "+(pageNow-1)+","+pageSize+"

3.代码示例:

    封装用户登录验证方法,分页实现方法的UserBeanDao类

//得到pageCount
	public int getCount(){
		int pageCount=0;
		ct = conn.getConnection();
		try {
			ps=ct.prepareStatement("select count(*) from use3");
			rs=ps.executeQuery();
			if(rs.next()){
				pageCount=rs.getInt(1);
				System.out.println("----"+pageCount);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.close();
		return pageCount;
	}
	
	
	public ArrayList getUser(int pageNow,int pageSize){
		ArrayList<User> al = new ArrayList<User>();
		try {
			ct = conn.getConnection();
			ps=ct.prepareStatement("select * from use3 limit "+(pageNow-1)+","+pageSize+""); 
//			//给?赋值
//			ps.setInt(1, pageSize);
//			ps.setInt(2, pageSize*(pageNow-1));
			rs=ps.executeQuery();
			while(rs.next()){
				System.out.println(rs.getString(1));
				User user = new User();
				user.setId(Integer.parseInt(rs.getString(1)));
				user.setUsername(rs.getString(2));
				user.setPwd(rs.getString(3));
				user.setEmail(rs.getString(4));
				user.setGrade(Integer.parseInt(rs.getString(5)));
				al.add(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.close();
		return al;
	}
	

 

    servlet实现类UserManage

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws               ServletException, IOException {

		
		String action = request.getParameter("action");
		//String action = new String(str.getBytes("ISO-8859-1"),"gbk");
		System.out.println(action);
		if("login".equals(action)){
			System.out.println("-----");
			UserBeanDao ub = new UserBeanDao();
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			if(ub.validate(username, pwd)){
				
			//cookie保存用户信息
				String keep = request.getParameter("keep");
				if(keep!=null){
				//将用户名和密码保存到客户端(cookie)
				//创建
				Cookie name = new Cookie("myname",username );
				Cookie userpwd = new Cookie("mypwd", pwd);
				//设置时间
				name.setMaxAge(14*24*3600);
				userpwd.setMaxAge(14*24*3600);
				//回写到客户端
				response.addCookie(name);
				response.addCookie(userpwd);
				}
				
				request.getSession().setAttribute("username", username);
				request.getSession().setAttribute("pwd", pwd);
				rowCount = ub.getCount();
				//计算pageCount
				if(rowCount%pageSize==0){
					pageCount=rowCount/pageSize;
				}else{
					pageCount=rowCount/ pageSize+1;
				}
				request.setAttribute("pageNow", pageNow);
				request.setAttribute("pageCount", pageCount);
				ArrayList<User> al = ub.getUser(pageNow, pageSize);
				request.setAttribute("fenye", al);
				request.getRequestDispatcher("/welcomeUser.jsp").forward(request, response);
			}else{
				request.setAttribute("errorMsg", "账号或密码错误!");
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
		}else if("fenye".equals(action)){
			UserBeanDao ub = new UserBeanDao();
			//request.setAttribute("username", username);
			//request.setAttribute("pwd", pwd);
			pageNow = Integer.parseInt(request.getParameter("pageNow").toString());
			rowCount = ub.getCount();
			//计算pageCount
			if(rowCount%pageSize==0){
				pageCount=rowCount/pageSize;
			}else{
				pageCount=rowCount/ pageSize+1;
			}
			request.setAttribute("pageNow", pageNow);
			request.setAttribute("pageCount", pageCount);
			ArrayList al = ub.getUser(pageNow, pageSize);
			request.setAttribute("fenye", al);
			request.getRequestDispatcher("/welcomeUser.jsp").forward(request, response);
		}
	}

 

    登录后的显示界面welcomeUser.jsp

    <%@ page language="java" import="java.util.*,test.blog.form.*" contentType="text/html; charset=gbk"

    pageEncoding="gbk"%>
    
    <%
    	String username = session.getAttribute("username").toString();
    	String pwd = session.getAttribute("pwd").toString();
    	int pageCount = Integer.parseInt(request.getAttribute("pageCount").toString());
    	int pageNow = Integer.parseInt(request.getAttribute("pageNow").toString());
    	ArrayList al = (ArrayList)request.getAttribute("fenye");
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<b><%=username %></b>
<table border="1px">

<%

for(int i=0;i<al.size();i++){
	User user = (User)al.get(i);
	%>
	<tr>
	<td><%=user.getId() %></td>
	<td><%=user.getUsername() %></td>
	<td><%=user.getPwd() %></td>
	<td><%=user.getEmail() %></td>
	<td><%=user.getGrade() %></td>
	</tr>
	<% 	
}
%>
</table>
<br>
<%
for(int i=1;i<pageCount;i++){
%>
<a href="UserManage?action=fenye&pageNow=<%=i %>"><%=i %> &nbsp;</a>
<%
}%>
</body>
</html>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值