Java Pagination concept is applied for moving among the pages with First Page, Second Page, Third Page, Fourth Page etc. Buttons or Links. Pagination main motto is to move among the content immediately by clicking links or buttons. Java Pagination has multiple links or buttons provided for First Page, Second Page, Third Page, Fourth Page, etc. Create First Page, Second Page, Third Page, Fourth Page etc., buttons in Java; we have Servlets to achieve this.
Java Pagination concept is used to access the content by using First Page, Second Page, Third Page, Fourth Page, etc., buttons or more links or buttons based on client requirement to smoothly access the content.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsGiven below shows why we use JavaScript Pagination:
Real-Time Scenario:
Let’s take the amazon website or Flipkart website for displaying available products in their database. Let suppose they have 1 million products with them. If they are trying to show all the items at a time, the customer must wait more time, like one day, to see all the product lists.
4. Create an HTML view page for viewing pagination.
Syntax:
Servlet Syntax:
//create a setter and getter class public class Customer { private int id; private String name; private float salary; //setters and getters } //for pagination logic in servlet class protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter printWriterOut=response.getWriter(); String stringPageNumber=request.getParameter("page"); int paginationPageID=Integer.parseInt(stringPageNumber); int toalCount=pageNumbers; if(paginationPageID==1){} else{ paginationPageID=paginationPageID-1; paginationPageID=paginationPageID*toalCount+1; } } //database connection for getting customer values public static Connection getConnection(){ Connection con=null; try{ Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); }catch(Exception e){System.out.println(e);} return con; } //view output html page <body> <div class="a"> <a href="PaginationServlet?page=1">View Customer Details</a> </div> </body>
Above mentioned each step we have taken as a single example for better understanding. Once you have done all the examples, your project structure must be like the below in Eclipse; otherwise, it may not work.
Create a Dynamic web project and add all the below examples as like below:
Note: Use Apache Tomcat server 7.0.Creating customer class.
Java Code: Customer.java
package com.pagination.setget; public class Customer { private int id; private String name; private float salary; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } }
Creating servlet class for pagination logic.
Java Servlet Code: Pagination.java
package com.pagination.view; import java.io.IOException; import java.io.PrintWriter; 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 com.pagination.main.Pagination; import com.pagination.setget.*; @SuppressWarnings("serial") @WebServlet("/PaginationServlet") public class ViewPagination extends HttpServlet { protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { httpServletResponse.setContentType("text/html"); PrintWriter printWriterOut=httpServletResponse.getWriter(); String stringPageNumber=httpServletRequest.getParameter("page"); int paginationPageID=Integer.parseInt(stringPageNumber); int toalCount=5; if(paginationPageID==1){} else{ paginationPageID=paginationPageID-1; paginationPageID=paginationPageID*toalCount+1; } List<Customer> customerList=Pagination.getRecords(paginationPageID,toalCount); printWriterOut.print("<h2 style='color:green;text-align:center'>Introduction to Servlet Pagination</h2>"); printWriterOut.print("<h3 style='color:blue;text-align:center'>Customer Details in Table Format</h3>"); printWriterOut.print("<h1 style='color:brown'>We are in Page number=>"+stringPageNumber+"</h1>"); printWriterOut.print("<table style='color:navy' border='2' cellpadding='4' width='80%'>"); printWriterOut.print("<tr><th>Customer ID</th><th>Customer Name</th><th>Customer Salary</th>"); for(Customer customer:customerList){ printWriterOut.print("<tr><td>"+customer.getId()+"</td><td>"+customer.getName()+"</td><td>"+customer.getSalary()+"</td></tr>"); } printWriterOut.print("</table>"); printWriterOut.print("<a href='PaginationServlet?page=1'>First Page||</a> "); printWriterOut.print("<a href='PaginationServlet?page=2'>Second Page||</a> "); printWriterOut.print("<a href='PaginationServlet?page=3'>Third Page||</a> "); printWriterOut.print("<a href='PaginationServlet?page=4'>Fourth Page||</a> "); printWriterOut.print("<a href='PaginationServlet?page=5'>Fifth Page</a> "); printWriterOut.close(); } }
Create MySQL database code for saving list values.
Java Code: MySQLPagination.java
package com.pagination.main; import com.pagination.setget.*; import java.sql.*; import java.util.ArrayList; import java.util.List; public class Pagination { public static Connection getConnection(){ Connection connection=null; try{ Class.forName("com.mysql.jdbc.Driver"); connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); }catch(Exception e){System.out.println(e);} return connection; } public static List<Customer> getRecords(int start,int total){ List<Customer> list=new ArrayList<Customer>(); try{ Connection connection=getConnection(); PreparedStatement preparedStatement=connection.prepareStatement("select * from customer limit "+(start-1)+","+total); ResultSet rs=preparedStatement.executeQuery(); while(rs.next()){ Customer customer=new Customer(); customer.setId(rs.getInt(1)); customer.setName(rs.getString(2)); customer.setSalary(rs.getFloat(3)); list.add(customer); } connection.close(); }catch(Exception e){System.out.println(e);} return list; } }
View HTML page.
HTML Code: ViewPagination.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Pagination</title> <style type="text/css"> .a { text-align: center; } </style> </head> <body> <div class="a"> <a href="PaginationServlet?page=1">View Customer Details</a> </div> </body> </html>
Output:
Explanation:
Pagination of Java is used to move between the pages by using buttons or links immediately. Pagination in Java can be done with Servlet and HTML y using MySQL jar file.
Das obige ist der detaillierte Inhalt vonPaginierung in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!