Java 分頁概念用於在帶有第一頁、第二頁、第三頁、第四頁等按鈕或連結的頁面之間移動。分頁的主要座右銘是透過點擊連結或按鈕立即在內容之間移動。 Java分頁為第一頁、第二頁、第三頁、第四頁等提供了多個連結或按鈕。用Java創建第一頁、第二頁、第三頁、第四頁等按鈕;我們有 Servlet 來實現這一點。
Java分頁概念是根據客戶需求使用第一頁、第二頁、第三頁、第四頁等按鈕或更多連結或按鈕來存取內容,以順利存取內容。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗下面顯示了我們為什麼要使用 JavaScript 分頁:
即時場景:
讓我們以亞馬遜網站或 Flipkart 網站為例,用於顯示其資料庫中的可用產品。假設他們有 100 萬種產品。如果他們試圖一次展示所有商品,顧客必須等待更多時間(例如一天)才能看到所有商品清單。
4. 建立一個 HTML 檢視頁面以查看分頁。
文法:
Servlet 語法:
//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>
上面提到的每個步驟我們都作為一個例子來更好地理解。完成所有範例後,您的專案結構在 Eclipse 中必須如下所示;否則可能無法運作。
建立一個動態 Web 專案並新增以下所有範例,如下所示:
注意:使用 Apache Tomcat 伺服器 7.0。建立客戶類別。
Java 程式碼: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; } }
為分頁邏輯建立 servlet 類別。
Java Servlet 程式碼: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(); } }
建立 MySQL 資料庫程式碼來保存清單值。
Java 程式碼: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; } }
查看 HTML 頁面。
HTML 代碼: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>
輸出:
說明:
Java 的分頁用於透過按鈕或連結立即在頁面之間移動。 Java 中的分頁可以透過 Servlet 和 HTML 來完成,也可以使用 MySQL jar 檔案來完成。
以上是Java 中的分頁的詳細內容。更多資訊請關注PHP中文網其他相關文章!