1.创建SepPage类,并设置有关的方法。
2.在进行页面的转移时传递并当前的页面并传递参数。
3.进行分页处理。
a.计算总记录数
b.怎么样进行分页
c.首页显示处理
d.传递有关参数
e.获取有关传递的页面的参数
实例:以查询所有的用户信息为例子进行说明
-
packagebean;
-
-
publicclassSepPage
- {
-
privateintallRows;
-
privateintcurPage=1;
-
privateintrowPerPage=8;
-
privateintallPages;
-
publicintgetAllRows()
- {
-
returnallRows;
- }
-
publicvoidsetAllRows(intallRows)
- {
-
this.allRows=allRows;
- }
-
publicintgetCurPage()
- {
-
returncurPage;
- }
-
publicvoidsetCurPage(intcurPage)
- {
-
this.curPage=curPage;
- }
-
publicintgetRowPerPage()
- {
-
returnrowPerPage;
- }
-
publicvoidsetRowPerPage(introwPerPage)
- {
-
this.rowPerPage=rowPerPage;
- }
-
publicintgetAllPages()
- {
-
returnallPages;
- }
-
publicvoidsetAllPages(intallPages)
- {
-
this.allPages=allPages;
- }
- }
-
2.数据接收servlet
-
packageservlet;
-
importjava.io.IOException;
-
importjava.io.PrintWriter;
-
importjavax.servlet.ServletException;
-
importjavax.servlet.http.HttpServlet;
-
importjavax.servlet.http.HttpServletRequest;
-
importjavax.servlet.http.HttpServletResponse;
-
importbean.DBOperationBean;
-
importjava.util.*;
-
importutil.Convert;
-
importjava.sql.*;
-
importbean.*;
-
-
publicclassCustomerServletextendsHttpServlet
- {
-
-
publicstaticfinalStringSELECTBYALL="select*fromSCOTT.EXRM_T_CUSTOMERorderbyCUM_FULLNAME";
-
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
-
throwsServletException,IOException
- {
-
this.doPost(request,response);
- }
-
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
-
throwsServletException,IOException
- {
-
-
request.setCharacterEncoding("GBK");
-
response.setContentType("text/html;charset=GBK");
-
response.setCharacterEncoding("GBK");
-
Stringstatus=request.getParameter("status").trim();
-
Stringpath="error.jsp";
-
if(!status.equals("")||status!=null)
- {
-
-
if(status.equals("findall"))
- {
-
DBOperationBeandb=newDBOperationBean();
-
Stringpage=request.getParameter("curpage");
-
try
- {
-
intcurPage=Integer.parseInt(request.getParameter("curPage"));
-
-
SepPagepa=newSepPage();
- pa.setCurPage(curPage);
-
ArrayListal=db.query(this.SELECTBYALL,pa);
-
if(al!=null)
- {
-
-
path="TestMain2.jsp";
-
request.setAttribute("all",al);
- }
-
else
- {
-
path="error.jsp";
- }
-
- }
-
catch(Exceptionex)
- {
- ex.printStackTrace();
- }
- }
-
- }
- request.getRequestDispatcher(path).forward(request,response);
-
- }
- }
-
4.数据访问类
-
packagebean;
-
importjava.sql.*;
-
importjavax.sql.*;
-
importjavax.xml.parsers.DocumentBuilder;
-
importjavax.xml.parsers.DocumentBuilderFactory;
-
importjavax.naming.*;
-
importorg.w3c.dom.Document;
-
importorg.w3c.dom.Element;
-
importorg.w3c.dom.NodeList;
-
importjava.util.*;
-
importbean.SepPage;
-
-
publicclassDBOperationBean
- {
-
-
privatefinalStringDBDRIVER="oracle.jdbc.driver.OracleDriver";
-
-
privatefinalStringDBURL="jdbc:oracle:thin:@127.0.0.1:1521:data";
-
-
privatefinalStringDBUSER="SCOTT";
-
-
privatefinalStringDBPASSWORD="sqh";
-
-
privateConnectionconn=null;
-
privatePreparedStatementpstm=null;
-
privateResultSetrs=null;
-
publicDBOperationBean()
- {
-
this.init();
- }
-
privatevoidinit()
- {
-
conn=newDBConnection().getConnection();
- }
-
privateConnectiongetConnection()
- {
-
try
- {
- Class.forName(DBDRIVER);
-
-
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
-
System.out.println("connected");
- }
-
catch(Exceptionex)
- {
- ex.printStackTrace();
- }
-
-
-
returnthis.conn;
- }
-
-
publicArrayListquery(Stringsql,SepPagepage)throwsException
-
{
-
ArrayListal=newArrayList();
-
try
- {
-
Connectionconn=this.getConnection();
- pstm=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- rs=pstm.executeQuery();
-
rs.last();
-
page.setAllRows(rs.getRow());
-
-
if(page.getAllRows()%page.getRowPerPage()==0)
- {
- page.setAllPages(page.getAllRows()/page.getRowPerPage());
- }
-
else
- {
-
page.setAllPages(page.getAllRows()/page.getRowPerPage()+1);
- }
-
-
if(page.getCurPage()==1)
- {
-
- rs.beforeFirst();
- }
-
else
- {
-
-
rs.absolute((page.getCurPage()-1)*page.getRowPerPage());
- }
-
inti=0;
-
while(rs.next()&&i<page.getRowPerPage())
- {
-
Customercu=newCustomer();
-
cu.setId(rs.getInt("CUM_ID"));
-
cu.setName(rs.getString("CUM_FULLNAME"));
-
cu.setAddress(rs.getString("CUM_MAINADDRESS"));
-
cu.setPhone(rs.getString("CUM_PHONE"));
- al.add(cu);
- i++;
- }
- }
-
catch(Exceptionex)
- {
- ex.printStackTrace();
- }
-
finally
- {
-
try
- {
-
if(conn!=null)
- {
-
this.conn.close();
- }
- }
-
catch(Exceptionex)
- {
- ex.printStackTrace();
- }
- }
-
returnal;
- }
- }
-
4、页面显示
-
<%@pagelanguage="java"contentType="text/html;charset=GBK"%>
-
<%@pageimport="java.util.*,bean.SepPage"%>
-
<%@pageimport="bean.Customer;"%>
- <%
-
ArrayListall=(ArrayList)request.getAttribute("all");
-
SepPageseppage=(SepPage)request.getAttribute("pagebean");
-
- %>
-
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
-
<htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>客户管理</title>
-
<linkhref="css/style.css"rel="stylesheet"/>
- </head>
- <body>
-
<divclass="top">
- 『作者管理』
- </div>
-
<divclass="center">
-
<formaction="AuthorServlet?status=selectbylike&curPage=1"method="post"style="margin:0;">
-
<tableclass="fine"cellpadding="0"cellspacing="0">
- <tr>
-
<tdcolspan="3"style="height:40px;">
- </td>
- </tr>
- <tr>
- <td>
- 序号
- </td>
- <td>
- 客户名称
- </td>
- <td>
- 联系电话
- </td>
- <td>
- 地址
- </td>
- <td>
- 操作
- </td>
- </tr>
- <%
-
inti=0;
-
if(all!=null&&all.size()!=0)
- {
-
for(intj=0;j<all.size();j++)
- {
- i++;
- Customercus=(Customer)all.get(j);
-
intauthorId1=cus.getId();
-
Stringstr="找不到记录!!!";
-
if(i==0)
- {
-
-
-
out.println("<scrtipt>alert('找不到记录')<script>");
- }
-
else
- {%>
-
- <tr>
- <td>
- <%=i%>
- </td>
- <td>
- <%=cus.getName()%>
- </td>
- <td>
- <%=cus.getPhone()%>
- </td>
- <td>
- <%=cus.getAddress()%>
- </td>
- <td>
-
<td><ahref="CustomerServlet?status=selectbyid&id=<%=cus.getId()%>">更新</a> </td>
-
-
<td><ahref="CustomerServlet?status=delete&id=<%=cus.getId()%>"onclick="if(confirm('是否删除业务信息?')){returntrue;}else{returnfalse;}">删除</a> </td>
-
<td><ahref="addCus.jsp">添加</a></td>
- </tr>
-
- <%}%>
-
- <%
-
}
-
}
- %>
- <%
-
if(seppage!=null){
- %>
- <tr>
-
<tdcolspan="3"
-
style="height:32px;text-align:right;font-weight:bold;padding-right:10px;">
-
<fontcolor="#FF5BAD">一共有 <fontcolor="red"><%=seppage.getAllPages()%></font> 页 当前在第 <font
-
color="red"><%=seppage.getCurPage()%></font> 页 </font>
- <%
-
if(seppage.getCurPage()!=1)
- {
- %>
-
<ahref="CustomerServlet?status=selectbyall&curPage=1">首页</a>
- <a
-
href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getCurPage()-1%>">上一页</a>
- <%
- }
- %>
-
- <%
-
if(seppage.getCurPage()!=seppage.getAllPages())
- {
- %>
- <a
-
href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getCurPage()+1%>">下一页</a>
- <a
-
href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getAllPages()%>">末
- 页</a>
- <%
- }
- %>
- </td>
- </tr>
- <%
- }
- %>
- </table>
- </form>
-
<formaction="CustomerServlet?status=selectbylike&curPage=1"method="post"style="margin:0;">
-
<tableclass="fine"cellpadding="0"cellspacing="0">
- <tr>
-
<tdcolspan="3"style="height:40px;">
-
<selectstyle="height:20px"name="sel">
-
<optionselectedvalue="由客户姓名">
- 由客户姓名
- </option>
-
<optionvalue="由地址">
- 由地址
- </option>
- </select>
-
-
<inputtype="text"name="in"style="width:200px"/><inputtype="submit"value="查找"/>
- </td>
- </tr>
- </table>
- </form>
- </div>
- </body>
分享到:
相关推荐
JAVA+servlet分页处理,JSP分页超级算法和页面,PAGE
jsp+servlet 分页
页面是jsp使用ajax显示分页功能,servlet控制中转,结构清晰,基于mvc思想实现,包含数据库文件。
jsp加servlet分页代码,jsp加servlet分页代码jsp加servlet分页代码jsp加servlet分页代码jsp加servlet分页代码jsp加servlet分页代码jsp加servlet分页代码
NULL 博文链接:https://luoshisiji.iteye.com/blog/1225256
这是一个用Servlet技术分页的组件、直接导入这个jar包就可以使用分页组件了、很方便的。有一个分页标签 在页面中就用这个分页标签来完成数据的分页吧!!!多谢欣赏哦!
简单的小例子Ext+servlet 分页 类表 分页 服务器端用的是 servlet
jsp + servlet分页代码更新版
这是一个jsp+javaBean+servlet分页 主要功能 是通过 jstl标签中的 中的开始 begin和末尾end、list结果集的items属性 操作整个分页。大家有兴趣看看
JSP Servlet 分页技术 进行分页管理
我的目标是,让每个人都学会分页
使用FreeMarker配合Servlet写了一个分页程序,使用环境是Java7和Tomcat7,Eclipse 4.2,程序比较简单,并且加了注释,应该算是清楚易懂吧,适合初学者(我本身也是初学者),有问题私信可以再和大家讨论。
一个完整的servlet分页,根据个人需要改变里面的参数想怎么用就怎么用
这是分页的核心代码,结合servlet和页面jsp之间来显示SQLserver中查询数据并分页显示。
知道分页的大小之后,我们生成了页好的选取下拉框,每次选择第几页的时候,都会向Servlet传递当前选择页号的参数,这样Servlet调用后面的DAO相应的方法,取得文章列表信息,再回传到JSP以供显示。 ; charset=UTF-8...
用jsp+servlet技术实现分页,包含jsp标签
这是一个采用ajax技术做的一个简单的分页,代码简单,注释清楚。下载后稍作按说明稍作提示即可运行。说明文档在下载的文件夹中。
通过servlet以及JDBC等知识点,实现分页功能,满足分页需求!
SQL传参Servlet翻页.....................
基本的jsp+servlet+mysql实现的分页的查询