分页源代码

来源:互联网 发布:淘宝淘金币怎么没有了 编辑:程序博客网 时间:2024/05/21 15:41
 

package com.hbsi.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hbsi.domain.StudentBean;
import com.hbsi.service.StudentServiceImpl;

public class ControllServlet extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public ControllServlet() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

//  response.setContentType("text/html");
//  PrintWriter out = response.getWriter();
//  out
//    .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//  out.println("<HTML>");
//  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//  out.println("  <BODY>");
//  out.print("    This is ");
//  out.print(this.getClass());
//  out.println(", using the GET method");
//  out.println("  </BODY>");
//  out.println("</HTML>");
//  out.flush();
//  out.close();
  doPost(request,response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  StudentServiceImpl service=new StudentServiceImpl();
  //List<StudentBean> list=service.findAll();
  //先要计算一下一共有多少页
  
  int total=service.getSize();//一共有多少行记录
  
  
  int pageSize=3;//每页显示的记录行数
  
  int totalPages=total/pageSize;
  totalPages=(total%pageSize==0)?totalPages:totalPages+1;//总共有totalPages页
  //初始化当前页数
  int currentPage=1;
  if(request.getParameter("pageNo")!=null){
   currentPage=Integer.parseInt(request.getParameter("pageNo"));
  }
  if(currentPage<1){
   currentPage=1;
  }
  if(currentPage>totalPages){
   currentPage=totalPages;
  }
  List<StudentBean> list=service.listPage(currentPage, pageSize);
  
  
  
  request.setAttribute("list",list);
  //请求的转向---list.jsp把list中的数据显示给用户
  RequestDispatcher rd=request.getRequestDispatcher("/list.jsp?pageNo="+currentPage);
  rd.forward(request, response);
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

<%@ page language="java" import="java.util.*,com.hbsi.domain.*" pageEncoding="utf-8"%>
<%
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>My JSP 'list.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
  <%
  List<StudentBean> list=(List<StudentBean>)request.getAttribute("list");
  int pageNo=Integer.parseInt(request.getParameter("pageNo"));
  %>
  <h3>学生信息表</h3>
  <table>
  <%
  for(int i=0;i<list.size();i++){
 %>
 <tr>
 <td><%=list.get(i).getId() %></td>
 <td><%=list.get(i).getName() %></td>
 <td><%=list.get(i).getAge() %></td>
 <td><%=list.get(i).getAddress() %></td>
 </tr>
 <%
  }
  %>
  </table><br/>
  <a href="listStudent?pageNo=1">第一页</a> <a href="listStudent?paeNo=<%=pageNo-1 %>">上一页</a> <a href="listStudent?pageNo=<%=pageNo+1 %>">下一页</a> <a href="">最后页</a>
  
  </body>
</html>


 

原创粉丝点击