对于结果集的处理,大家各有办法,总结起来就是缓存和重新查询两种方式,lucene推荐第二种,如果使用缓存的方式感觉控制资源的释放比较困难,在并发性较多的情况下系统可能资源耗尽,因此我们这里采用lucene推荐的做法实现类似百度和google的分页效果。
终于调试通过了,大家可以直接使用了,哪里不清楚可以给我留言。
<%@ page contentType="text/html; charset=gb2312" buffer="none"%>
<%@ page import="java.util.*"%>
<%
//初始化全局变量
int totalNum = 0; //总共记录条数
int numPerPage = 0; //每页显示记录条数
int maxPageNum = 0; //本次搜索最大页数
int curPage = 1; //当前页
int startLocation = 0; //起始位置
Vector searchresult = (Vector) session.getAttribute("searchresult");
numPerPage = ((Integer)session.getAttribute("setPageNum")).intValue();
totalNum = searchresult.size();
maxPageNum=((totalNum%numPerPage) == 0)?totalNum/numPerPage:(totalNum/numPerPage+1);
curPage=startLocation/numPerPage + 1;
%>
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<title>查询结果页面</title>
<STYLE>
BODY { MARGIN: 6px 0px 0px; COLOR: #000; BACKGROUND-COLOR: #fff; FONT-FAMILY: arial }
TABLE { BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px }
TD { FONT-SIZE: 11pt; LINE-HEIGHT: 18px; FONT-FAMILY: arial; text-align:center}
.p { PADDING-LEFT: 18px; FONT-SIZE: 14px; WORD-SPACING: 4px }
#ft { CLEAR: both; BACKGROUND: #e6e6e6; LINE-HEIGHT: 20px; TEXT-ALIGN: center; FONT-SIZE: 12px; COLOR: #77c; FONT-FAMILY: Arial }
</STYLE>
</head>
<%
if(request.getParameter("startLocation")!=null)
{
startLocation = Integer.parseInt(request.getParameter("startLocation")); //取当前页码
System.out.println("startLocation ="+startLocation);
curPage = ((startLocation%numPerPage) == 0)?(startLocation/numPerPage):(startLocation/numPerPage+1);
curPage=curPage+1;
System.out.println("curPage ="+curPage);
}
%>
<body>
<form method="POST" >
<table border="0.5" width="100%" >
<tr bgcolor="#72963D">
<td align="center" class="colsTitle" nowrap height="19">
查询结果
</td>
</tr>
<%
if(curPage<maxPageNum){
System.out.println("startLocation="+startLocation);
for(int i=startLocation; i<startLocation+numPerPage; i++){
String result = (String) searchresult.elementAt(i);
out.println("<tr bgcolor=/"#F1F0ED/">");
out.println("<td align=/"center/" nowrap height=/"19/">");
out.println(result);
out.println("</td>");
out.println("</tr>");
}
}else{
System.out.println("totalNum="+totalNum);
for(int i=startLocation; i<totalNum; i++){
String result = (String) searchresult.elementAt(i);
out.println("<tr bgcolor=/"#F1F0ED/">");
out.println("<td align=/"center/" nowrap height=/"19/">");
out.println(result);
out.println("</td>");
out.println("</tr>");
}
}
//searchresult.clear();
%>
<tr><td>
<hr align="center" color="#990066" width="500">
</td>
<tr>
<td>
<%
if (curPage > 1 ) {
out.println("<a href='searchresult.jsp?startLocation="+(startLocation-numPerPage));
out.println("'>上一页</a>");
}
if(maxPageNum > 10) {
int totalNumShow = curPage + 9;
int startLocationPage = 0;
if( totalNumShow > maxPageNum) {
totalNumShow = maxPageNum;
}
if( maxPageNum > 11 && curPage > 11) {
startLocationPage = curPage - 11;
}
for(int i=startLocationPage;i<totalNumShow;i++) {
if(i+1 == curPage) {
%> [<%=i+1%>] <%
} else {
%><a href="searchresult.jsp?startLocation=<%=i*numPerPage%>"> <%=i+1 %> </a><%
}
}
}
else {
for( int i=0;i<maxPageNum;i++) {
if( i+1 == curPage) {
%> [<%=i+1 %>] <%
} else {
%><a href="searchresult.jsp?startLocation=<%=i*numPerPage%>"> <%=i+1 %> </a><%
}
}
}
if ( curPage < maxPageNum) {
out.println("<a href='searchresult.jsp?startLocation="+(startLocation+numPerPage));
out.println("'>下一页</a>");
}
%>
</td>
</tr>
</table>
</form>
</body>
</html>
终于调试通过了,大家可以直接使用了,哪里不清楚可以给我留言。
<%@ page contentType="text/html; charset=gb2312" buffer="none"%>
<%@ page import="java.util.*"%>
<%
//初始化全局变量
int totalNum = 0; //总共记录条数
int numPerPage = 0; //每页显示记录条数
int maxPageNum = 0; //本次搜索最大页数
int curPage = 1; //当前页
int startLocation = 0; //起始位置
Vector searchresult = (Vector) session.getAttribute("searchresult");
numPerPage = ((Integer)session.getAttribute("setPageNum")).intValue();
totalNum = searchresult.size();
maxPageNum=((totalNum%numPerPage) == 0)?totalNum/numPerPage:(totalNum/numPerPage+1);
curPage=startLocation/numPerPage + 1;
%>
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<title>查询结果页面</title>
<STYLE>
BODY { MARGIN: 6px 0px 0px; COLOR: #000; BACKGROUND-COLOR: #fff; FONT-FAMILY: arial }
TABLE { BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px }
TD { FONT-SIZE: 11pt; LINE-HEIGHT: 18px; FONT-FAMILY: arial; text-align:center}
.p { PADDING-LEFT: 18px; FONT-SIZE: 14px; WORD-SPACING: 4px }
#ft { CLEAR: both; BACKGROUND: #e6e6e6; LINE-HEIGHT: 20px; TEXT-ALIGN: center; FONT-SIZE: 12px; COLOR: #77c; FONT-FAMILY: Arial }
</STYLE>
</head>
<%
if(request.getParameter("startLocation")!=null)
{
startLocation = Integer.parseInt(request.getParameter("startLocation")); //取当前页码
System.out.println("startLocation ="+startLocation);
curPage = ((startLocation%numPerPage) == 0)?(startLocation/numPerPage):(startLocation/numPerPage+1);
curPage=curPage+1;
System.out.println("curPage ="+curPage);
}
%>
<body>
<form method="POST" >
<table border="0.5" width="100%" >
<tr bgcolor="#72963D">
<td align="center" class="colsTitle" nowrap height="19">
查询结果
</td>
</tr>
<%
if(curPage<maxPageNum){
System.out.println("startLocation="+startLocation);
for(int i=startLocation; i<startLocation+numPerPage; i++){
String result = (String) searchresult.elementAt(i);
out.println("<tr bgcolor=/"#F1F0ED/">");
out.println("<td align=/"center/" nowrap height=/"19/">");
out.println(result);
out.println("</td>");
out.println("</tr>");
}
}else{
System.out.println("totalNum="+totalNum);
for(int i=startLocation; i<totalNum; i++){
String result = (String) searchresult.elementAt(i);
out.println("<tr bgcolor=/"#F1F0ED/">");
out.println("<td align=/"center/" nowrap height=/"19/">");
out.println(result);
out.println("</td>");
out.println("</tr>");
}
}
//searchresult.clear();
%>
<tr><td>
<hr align="center" color="#990066" width="500">
</td>
<tr>
<td>
<%
if (curPage > 1 ) {
out.println("<a href='searchresult.jsp?startLocation="+(startLocation-numPerPage));
out.println("'>上一页</a>");
}
if(maxPageNum > 10) {
int totalNumShow = curPage + 9;
int startLocationPage = 0;
if( totalNumShow > maxPageNum) {
totalNumShow = maxPageNum;
}
if( maxPageNum > 11 && curPage > 11) {
startLocationPage = curPage - 11;
}
for(int i=startLocationPage;i<totalNumShow;i++) {
if(i+1 == curPage) {
%> [<%=i+1%>] <%
} else {
%><a href="searchresult.jsp?startLocation=<%=i*numPerPage%>"> <%=i+1 %> </a><%
}
}
}
else {
for( int i=0;i<maxPageNum;i++) {
if( i+1 == curPage) {
%> [<%=i+1 %>] <%
} else {
%><a href="searchresult.jsp?startLocation=<%=i*numPerPage%>"> <%=i+1 %> </a><%
}
}
}
if ( curPage < maxPageNum) {
out.println("<a href='searchresult.jsp?startLocation="+(startLocation+numPerPage));
out.println("'>下一页</a>");
}
%>
</td>
</tr>
</table>
</form>
</body>
</html>