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

index.jsp


  	查看所有学生  
  

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 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+"/";
%>



  
        
    显示所有学生    
  
  
  
   	
<% ArrayList list=(ArrayList)session.getAttribute("list"); for(Student s:list){ %> <% } %>
学号姓名性别年龄班级DID生日 成绩英文名电话 操作
<%=s.getSid()%> <%=s.getSname()%> <%=s.getSsex()%> <%=s.getSage()%> <%=s.getSclass()%> <%=s.getDid()%> <%=s.getSbir()%> <%=s.getSscore()%> <%=s.getSename()%> <%=s.getSphone()%> 编辑 删除




首页| 上一页| 下一页| ">尾页



你可能感兴趣的:(java应用开发实验)