效果如下:
<%--
Document : page
Created on : 2014-11-18, 8:55:02
Author : HJZ
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script language= "javascript">
function dumpTo(tot) { //函数参数定义时不加var,否则出错
var pageMsg = document.getElementById("pageTo").value;
var patrn=/^\d+$/;
if (patrn.test(pageMsg)) {
if(parseInt(pageMsg)<1 || parseInt(pageMsg)>parseInt(tot)) return;
window.location.href="page.jsp?page=" + pageMsg;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>分页显示</title>
</head>
<body>
<%
int pageCur = 0;
int pageTot = 20;
int pageBegin = 0;
String curPage = request.getParameter("page");
if(curPage == null) pageCur = 1;
else pageCur = Integer.valueOf(curPage);
pageBegin = pageCur-5;
if(pageBegin < 1) pageBegin = 1;
%>
<talbe border="10">
<tr>
<%if(pageCur!=1){%>
<td>
<input type="submit" name="skip" value="上一页" onclick="javascript:location.href='page.jsp?page=<%=pageCur-1%>'">
</td>
<%}%>
<% for(int i=pageBegin, j=1; j<=10 && i<=pageTot; ++i, ++j) {%>
<%if(i == pageCur){%>
<td>
<%=i%>
</td>
<%} else {%>
<td>
<input type="submit" name="skip" value="<%=i%>" onclick="javascript:location.href='page.jsp?page=<%=i%>'">
</td>
<%}%>
<% } %>
<%if(pageCur!=pageTot){%>
<td>
<input type="submit" name="skip" value="下一页" onclick="javascript:location.href='page.jsp?page=<%=pageCur+1%>'">
</td>
<%}%>
<td>
共<%=pageTot%>页
</td>
<td width="105px">
向第<input type="text" id="pageTo">页
</td>
<td>
<input type="button" name="skip" value="跳转" onclick="dumpTo(<%=pageTot%>)"> <!--传递总页数-->
</td>
</tr>
</table>
</body>
</html>