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

编程知识 行业动态 更新时间:2024-06-13 00:21:18

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>



更多推荐

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

本文发布于:2023-03-31 14:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/b92cf2e75ee9de26f8ea9fd3416dabf4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下一页   上一页   分页   servlet   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!