欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp servlet javaBean后台分页实例代码解析

程序员文章站 2022-07-12 08:28:14
首先后台分页需要理清分页思路,把数据库里面需要分页的信息放到list集合中,然后按照页面反馈给后台的页码对list集合进行sublist切割把切完的list传到前端进行显示...

首先后台分页需要理清分页思路,把数据库里面需要分页的信息放到list集合中,然后按照页面反馈给后台的页码对list集合进行sublist切割把切完的list传到前端进行显示。

1.分页的demo文件结构图

jsp servlet javaBean后台分页实例代码解析

导入的包

jsp servlet javaBean后台分页实例代码解析

2.代码

splitpageservlet代码

package actionservlet;

import java.io.ioexception;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import bean.projectbean;
import service.splitpage;

/**
 * servlet implementation class splitpageservlet
 */
@webservlet("/splitpageservlet")
public class splitpageservlet extends httpservlet {
 private static final long serialversionuid = 1l;
  
 /**
  * @see httpservlet#httpservlet()
  */
 public splitpageservlet() {
  super();
  // todo auto-generated constructor stub
 }

 /**
  * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
  */
 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
  // todo auto-generated method stub
  list<projectbean>listarr = new arraylist<projectbean>();
  string currrentpagestring = request.getparameter("currrentpage");
  string numberforsplitpage = request.getparameter("numberforsplitpage");
  if( currrentpagestring ==null){
   currrentpagestring = "1";
  }
  if( numberforsplitpage == null){
   numberforsplitpage = "5";
  }
  splitpage splitpage = new splitpage();
  try {
   listarr = splitpage.allsplitpage(numberforsplitpage, currrentpagestring);
   request.setattribute("subresult", listarr);
  } catch (classnotfoundexception | sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  system.out.println(splitpage.pagenumber);
  system.out.println(splitpage.currentpageindex);
  request.setattribute("pagenumber", splitpage.pagenumber);
  request.setattribute("currentpageindex", splitpage.currentpageindex);
  request.getrequestdispatcher("/servlet/showviewindex").forward(request, response);
  
 }

 /**
  * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
  */
 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
  // todo auto-generated method stub
  doget(request, response);
 }

}

projectbean代码

package bean;

public class projectbean {
 private string projectid = null;
 private string projectname = null;
 private string projecttype = null;
 private string userno = null;
 private string projectuser = null;
 public string getprojectid(){
  if(projectid==null){
   projectid ="";
  }
  return this.projectid;
 }
 public void setprojectid(string projectid){
  this.projectid = projectid;
 }
 
 public void setprojectname(string projectname){
  this.projectname = projectname;
 }
 
 public string getprojectname(){
  return this.projectname;
 }
 
 public void settype(string projecttype){
  this.projecttype = projecttype;
 }
 
 public string getprojecttype(){
  return this.projecttype;
 }
 
 public void setuserno(string userno){
  this.userno = userno;
 }
 
 public string getuserno(){
  return this.userno;
 }
 
 public void setprojectuser(string projectuser){
  this.projectuser = projectuser;
 }
 
 public string getprojectuser(){
  return this.projectuser;
 }
 
}

splitpagebean 代码

package bean;

public class splitpagebean {
 private int allitems;//总的记录数
 private int currentrecord;//当前的记录数
 private int lastpagerecord ; //上一页记录数开始数
 private int nextpagerecord;//下一页记录数开始数
 private int lastpageindex ; //上一页
 private int nextpageindex;//下一页
 private int currentpageindex;//当前页
 private int numberforsplitpage;//每页分的数量
 private int allpagenumber;

 public int getallitems(){
  return this.allitems;
 }
 public void setallitems(int allitems){
  this.allitems = allitems; 
 }
 
 public int getcurrentrecord(){
  return this.currentrecord;
 }
 public void setcurrentrecord(int currentpageindex){
  this.currentrecord = currentpageindex * this.numberforsplitpage; 
 }
 
 public int getlastpagerecord(){
  return this.lastpagerecord;
 }
 public void setlastpagerecord(int lastpageindex){
  this.lastpagerecord = lastpageindex * this.numberforsplitpage; 
 }
 
 public int getnextpagerecord(){
  return this.nextpagerecord;
 }
 public void setnextpagerecord(int nextpageindex){
  this.nextpagerecord = nextpageindex * this.numberforsplitpage; 
 }
 
 public int getlastpageindex(){
  return this.lastpageindex;
 }
 public void setlastpageindex(int currentpageindex){
  this.lastpageindex = currentpageindex - 1; 
 }
 
 public int getnextpageindex(){
  return this.nextpageindex;
 }
 public void setnextpageindex(int currentpageindex){
  this.nextpageindex = currentpageindex - 1; 
 }
 
 public int getcurrentpageindex(){
  return this.currentpageindex;
 }
 public void setcurrentpageindex(int currentpageindex){
  this.currentpageindex = currentpageindex; 
 }
 
 public int getnumberforsplitpage(){
  return this.numberforsplitpage;
 }
 public void setnumberforsplitpage(int numberforsplitpage){
  this.numberforsplitpage = numberforsplitpage; 
 }
 
 public int getallpagenumber(){
  return this.allpagenumber;
 }
 public void setallpagenumber(int allitems){
  this.allpagenumber = allitems / this.numberforsplitpage + 1; 
 } 
 
}

queryproject代码

package dao;

import java.sql.connection;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.arraylist;
import java.util.list;

import bean.projectbean;
import service.connectdatabase;

public class queryproject {
 private list<projectbean> listarr = new arraylist();
 public list<projectbean> queryallproject() throws classnotfoundexception, sqlexception{
  connection conn=null;
  statement stat=null;
  resultset rs=null;
  connectdatabase connectdatabase =new connectdatabase();
  conn = connectdatabase.connect();
  stat = conn.createstatement();
  rs = stat.executequery("select*from project");
  
  while(rs.next()){
   projectbean projectbean = new projectbean();
   projectbean.setprojectid(rs.getstring("projectid"));
   projectbean.setprojectname(rs.getstring("projectname"));
   projectbean.settype(rs.getstring("projecttype"));
   projectbean.setuserno(rs.getstring("userno"));
   projectbean.setprojectuser(rs.getstring("projectuser"));
   listarr.add(projectbean);
  }
  connectdatabase.close(stat, conn);
  return listarr;
 }
// public static void main(string[] args) throws classnotfoundexception, sqlexception{
//  list<projectbean> listarr = new arraylist();
//  queryproject queryproject = new queryproject();
//  listarr = queryproject.queryallproject();
//  projectbean projectbean = new projectbean();
//  projectbean = listarr.get(0);
//  system.out.println(projectbean.getprojectid());
//  projectbean = listarr.get(2);
//  system.out.println(projectbean.getprojectid());
//  system.out.println(listarr.size());
// }
}

conenctdatabase代码

package service;

import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
import java.sql.statement;

public class connectdatabase {
 private string url = "jdbc:mysql://localhost:3306/zhongchuangweb";
 private string user = "root";
 private string password = "12345";
 
 public connection connect() throws classnotfoundexception, sqlexception{
   class.forname("com.mysql.jdbc.driver");
   connection conn = drivermanager.getconnection(url,user,password); 
   return conn;
 }
 //关闭数据库资源
 public void close(statement stat,connection conn) throws sqlexception{
  if(stat!=null){
    stat.close();
  }
  if(conn!=null){
    conn.close();
  }
 }
}

splitpage代码

package service;

import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;

import bean.projectbean;
import bean.splitpagebean;
import dao.queryproject;

public class splitpage {
 public static int pagenumber;
 public static int currentpageindex;
 private list<projectbean> splitarr = new arraylist(); 

 {
  queryproject queryproject = new queryproject();
  try {
   splitarr = queryproject.queryallproject();
  } catch (classnotfoundexception | sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }
 public list<projectbean> allsplitpage(string numberforsplitpage,string requestpageindex) throws classnotfoundexception, sqlexception{
  list<projectbean> result = new arraylist<projectbean>(); 
  int numberforsplitpageint = integer.parseint(numberforsplitpage);//每页的数量
  int requestpageindexint = integer.parseint(requestpageindex);//请求的页码
  splitpagebean splitpagebean = new splitpagebean();
  splitpagebean.setallitems(splitarr.size());//设置总的记录数
  splitpagebean.setnumberforsplitpage(numberforsplitpageint);//设置每页的记录数量
  splitpagebean.setallpagenumber(splitarr.size());
  pagenumber = splitpagebean.getallpagenumber();
  splitpagebean.setcurrentpageindex(requestpageindexint);//设置请求页页码
  currentpageindex = splitpagebean.getcurrentpageindex();
  splitpagebean.setlastpagerecord(requestpageindexint-1);
  if(splitpagebean.getallitems()<splitpagebean.getnumberforsplitpage()){
   result = splitarr;
  }else if(splitpagebean.getcurrentpageindex()*splitpagebean.getnumberforsplitpage() > splitpagebean.getallitems()){
   result = splitarr.sublist(splitpagebean.getlastpagerecord(),splitpagebean.getallitems());
  }else{
   result = splitarr.sublist(splitpagebean.getlastpagerecord(),splitpagebean.getlastpagerecord()+splitpagebean.getnumberforsplitpage());//sublist用法不包含末尾索引
  }
  return result;
 }
 
// public static void main(string[] args) throws classnotfoundexception, sqlexception{
//  list<projectbean> testresult = new arraylist<projectbean>(); 
//  projectbean projectbean = new projectbean();
//  splitpage splitpage = new splitpage();
//  testresult = splitpage.allsplitpage("5","1");
//  projectbean = testresult.get(0);
//  system.out.println(projectbean.getprojectid());
//  projectbean = testresult.get(1);
//  system.out.println(projectbean.getprojectid());
//  projectbean = testresult.get(2);
//  system.out.println(projectbean.getprojectid());
//  projectbean = testresult.get(3);
//  system.out.println(projectbean.getprojectid());
//  projectbean = testresult.get(4);
//  system.out.println(projectbean.getprojectid());
//  projectbean = testresult.get(5);
//  system.out.println(projectbean.getprojectid());
//    
// }

}

显示层
showviewindex代码

package view;

import java.io.ioexception;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

/**
 * servlet implementation class showviewindex
 */
@webservlet("/showviewindex")
public class showviewindex extends httpservlet {
 private static final long serialversionuid = 1l;
  
 /**
  * @see httpservlet#httpservlet()
  */
 public showviewindex() {
  super();
  // todo auto-generated constructor stub
 }

 /**
  * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
  */
 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
  // todo auto-generated method stub
  request.getrequestdispatcher("/newfile.jsp").forward(request, response);
 }

 /**
  * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
  */
 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
  // todo auto-generated method stub
  doget(request, response);
 }

}

jsp页面代码

<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>数据库内容分页显示</title>
<style type="text/css">
*{ margin:0; padding:0;}
ul li{ width:50px; height:24px; line-height:24px; float:left; margin-left:0px; display:inline; margin-top:5px; overflow:hidden;}
ul li button{width:50px;height:24px;margin:0px;padding:0px;}
</style>
</head>
<body>
 <table>
  <thead>
   <tr>
    <th style="text-align:center;">编号</th>
    <th style="text-align:center;">名称</th>
    <th style="text-align:center;">类型</th>
    <th style="text-align:center;">学号</th>
    <th style="text-align:center;">负责人</th>
   </tr>
  </thead>
  <tbody>
    <c:foreach var="project" items="${subresult}">
     <tr>
      <td><c:out value="${project.projectid}"></c:out></td>
      <td><c:out value="${project.projectname}"></c:out></td>
      <td><c:out value="${project.projecttype}"></c:out></td>
      <td><c:out value="${project.userno}"></c:out></td>
      <td><c:out value="${project.projectuser}"></c:out></td>
     </tr>
    </c:foreach>
  </tbody>
 </table>
 <div align = "center">
    <ul>
    <c:choose>
     <c:when test="${currentpageindex>1}">
       <li><a href="${pagecontext.request.contextpath}/servlet/splitpageservlet?currrentpage=${currentpageindex-1}"><button><</button></a></li>
     </c:when>
     <c:otherwise>
       <li><a><button><</button></a></li>
     </c:otherwise>
    </c:choose>
     <%
        int i =(integer)request.getattribute("pagenumber");
       for (int j=0;j<i;j++)
       {%>
       <li><a href="${pagecontext.request.contextpath}/servlet/splitpageservlet?currrentpage=<%=j+1%>"><button><%=j+1%></button></a></li>
      <%}%>
     <c:choose>
     <c:when test="${currentpageindex+1<=pagenumber}">
       <li><a href="${pagecontext.request.contextpath}/servlet/splitpageservlet?currrentpage=${currentpageindex+1}"><button>></button></a></li>
     </c:when>
     <c:otherwise>
       <li><a><button>></button></a></li>
     </c:otherwise>
    </c:choose>
    </ul> 
   </div> 
</body>
</html>

xml代码

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 <display-name></display-name>
 <welcome-file-list>
 <welcome-file>splitpageservlet</welcome-file>
 </welcome-file-list> 
 <servlet>
 <servlet-name>showviewindex</servlet-name>
 <servlet-class>view.showviewindex</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>showviewindex</servlet-name>
 <url-pattern>/servlet/showviewindex</url-pattern>
 </servlet-mapping>
 <servlet>
 <servlet-name>splitpageservlet</servlet-name>
 <servlet-class>actionservlet.splitpageservlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>splitpageservlet</servlet-name>
 <url-pattern>/servlet/splitpageservlet</url-pattern>
 </servlet-mapping>
</web-app>

运行结果展示

jsp servlet javaBean后台分页实例代码解析

数据库describe

jsp servlet javaBean后台分页实例代码解析

同时在本次demo中认识到了xml的中使用欢迎界面通过  <welcome-file-list>  <welcome-file>静态界面(jsp html)</welcome-file> </welcome-file-list>还可以使用servlet直接用servlet名即可。
初学java和javaee 写一点自己做的东西,代码写的不是很规范,不喜勿喷。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。