Jsp真分頁(yè)實(shí)例---分頁(yè)
網(wǎng)頁(yè)的分頁(yè)功能的實(shí)現(xiàn)比較簡(jiǎn)單,實(shí)現(xiàn)方法也多種多樣。
今天總結(jié)一個(gè)簡(jiǎn)單的Jsp真分頁(yè)實(shí)例。
首先,提到分頁(yè)就要先明確一個(gè)概念,何為真分頁(yè)何謂假分頁(yè)。
假分頁(yè):一次性從數(shù)據(jù)庫(kù)讀出表的所有數(shù)據(jù)一次性的返回給客戶端,由js來控制每一頁(yè)的顯示。
真分頁(yè):由程序控制,每一次只返回一頁(yè)大小的數(shù)據(jù),顯示到客戶端。
由此可以很清楚的分辨出真假分頁(yè)各自的優(yōu)缺點(diǎn):
假分頁(yè):由于一次性讀出所有數(shù)據(jù)并返回給客戶端,如果數(shù)據(jù)量龐大,所以這一次的動(dòng)作可能是非常消耗服務(wù)器資源和帶寬的,
但是返回給客戶端以后就非常輕松了,客戶在一段時(shí)間內(nèi)不會(huì)再像服務(wù)器端請(qǐng)求資源。但不代表可能出現(xiàn)一些意外情況,
比如說客戶將瀏覽器關(guān)閉,重新訪問網(wǎng)站等。所以,如果數(shù)據(jù)量相當(dāng)龐大,不建議使用用真分頁(yè)。
真分頁(yè):假分頁(yè)每次只取需要的數(shù)據(jù)返回給客戶端,比起真分頁(yè)沒有那么大的數(shù)據(jù)庫(kù)壓力。但也因?yàn)檫@個(gè)工作特性,所以假分頁(yè)
的方法需要頻繁和服務(wù)器端進(jìn)行交互。既然頻繁交互,自然也會(huì)給服務(wù)器帶來負(fù)擔(dān)。
綜上:如果數(shù)據(jù)量較小,使用假分頁(yè)的效果會(huì)更優(yōu),如果數(shù)據(jù)量龐大,使用真分頁(yè)的效果更優(yōu)。
分析完特性,下面就來列舉一個(gè)簡(jiǎn)單的真分頁(yè)實(shí)例。
真分頁(yè)是通過程序來控制的,每次向數(shù)據(jù)庫(kù)請(qǐng)求需要的數(shù)據(jù)。
簡(jiǎn)述實(shí)現(xiàn)思路業(yè)務(wù)流程:
首先:客戶端帶著page參數(shù)請(qǐng)求客戶端,若沒有帶page參數(shù),說明是第一次訪問,則page參數(shù)默認(rèn)為0;
其次:服務(wù)端根據(jù)page參數(shù),調(diào)用相關(guān)函數(shù),從數(shù)據(jù)庫(kù)中取出表中數(shù)據(jù),封裝成相關(guān)對(duì)象,返回給客戶端,并且返回新page參數(shù)及總頁(yè)數(shù);
最后:再客戶端顯示請(qǐng)求的相關(guān)數(shù)據(jù),并根據(jù)page參數(shù)及總頁(yè)數(shù)兩個(gè)參數(shù),決定上一頁(yè)下一頁(yè)的按鈕是否可用。
數(shù)據(jù)庫(kù)操作類:
public class DBBean {
private Connection con;
private PreparedStatement pstmt;
private ResultSet rs;
private String dbName ="test";
private String dbuser = "root";
private String dbpass ="******";
static{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println(e);
}
}
public void prepareConnection(){
try{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName,dbuser,dbpass);
}catch(SQLException e){
System.out.println(e);
}
}
//關(guān)閉連接
public void close(){
try {
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con = null;
try {
if(pstmt!=null)
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt = null;
}
//設(shè)置參數(shù)
private void setParems(String[] parems){
if(parems!=null){
for(int i=0;i<parems.length;i++){
try {
pstmt.setString(i+1, parems[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public ResultSet executeQuery(String sql,String[] parems){
ResultSet res = null;
prepareConnection();
try {
pstmt = con.prepareStatement(sql);
setParems(parems);
res = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
return res;
}
}
學(xué)生類:
public class StudentBean {
private long id;
private String name;
private String phone;
private int age;
private int score;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
學(xué)生數(shù)據(jù)操作類
public class StudentDao implements StudentDaoIn {
@Override
public ArrayList<StudentBean> findByPage(int page){
DBBean db = new DBBean();
int begin = (page-1) * 5;
String sql = "select * from t_student limit "+begin+",5";
ResultSet rs = db.executeQuery(sql,null);
ArrayList<StudentBean> list = new ArrayList<StudentBean>();
try {
while(rs.next()){
StudentBean st = new StudentBean();
st.setName(rs.getString("name"));
st.setAge(rs.getInt("age"));
st.setId(rs.getInt("id"));
st.setPhone(rs.getString("phnoe"));
st.setScore(rs.getInt("score"));
list.add(st);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public int userCount(){
DBBean db = new DBBean();
String sql = "select count(*) from t_student";
ResultSet rs = db.executeQuery(sql, null);
int count = 0;
try {
rs.next();
count = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
}
相關(guān)業(yè)務(wù)邏輯
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String page = null;
page = request.getParameter("page");
if(page == null || page=="")
page = "1";
StudentDao studao = new StudentDao();
request.setAttribute("student",studao.findByPage(Integer.parseInt(page)));
request.setAttribute("pagenum",studao.userCount()/5+1);//總頁(yè)數(shù)
request.setAttribute("page", page);//當(dāng)前頁(yè)
request.getRequestDispatcher("student.jsp").forward(request, response);
}
前臺(tái)JSP代碼:
<table id="t_stu" border="1" cellpadding="2" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年齡</th>
<th>電話</th>
<th>成績(jī)</th>
</tr>
</thead>
<c:forEach items="${student}" var="st">
<tr>
<td>${st.getId()}</td>
<td>${st.getName()}</td>
<td>${st.getAge()}</td>
<td>${st.getPhone()}</td>
<td>${st.getScore()}</td>
</tr>
</c:forEach>
</table>
<br>
共 ${pagenum}頁(yè) 當(dāng)前 第${page}頁(yè)
<c:choose>
<c:when test="${page>1}">
<a href="getSutent?page=${page-1}" rel="external nofollow" ><input type="button" value="上一頁(yè)" ></a>
</c:when>
<c:otherwise>
<input type="button" value="上一頁(yè)" disabled="disabled" />
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page!=pagenum}">
<a href="getSutent?page=${page+1}" rel="external nofollow" ><input type="button" value="下一頁(yè)"></a>
</c:when>
<c:otherwise>
<input type="button" value="下一頁(yè)" disabled="disabled" />
</c:otherwise>
</c:choose>
本例是真分頁(yè)的一個(gè)簡(jiǎn)單實(shí)現(xiàn),有著明顯的缺點(diǎn)。
例如:
1.在后臺(tái)相關(guān)業(yè)務(wù)邏輯處,只對(duì)page做了簡(jiǎn)單的判斷,因?yàn)椴樵兿嚓P(guān)page時(shí),參數(shù)是寫入前臺(tái)a標(biāo)簽中的,所以懂技術(shù)的用戶,可以隨意改動(dòng)其值
由此查詢數(shù)據(jù)庫(kù)可能帶來意想不到的錯(cuò)誤。
2.功能不夠完善,僅提供了上一頁(yè)下一頁(yè)按鈕的簡(jiǎn)單功能。
另外:實(shí)現(xiàn)假分頁(yè)時(shí)可以結(jié)合ajax和json。以此可實(shí)現(xiàn)無刷新翻頁(yè),看起來功能和真分頁(yè)一樣。。。
相關(guān)文章
JSP 獲取用戶的真實(shí)IP兩種實(shí)現(xiàn)方法詳解
這篇文章主要介紹了JSP 獲取用戶的真實(shí)IP兩種實(shí)現(xiàn)方法詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
JSP 開發(fā)之THE SERVLET NAME ALREADY EXISTS.解決方法
這篇文章主要介紹了JSP 開發(fā)之THE SERVLET NAME ALREADY EXISTS.解決方法的相關(guān)資料,希望通過本文大家能解決這樣的問題,需要的朋友可以參考下2017-09-09
JSP使用自定義標(biāo)簽防止表單重復(fù)提交的方法
這篇文章主要介紹了JSP使用自定義標(biāo)簽防止表單重復(fù)提交的方法,以實(shí)例形式較為詳細(xì)的分析了JSP基于自定義標(biāo)簽防止表單重復(fù)提交的具體步驟與實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
建立JSP操作以提高數(shù)據(jù)庫(kù)訪問的效率
建立JSP操作以提高數(shù)據(jù)庫(kù)訪問的效率...2006-10-10
JSP 中response.setContentType()的作用及參數(shù)
這篇文章主要介紹了JSP 中response.setContentType()的作用及參數(shù) 的相關(guān)資料,希望通過本能幫助到大家,讓大家理解使用這部分內(nèi)容,需要的朋友可以參考下2017-09-09

