更新時(shí)間:2022-01-04 10:50:26 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1173次
為了將大量記錄分成多個(gè)部分,我們使用分頁。它允許用戶只顯示部分記錄。在單個(gè)頁面中加載所有記錄可能需要時(shí)間,因此始終建議創(chuàng)建分頁。在servlet中,我們可以很容易地開發(fā)分頁示例。
在這個(gè) servlet 分頁示例中,我們使用 MySQL 數(shù)據(jù)庫來獲取記錄。
在這里,我們?cè)?ldquo;test”數(shù)據(jù)庫中創(chuàng)建了“emp”表。emp 表有三個(gè)字段:id、name 和salary。手動(dòng)創(chuàng)建表和插入記錄或?qū)胛覀兊?sql 文件。
索引.html
<a href="ViewServlet?page=1">View Employees</a>
視圖Servlet.java
package com.javatpoint.servlets;
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.javatpoint.beans.Emp;
import com.javatpoint.dao.EmpDao;
@WebServlet("/ViewServlet")
public class ViewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String spageid=request.getParameter("page");
int pageid=Integer.parseInt(spageid);
int total=5;
if(pageid==1){}
else{
pageidpageid=pageid-1;
pageidpageid=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>");
out.print("<a href='ViewServlet?page=1'>1</a> ");
out.print("<a href='ViewServlet?page=2'>2</a> ");
out.print("<a href='ViewServlet?page=3'>3</a> ");
out.close();
}
}
Emp.java
package com.javatpoint.beans;
public class Emp {
private int id;
private String name;
private float salary;
//getters and setters
}
EmpDao.java
package com.javatpoint.dao;
import com.javatpoint.beans.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
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","","");
}catch(Exception e){System.out.println(e);}
return con;
}
public static List<Emp> getRecords(int start,int total){
List<Emp> list=new ArrayList<Emp>();
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);
}
con.close();
}catch(Exception e){System.out.println(e);}
return list;
}
}
輸出
以上就是關(guān)于“Servlet分頁實(shí)例”的介紹,如果大家對(duì)此比較感興趣,想了解更多相關(guān)知識(shí),可以關(guān)注一下動(dòng)力節(jié)點(diǎn)的Servlet教程,里面有更多更豐富的知識(shí)在等著大家去學(xué)習(xí),希望對(duì)大家能夠有所幫助。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)