servlet+mysql分页(上一页,下一页)

来源:互联网 发布:lua软件 编辑:程序博客网 时间:2024/04/29 05:17

index.jsp

<body>  <a href="FindAll">查看所有学生</a>    </body>

FindAll.java

package com.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.dao.StudentDao;import com.daoImpl.StudentDaoImpl;import com.entity.Student;public class FindAll extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {StudentDao sd=new StudentDaoImpl();String str=request.getParameter("n");//总行数int lineCount=sd.countStudent();//每页的行数int pageSize=7;//总共的页数int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);int n = 1;if(str == null || str.equals("")){n = 1;}else if(str.equals("a")){ n = (Integer)request.getSession().getAttribute("pageNow");n--;}else if(str.equals("b")){ n = (Integer)request.getSession().getAttribute("pageNow");n++;}else if(str.equals("1")){n=1;}else{n=pageCount;}if(n>pageCount){n=pageCount;}if(n<1){n=1;}request.getSession().setAttribute("pageNow", n);request.getSession().setAttribute("pageCount", pageCount);ArrayList<Student> list=sd.getAllStudent(n,pageSize);request.getSession().setAttribute("list", list);response.sendRedirect("showAllStudent.jsp");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}}


showAllStudent.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="com.entity.Student"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>显示所有学生</title>      </head>    <body>   <center>   <table border=1>   <tr>   <th>学号</th><th>姓名</th><th>性别</th><th>年龄</th><th>班级</th><th>DID</th><th>生日</th>   <th>成绩</th><th>英文名</th><th>电话</th>   <th colspan="2">操作</th>   </tr>   <%   ArrayList<Student> list=(ArrayList<Student>)session.getAttribute("list");   for(Student s:list){    %>       <tr>   <td><%=s.getSid()%></td>   <td><%=s.getSname()%></td>   <td><%=s.getSsex()%></td>   <td><%=s.getSage()%></td>   <td><%=s.getSclass()%></td>   <td><%=s.getDid()%></td>   <td><%=s.getSbir()%></td>   <td><%=s.getSscore()%></td>   <td><%=s.getSename()%></td>   <td><%=s.getSphone()%></td>   <td><a href="update?id=<%=s.getSid()%>"">编辑<a/></td>   <td><a href="delete?id=<%=s.getSid()%>">删除<a/></td>   </tr>   <%   }    %>      </table>      <br/><br/><br/><br/>   <a href="FindAll?n=1">首页|</a>   <a href="FindAll?n=a">上一页|</a>   <a href="FindAll?n=b">下一页|</a>   <a href="FindAll?n=<%=session.getAttribute("pageCount")%>">尾页</a>      </center>     </body></html>



0 2