当前位置:网站首页>JSP - 分页
JSP - 分页
2022-07-01 06:45:00 【allway2】
介绍
JavaServer Pages或 JSP 是一种基于 Java 的技术,用于创建动态网页。JSP 使用特殊的 JSP 标记帮助在 HTML 文件中插入 Java 代码。例如,显示前五个偶数的 JSP 页面将如下所示,
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <style> body{ padding: 50px; } </style> <body> <% for(int i=0;i<10;i++){ if(i%2==0) out.print("<h1>Even No: "+i+"</h1>"); } %> </body> </html>
分页
假设我们的数据库中有多个记录。在这种情况下,我们使用分页将记录分解成更方便的可读位,对用户和处理器来说都是如此,因为具有多个记录的单个页面可能需要时间来加载并且用户可能会很累。让我们看一个分页的例子,
我们将在 Netbeans 中创建我们的项目,使用 XAMPP 服务器连接到我们的 SQL 数据库。以下是项目结构。

index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <a href="view.jsp?page=1">View Employees</a> </body> </html>
view.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <style> body{ padding: 50px; } </style> <body> <%@ page import="java.util.*,Emp.*,EmpDao.*" %> <% String spageid=request.getParameter("page"); int pageid=Integer.parseInt(spageid); int total=5; if(pageid!=1){ pageid=pageid-1; pageid=pageid*total+1; } List<Emp> list=EmpDao.getRecords(pageid,total); out.print("<h1>Page No: "+spageid+"</h1>"); out.print("<table border='1' cellpadding='4' width='60%'>"); out.print("<tr><th>Id</th><th>Name</th><th>Salary</th>"); for(Emp e:list){ out.print("<tr><td>"+e.getId()+"</td><td>"+e.getName()+"</td><td>"+e.getSalary()+"</td></tr>"); } out.print("</table>"); %> <a href="view.jsp?page=1">1</a> <a href="view.jsp?page=2">2</a> <a href="view.jsp?page=3">3</a> </body> </html>
Emp.java
package Emp; /** * * @author 91963 */ public class Emp { private int id; private String name; private float salary; public void setId(int aInt) { id = aInt; } public void setName(String string) { name = string; } public void setSalary(float aFloat) { salary = aFloat; } public int getId(){ return id; } public String getName(){ return name; } public float getSalary(){ return salary; } }
EmpDao.java
package EmpDao; import java.sql.*; import Emp.*; import java.util.ArrayList; import java.util.List; /** * * @author 91963 */ public class EmpDao { public static Connection getConnection(){ Connection con=null; try{ Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",""); }catch(ClassNotFoundException | SQLException e){System.out.println(e);} return con; } public static List<Emp> getRecords(int start,int total){ List<Emp> list=new ArrayList<>(); try { try (Connection con = getConnection()) { PreparedStatement ps=con.prepareStatement("select * from emp limit "+(start-1)+","+total); ResultSet rs=ps.executeQuery(); while(rs.next()){ Emp e=new Emp(); e.setId(rs.getInt(1)); e.setName(rs.getString(2)); e.setSalary(rs.getFloat(3)); list.add(e); } } }catch(Exception e){System.out.println(e);} return list; } }
输出




常见问题
- 什么是 JSP?
JavaServer Pages 或 JSP 是一种基于 Java 的技术,用于创建动态网页。JSP 使用唯一的 JSP 标记帮助在 HTML 文件中插入 Java 代码。
- 什么是分页?
分页用于将记录分成更方便的可读位,对用户和处理器都是如此,因为具有多条记录的单个页面可能需要时间来加载并且用户可能会很累。
关键要点
本博客涵盖了有关 JavaServer Pages 中分页的所有必要要点,深入讨论了其功能和分页应用的方法。以及它在 JavaServer Pages 中的实现。
边栏推荐
- [wechat applet] view container and basic content components
- 如果我在广州,到哪里开户比较好?究竟网上开户是否安全么?
- 存储函数学习笔记
- 给逆序对数求原数组
- 【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现
- 2022 Jiangsu Vocational College skills competition (secondary vocational school) network construction and application open competition volume
- 比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
- Some pits designed by NOC
- Embedded system
- MySQL learning
猜你喜欢

嵌入式系统

Software engineering review

Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor

概率论学习笔记

2022 Jiangsu Vocational College skills competition (secondary vocational school) network construction and application open competition volume
![[wechat applet low code development] second, resolve the code composition of the applet in practice](/img/ab/28ab01db84b1437220e659118b2871.png)
[wechat applet low code development] second, resolve the code composition of the applet in practice

Dirty reading, unreal reading and unrepeatable reading

【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现

8 figures | analyze Eureka's first synchronization registry

Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
随机推荐
Summary of wechat official account embedded program to jump to wechat
【微信小程序低代码开发】二,在实操中化解小程序的代码组成
Principle of introducing modules into node
JSON module
Solve the problem that the class defined in meta-inf.services cannot be read
8 张图 | 剖析 Eureka 的首次同步注册表
Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor
Internet worm
产品学习(一)——结构图
解决无法读取META-INF.services里面定义的类
Terminology description in the field of software engineering
rclone常用子命令中文解释
Product learning (III) - demand list
How to use SCI hub
@Propagation property of transactional requires_ New in-depth understanding
Async and await
三说 拷贝构造之禁用
HW(OD)岗面试题
了解ESP32睡眠模式及其功耗
【微信小程序】如何搭积木式开发?