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中文网其他相关文章!