当前位置:网站首页>JSP - paging
JSP - paging
2022-07-01 06:57:00 【allway2】
Introduce
JavaServer Pages or JSP It's based on Java Technology , For creating dynamic web pages .JSP Use special JSP Tag help in HTML File insert Java Code . for example , Displays the first five even numbers JSP The page will look like this ,
<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>
Pagination
Suppose there are multiple records in our database . under these circumstances , We use paging to break down records into more readable bits , This is true for both users and processors , Because a single page with multiple records may take time to load and users may be tired . Let's look at an example of paging ,
We will be in Netbeans Create our project in , Use XAMPP The server is connected to our SQL database . The following is the project structure .
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; } }
Output
common problem
- What is? JSP?
JavaServer Pages or JSP It's based on Java Technology , For creating dynamic web pages .JSP Use the only JSP Tag help in HTML File insert Java Code .
- What is pagination ?
Paging is used to divide records into more convenient readable bits , This is true for both users and processors , Because a single page with multiple records may take time to load and users may be tired .
Key points
This blog covers JavaServer Pages All the necessary points for paging in , The function and the method of paging application are discussed in detail . And it's in JavaServer Pages In the implementation of .
边栏推荐
- JSP - 分页
- Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
- AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
- Is it suitable for girls to study product manager? What are the advantages?
- 图解事件坐标screenX、clientX、pageX, offsetX的区别
- [wechat applet] how to build a building block development?
- Record an online interface slow query problem troubleshooting
- Methods of downloading Foreign Periodicals
- 北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
- 自动化测试平台(十三):接口自动化框架与平台对比及应用场景分析及设计思路分享
猜你喜欢
【MATLAB】求解非线性规划
问题:OfficeException: failed to start and connect(三)
[matlab] solve nonlinear programming
为什么这么多人转行产品经理?产品经理发展前景如何?
北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
[wechat applet low code development] second, resolve the code composition of the applet in practice
Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
Esp32 esp-idf ADC monitors battery voltage (with correction)
C语言实现【扫雷游戏】完整版(实现源码)
ESP32 ESP-IDF GPIO按键中断响应
随机推荐
JSP - 分页
Esp32 esp-idf GPIO key interrupt response
8 张图 | 剖析 Eureka 的首次同步注册表
Stored procedure learning notes
MySQL constraint learning notes
MySQL learning
Storage function learning notes
Terminology description in the field of software engineering
Embedded system
ESP32 ESP-IDF ADC监测电池电压(带校正)
MySQL data type learning notes
H5 web page determines whether an app is installed. If it is installed, it will jump to the summary of the scheme to download if it is not installed
ESP32 ESP-IDF GPIO按键中断响应
Database objects: view learning records
ESP32在电池供电时用ULP监测电池电压
Is it safe to buy funds on Alipay? Where can I buy funds
【分类模型】Q 型聚类分析
TDB中多个model情况下使用fuseki查询
AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
Some pits designed by NOC