效果如下: jsp %@page import= "com.cyh.test.Student" % %@ page language= "java" contentType= "text/html; charset=gb2312" % %@ page language= "java" import= "java.util.*" % title / title / head html % ListStudent list = (ArrayListStudent)
效果如下:
jsp
<%@page import="com.cyh.test.Student"%>
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<%@ page language="java" import="java.util.*" %>
<title></title>
</head>
<html>
<% List<Student> list = (ArrayList<Student>)request.getAttribute("list"); if (list == null){System.out.println("为null");%>
<jsp:forward page="/myServlet?page=1"/>
<% } %>
<body>
<table border="1">
<tr>
<td>编号</td>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<% for (Student stu : list){%>
<tr>
<td><%=stu.getId()%></td>
<td><%=stu.getNumber() %></td>
<td><%=stu.getName() %></td>
<td><%=stu.getAge() %></td>
</tr>
<% } %>
<td>
<%=request.getAttribute("bar") %>
</td>
</tr>
</table>
</body>
</html>
dao类
public class Dao {
public Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3307/student";
String user="root";
String password="1234";
conn = DriverManager.getConnection(url,user,password);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return conn;
}
/** * 查询指定页(page这页)的记录 * @param page * @return */
public List<Student> find(int page){
List<Student> list = new ArrayList<Student>();
Connection conn = getConnection();
String sql = "select * from tb_stu limit ?,?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, (page-1)*Student.PAGE_SIZE);
ps.setInt(2, Student.PAGE_SIZE);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setNumber(rs.getInt("number"));
stu.setName(rs.getString("name"));
stu.setAge(rs.getInt("age"));
list.add(stu);
}
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return list;
}
/** * 总记录数 * @return */
public int findCount(){
int count = 0;
Connection conn = getConnection();
String sql = "select count(*) from tb_stu";
Statement sta;
try {
sta = conn.createStatement();
ResultSet rs = sta.executeQuery(sql);
if (rs.next()){
count = rs.getInt(1);
}
rs.close();
sta.close();
conn.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return count;
}
}
servlet
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int currPage = 1;
if (request.getParameter("page") == null){
currPage = 1;
}else{
currPage = Integer.parseInt(request.getParameter("page"));
}
System.out.println(currPage+","+request.getParameter("page"));
Dao dao = new Dao();
List<Student> list = dao.find(currPage);
request.setAttribute("list", list);
int pages;
int count = dao.findCount();
if (count % Student.PAGE_SIZE == 0){
pages = count/Student.PAGE_SIZE; //分为几页
}else{
pages = count / Student.PAGE_SIZE+1; //多余的增加一列
}
StringBuffer sb = new StringBuffer();
if (currPage != 1){
sb.append("<a href='myServlet?page="+(currPage-1)+"'>"+"上一页"+"</a>");
sb.append(" ");
}
for (int i=1; i<= pages; i++){
if(i == currPage){
sb.append("["+i+"]");
}else{
sb.append("<a href='myServlet?page="+i+"'>"+i+"</a>");
sb.append(" ");
}
}
if (currPage != pages){
sb.append("<a href='myServlet?page="+(currPage+1)+"'>"+"下一页"+"</a>");
}
System.out.println(sb.toString());
System.out.println("当前"+currPage);
request.setAttribute("bar", sb.toString());
request.getRequestDispatcher("").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
bean类
public class Student {
public static final int PAGE_SIZE=2; //每页显示的数量
private int id;
private int number;
private String name;
private int age;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}