首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

小说分页技术的实现

2012-07-16 
小谈分页技术的实现//得到pageCountpublic int getCount(){int pageCount0ct conn.getConnection()tr

小谈分页技术的实现

//得到pageCountpublic 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;}

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();//计算pageCountif(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();//计算pageCountif(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>
?

热点排行