JSP servlet JavaBean Background Pagination Instance code resolution _JSP programming

Source: Internet
Author: User
Tags stub

First of all, the background paging need to sort out the idea of the page, the database needs to be paginated information into the list, and then according to the page feedback to the background page number to the list of sublist cut to cut the list to the front end to display.

1. Paging the demo file structure chart

Imported Packages

2. Code

Splitpageservlet Code

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 ex
  
 Tends HttpServlet {private static final long serialversionuid = 1L;
  /** * @see httpservlet#httpservlet () * * * Public splitpageservlet () {super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest request, httpservletrespons E 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 Code

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 Code

Package Bean;  public class Splitpagebean {private int allitems;//total number of records private int currentrecord;//current number of records private int Lastpagerecord ; Number of previous page records start with private int nextpagerecord;//the number of records on the next page start with private int lastpageindex; Previous private int nextpageindex;//next page private int currentpageindex;//The quantity of the current page private int numberforsplitpage;//per page Priva

 te int allpagenumber;
 public int Getallitems () {return this.allitems; 
 The 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; 
 The public void Setlastpageindex (int currentpageindex) {this.lastpageindex = currentPageIndex-1;
 public int Getnextpageindex () {return this.nextpageindex; 
 The public void Setnextpageindex (int currentpageindex) {this.nextpageindex = currentPageIndex-1;
 public int Getcurrentpageindex () {return this.currentpageindex; 
 The public void Setcurrentpageindex (int currentpageindex) {this.currentpageindex = CurrentPageIndex;
 public int getnumberforsplitpage () {return this.numberforsplitpage; 
 The 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 Code

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;
  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> lista
rr = 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 Code

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;
 }
 Shutdown database resource public
 void Close (Statement stat,connection conn) throws sqlexception{
  if (stat!=null) {
    Stat.close ();
  }
  if (conn!=null) {
    conn.close ();}}}


Splitpage Code

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);//number of per page int requestpageindexint = Integer.parseint (
  Requestpageindex)//The requested page number Splitpagebean Splitpagebean = new Splitpagebean (); Splitpagebean.setalliTEMs (Splitarr.size ())//Set the total number of records splitpagebean.setnumberforsplitpage (numberforsplitpageint);/Set the number of records per page
  Splitpagebean.setallpagenumber (Splitarr.size ());
  PageNumber = Splitpagebean.getallpagenumber (); Splitpagebean.setcurrentpageindex (Requestpageindexint)//Set Request Page page number 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 usage does not include end index} return result; }//public static void main (string[] args) throws ClassNotFoundException, sqlexception{//list<projectbean&Gt 
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 ());

 //    
// }

}

Display layer
Showviewindex code

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 Htt
  
 Pservlet {private static final long serialversionuid = 1L;
  /** * @see httpservlet#httpservlet () * * * Public showviewindex () {super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest request, httpservletrespons E response) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated Method Stub request.getrequestdispatcher ("/newfile.jsp"). Forward (Request, respons
 e);
/** * @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 Code

<%@ 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" >  

XML Code

<?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> &LT;SERVLET-NAME&GT;SPLITPAGESERVLET&LT;/SERVLet-name> <url-pattern>/servlet/SplitPageServlet</url-pattern> </servlet-mapping> </
 Web-app>

Run results show


Database describe


At the same time in this demo to recognize the use of XML in the Welcome interface through the <welcome-file-list> <welcome-file> static interface (JSP HTML) </welcome-file> < /welcome-file-list> can also use the servlet directly with the servlet name.
Beginners java and EE write a little bit of what they do, code written is not very standard, do not like to spray.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.