发布者:flyfox
在页面中,当检索的数据很多时,通常需要分页显示数据,并要实现翻页。
下面将通过一些例程来说明实现jsp页面翻页技术的实现。
首先,在jsp中,通过java servlet 来检索数据,而用jsp来调用结果来显示。
因而,此技术可分为两个部分(依赖关系):
1. 在服务器端的servlet 中的实现
要点:
& 将查询条件保存到session中,取session中的查询条件
& 设置statement对象的maxrows(确定一页显示多少行数据)
& 顺序地通过执行sql语句查询数据,按maxrows 来检索一个maxrows的数据,
下一页再检索下一maxrows的数据,以此类推。
2. 在jsp中的显示实现
要点:
& 显示maxrows条数据
& 通过“下一页”按钮或超链再次调用刚才的servlet查询下一maxrows的数据
流程如图所示:
下面通过一个例程来说明(一个servlet程序和一个jsp程序):
l querymedicine. java (medicine. querymedicine)
package medicine;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import medicine.medicinelist;
import medicine.searchdata;
////////////////////////////////////////////////////
// 接—mutiquery.jsp页面,并从request中得到5条查询条件 ///
// 按条件查询药品,并将结果存进session的“medicinelist”中。 ///
// 张乾 2000年3月15日 ///
////////////////////////////////////////////////////
public class querymedicine extends httpservlet {
private dbconnectionmanager connmgr;
//initialize global variables
public void init(servletconfig config) throws servletexception {
super.init(config);
connmgr = dbconnectionmanager.getinstance();
}
//========================处理 http get 请求============================
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
statement stmt=null;
connection con = connmgr.getconnection("medicine");
if (con == null) {
response.sendredirect("/medicine/con_error.html");
return;
}
// ==================== 创建数据库statement =============================
try {
stmt=con.createstatement();
}
catch (exception e)
{
connmgr.freeconnection("medicine",con);
response.sendredirect("/medicine/stmt_error.html");
return;
}
//————————————————————-
long all_count=0; //存满足条件的药品总数
//===================从request中得到session======================
try{
httpsession session = request.getsession(false);
if (session == null) {
connmgr.freeconnection("medicine",con);
response.sendredirect("/medicine/session_error.html");
return;
}
//———————————————————–
//======从“下一页”提交来的参数取得前页最后一条数据的药品编码=======
string ll_pos=request.getparameter("pos");
//如果pos=-1则表示要从头开始查
//———————————————————-
//=====================查询用的sql语句串==========================
string sqlstatment="select medicines.ypbm, medsmalltypes.zlmc, medsupertypes.clmc,
medtypes.dlmc, medicines.ypm, medicines.zyyx, medicines.ypzy from medicines,
medsmalltypes,medsupertypes,medtypes where medicines.ypbm>"+ll_pos+"
and (( medsmalltypes.zlbm = medicines.zlbm ) and ( medsupertypes.clbm = medicines.clbm )
and ( medtypes.dlbm = medicines.dlbm ) and ( ( medicines.del_flag = 0 ) ";
//—————————————————–
string zlbm;
string dlbm;
string clbm;
string zyyx;
string ypm;
searchdata slist=new searchdata();
synchronized (session) {
slist=(searchdata)session.getattribute("searchlist");
}
if(ll_pos.equals("-1")){
//===表示不是由“下一页”过来要从头开始查数据==========
if(slist!=null){
synchronized (session) {
session.removeattribute("searchlist");
}
}
slist=new searchdata();
//=============从设置查询条件页面取得查询条件的参数===================
zlbm=request.getparameter("zlbm");
dlbm=request.getparameter("dlbm");
clbm=request.getparameter("clbm");
zyyx=request.getparameter("zyyx");
ypm=request.getparameter("ypm");
//——————————————————
//=================将查询条件参数通过vector存到session中==========
slist.setzlbm (zlbm);
slist.setdlbm (dlbm);
slist.setclbm (clbm);
slist.setzyyx (zyyx);
slist.setypm (ypm);
synchronized (session) {
session.setattribute ("searchlist",slist);
}
//———————————————————
}
//——————————————————–
else
{
//============================取出查询条件参数====================
if(slist!=null){
zlbm=slist.getzlbm();
dlbm=slist.getdlbm();
clbm=slist.getclbm();
zyyx=slist.getzyyx();
ypm=slist.getypm();
}
else{
if(stmt!=null) stmt.close();
stmt.setmaxrows(0);
connmgr.freeconnection("medicine",con);
response.sendredirect("/medicine/session_error.html");
return;
}
//————————————————-
}
string sql2="select count(*) from medicines where del_flag=0";
//=====================根据条件参数设置sql语句=======================
if(!(zlbm.trim().equals("0"))) {
sqlstatment+=(" and ( medicines.zlbm="+zlbm.trim()+" ) ");
sql2+=(" and zlbm="+zlbm.trim()+"");
}
if(!(dlbm.trim().equals("0"))) {
sqlstatment+=(" and ( medicines.dlbm="+dlbm.trim()+" )");
sql2+=(" and dlbm="+dlbm.trim()+"");
}
if(!(clbm.trim().equals("0"))) {
sqlstatment+=(" and ( medicines.clbm="+clbm.trim()+" ) ");
sql2+=(" and clbm="+clbm.trim()+"");
}
if(!(zyyx.trim().length ()==0)) {
sqlstatment+=(" and ( medicines.zyyx like %"+zyyx.trim()+"% ) ");
sql2+=(" and zyyx like %"+zyyx.trim()+"%");
}
if(!(ypm.trim().length ()==0)) {
sqlstatment+=(" and ( medicines.ypm like %"+ypm.trim()+"% ) ");
sql2+=(" and ypm like %"+ypm.trim()+"%");
}
sqlstatment+=" )) order by medicines.ypbm asc ";
//————————————————————-
vector list = new vector();
//================= 设置一页显示的数据条数(一次检索出的数据条数)===========
stmt.setmaxrows(25);
//———————————————————————-
//===================执行查询将结果放到resultset中================
resultset rs = stmt.executequery(sqlstatment);
resultset rs2 = stmt.executequery(sql2);
//————————————————
if(rs==null){ //如果没有查询结果数据
if(stmt!=null) stmt.close();
stmt.setmaxrows(0);
connmgr.freeconnection("medicine",con);
response.sendredirect("/medicine/no_medicine.html"); //定向到一个页面
return;
}
//====================将药品信息填入数据对象并存入vector中================
if(rs2.next()){
all_count=rs2.getlong(1); //取得总条数
}
if(rs2!=null)rs2.close();
while(rs.next()){
medicinelist m = new medicinelist();
m.setypbm(rs.getint("ypbm"));
m.setzlmc(rs.getstring("zlmc"));
m.setclmc(rs.getstring("clmc"));
m.setdlmc(rs.getstring("dlmc"));
m.setypm(rs.getstring("ypm"));
m.setzyyx(rs.getstring("zyyx"));
m.setypzy(rs.getstring("ypzy"));
m.setclbm(clbm);
m.setdlbm(dlbm);
m.setzlbm(zlbm);
list.addelement(m);
}
//———————————————————-
if(rs!=null)rs.close();
stmt.setmaxrows(0);
if(stmt!=null)stmt.close();
connmgr.freeconnection("medicine",con);
//========================存入session中===========================
synchronized (session) {
session.setattribute("medicinelist",list);
}
//———————————————–
}catch (sqlexception e){
connmgr.freeconnection("medicine",con);
response.sendredirect("/medicine/sql_error.html");
return;}
//=======================重定向到一个jsp页面==========================
string url="/medicine/querymedicine.jsp?all_count="+all_count;
servletcontext sc = getservletcontext();
requestdispatcher rd = sc.getrequestdispatcher(url);
rd.forward(request, response);
//——————————————————-
}
public void destroy() {
// =================== 在servlet退出时终止数据库连接,取消statement对象
if(connmgr!=null) connmgr.release();
//————————————————————————
}
//========================处理http post 请求 ============================
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
doget(request,response);
}
//==========================取得 servlet 信息 ============================
public string getservletinfo() {
return "medicine.querymedicine information";
}
}
l querymedicine.jsp
<%@ page language="java" session="true" import="java.util.*,
medicine.method, medicine.medicinelist"contenttype="text/html;charset=gbk" %>
<script language="javascript">
function next(){
var ls_pos=document.form2.maxpos.value;
document.location="/servlet/medicine.querymedicine?pos="+ls_pos;
}
function detail(ypbm){
document.location="/servlet/medicine.detail?ypbm="+ypbm;
}
</script>
<!–add head–>
<div align="center">
<body topmargin="0" leftmargin="10" rightmargin="10” bgcolor="#ffffff">
<div align="center">
<center><iframe width=760 height=130 noresize scrolling=no frameborder=0
marginheight=0 marginwidth=0 src="..\medicinetitle.html"></iframe>
</div>
<!–finish–>
<title>商品列表</title>
<%
method md = new method();
vector sklist;
synchronized (session) {
sklist = (vector) session.getattribute("medicinelist");
}
%>
<!—————————– 显示表头 ——————————–>
<%
if (sklist == null or sklist.size() <= 0)
{
response.sendredirect("/medicine/no_medicine.html");
return;
}
else
{%>
<center>您好!以下是商品的列表,共有<font color=#ff0000>
<%=request.getparameter("all_count")%></font>条符合条件的药品信息
</center>
<div align="center">
<center>
<table border="1" cellpadding="0" cellspacing="0" width="840"
bordercolorlight="#ffffff" bordercolordark="#000000">
<tr bgcolor="#339933">
<td width=42 align="center"><font color="#339933"><b>明细</b></font></td>
<td width=160 align="center"><font color="#ffffff"><b>商品名称</b></font></td>
<td width=52 align="center"><font color="#ffffff"><b>超类</b></font></td>
<td width=145 align="center"><font color="#ffffff"><b>大类</b></font></td>
<td width=145 align="center"><font color="#ffffff"><b>子类</b></font></td>
<td width=270 align="center"><font color="#ffffff"><b>描述</b></font></td>
</tr>
<!——————————————————————————————————————->
<%
medicinelist d=new medicinelist();
for (int index=0; index < sklist.size();index++)
{
d = (medicinelist)sklist.elementat(index);
string lshref;
if (d.getypzy()==null or d.getypzy().equals("http://") or d.getypzy().equals(""))
{
lshref="";
}else{
lshref="<a href="+d.getypzy()+">";
}
out.println("<form name=form1 method=post action=/servlet/medicine.detail?ypbm="+d.getypbm()+">");
out.println("<tr bgcolor=#f3f3e9><td align=center><font color=#000000 size=2>
<input type=submit name=submit1 value=明细></font></td><td align=center><font color=#000000 size=2>");
out.println(lshref+d.getypm()+"</a></font></td><td align=center><font color=#000000 size=2>");
out.println(d.getclmc()+"</font></td><td align=center><font color=#000000 size=2>"
+d.getdlmc()+"</font></td><td align=center><font color=#000000 size=2>");
out.println(d.getzlmc()+"</font></td><td align=center><font color=#000000 size=2>"
+md.notnull(d.getzyyx())+"</font></td></tr></form>");
out.flush();
}
%>
<form name=form2 id=form2>
<input type=hidden name=maxpos id=maxpos value=<%=d.getypbm()%>>
</form>
<%
}
%>
</table>
<p></p>
<%
string ssll=request.getparameter("all_count");
long temp=new long(ssll);
long cou= temp.longvalue();
if(sklist.size()==25&&cou>25)
{%>
<input type=button value=" 下页 " onclick="next(1);">
<%}%>
<%
synchronized (session) {
if(session.getattribute("medicinelist")!=null){
session.removevalue("medicinelist");
}
}
%>
</center>
<p align="center"><input type="button" value=" 返回 " name="b3" onclick="history.back()">
<br><br><br>
<!–add bottom–>
<div align="center">
<center><iframe width=760 height=140 noresize scrolling=no frameborder=0
marginheight=0 marginwidth=0 src="..\bottom.html"></iframe>
</div>
<!–finish–>
