`
morning2008
  • 浏览: 112878 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

servlet分页

 
阅读更多
采用servlet进行分页处理
Java代码 复制代码
步骤:

1.创建SepPage类,并设置有关的方法。

2.在进行页面的转移时传递并当前的页面并传递参数。

3.进行分页处理。

a.计算总记录数

b.怎么样进行分页

c.首页显示处理

d.传递有关参数

e.获取有关传递的页面的参数

实例:以查询所有的用户信息为例子进行说明

  1. packagebean;
  2. /**********************分页类描述************************/
  3. publicclassSepPage
  4. {
  5. privateintallRows;//一共有多少行记录
  6. privateintcurPage=1;//当前页面
  7. privateintrowPerPage=8;//一页显示多少行
  8. privateintallPages;//一共有多少页
  9. publicintgetAllRows()
  10. {
  11. returnallRows;
  12. }
  13. publicvoidsetAllRows(intallRows)
  14. {
  15. this.allRows=allRows;
  16. }
  17. publicintgetCurPage()
  18. {
  19. returncurPage;
  20. }
  21. publicvoidsetCurPage(intcurPage)
  22. {
  23. this.curPage=curPage;
  24. }
  25. publicintgetRowPerPage()
  26. {
  27. returnrowPerPage;
  28. }
  29. publicvoidsetRowPerPage(introwPerPage)
  30. {
  31. this.rowPerPage=rowPerPage;
  32. }
  33. publicintgetAllPages()
  34. {
  35. returnallPages;
  36. }
  37. publicvoidsetAllPages(intallPages)
  38. {
  39. this.allPages=allPages;
  40. }
  41. }
  42. 2.数据接收servlet
  43. packageservlet;
  44. importjava.io.IOException;
  45. importjava.io.PrintWriter;
  46. importjavax.servlet.ServletException;
  47. importjavax.servlet.http.HttpServlet;
  48. importjavax.servlet.http.HttpServletRequest;
  49. importjavax.servlet.http.HttpServletResponse;
  50. importbean.DBOperationBean;
  51. importjava.util.*;
  52. importutil.Convert;
  53. importjava.sql.*;
  54. importbean.*;
  55. /**
  56. *servlet类,获取有关客户的信息并进行相关的处理
  57. *@authorqihuasun
  58. */
  59. publicclassCustomerServletextendsHttpServlet
  60. {
  61. //查询所有sql语句
  62. publicstaticfinalStringSELECTBYALL="select*fromSCOTT.EXRM_T_CUSTOMERorderbyCUM_FULLNAME";
  63. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
  64. throwsServletException,IOException
  65. {
  66. this.doPost(request,response);
  67. }
  68. publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
  69. throwsServletException,IOException
  70. {
  71. //处理有关中文乱码问题
  72. request.setCharacterEncoding("GBK");
  73. response.setContentType("text/html;charset=GBK");
  74. response.setCharacterEncoding("GBK");
  75. Stringstatus=request.getParameter("status").trim();//获取页面传递的标志位status
  76. Stringpath="error.jsp";
  77. if(!status.equals("")||status!=null)
  78. {
  79. if(status.equals("findall"))
  80. {
  81. DBOperationBeandb=newDBOperationBean();
  82. Stringpage=request.getParameter("curpage");
  83. try
  84. {
  85. intcurPage=Integer.parseInt(request.getParameter("curPage"));
  86. //获取页面传递的参数
  87. SepPagepa=newSepPage();
  88. pa.setCurPage(curPage);
  89. ArrayListal=db.query(this.SELECTBYALL,pa);
  90. if(al!=null)
  91. {
  92. //path="main.jsp";
  93. path="TestMain2.jsp";
  94. request.setAttribute("all",al);
  95. }
  96. else
  97. {
  98. path="error.jsp";
  99. }
  100. }
  101. catch(Exceptionex)
  102. {
  103. ex.printStackTrace();
  104. }
  105. }
  106. }
  107. request.getRequestDispatcher(path).forward(request,response);
  108. }
  109. }
  110. 4.数据访问类
  111. packagebean;
  112. importjava.sql.*;
  113. importjavax.sql.*;
  114. importjavax.xml.parsers.DocumentBuilder;
  115. importjavax.xml.parsers.DocumentBuilderFactory;
  116. importjavax.naming.*;
  117. importorg.w3c.dom.Document;
  118. importorg.w3c.dom.Element;
  119. importorg.w3c.dom.NodeList;
  120. importjava.util.*;
  121. importbean.SepPage;
  122. /**
  123. *bean类,获取有关配置文件的信息的页面的信息,并进行有关的处理
  124. *@authorqihuasun
  125. */
  126. publicclassDBOperationBean
  127. {
  128. //驱动
  129. privatefinalStringDBDRIVER="oracle.jdbc.driver.OracleDriver";
  130. //数据库连接地址
  131. privatefinalStringDBURL="jdbc:oracle:thin:@127.0.0.1:1521:data";
  132. //数据库用户名
  133. privatefinalStringDBUSER="SCOTT";
  134. //数据库连接密码
  135. privatefinalStringDBPASSWORD="sqh";
  136. //声明一个数据库连接对象
  137. privateConnectionconn=null;
  138. privatePreparedStatementpstm=null;
  139. privateResultSetrs=null;
  140. publicDBOperationBean()
  141. {
  142. this.init();
  143. }
  144. privatevoidinit()//从数据库连接属性XML配置文件中获取关于连接的信息
  145. {
  146. conn=newDBConnection().getConnection();
  147. }
  148. privateConnectiongetConnection()//取得数据库连接并设置为当前连接
  149. {
  150. try
  151. {
  152. Class.forName(DBDRIVER);
  153. conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//连接数据库
  154. System.out.println("connected");
  155. }
  156. catch(Exceptionex)
  157. {
  158. ex.printStackTrace();
  159. }
  160. returnthis.conn;
  161. }
  162. publicArrayListquery(Stringsql,SepPagepage)throwsException
  163. {//执行查询,返回结果集
  164. ArrayListal=newArrayList();
  165. try
  166. {
  167. Connectionconn=this.getConnection();
  168. pstm=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
  169. ResultSet.CONCUR_READ_ONLY);
  170. rs=pstm.executeQuery();
  171. rs.last();//移动到最后一行
  172. page.setAllRows(rs.getRow());//设置一共有多少行记录
  173. //算出有多少页
  174. if(page.getAllRows()%page.getRowPerPage()==0)
  175. {
  176. page.setAllPages(page.getAllRows()/page.getRowPerPage());
  177. }
  178. else
  179. {
  180. page.setAllPages(page.getAllRows()/page.getRowPerPage()+1);
  181. }
  182. //判定是否是第一页
  183. if(page.getCurPage()==1)//当前页
  184. {
  185. //将指针移动到此ResultSet对象的开头,正好位于第一行之前
  186. rs.beforeFirst();
  187. }
  188. else
  189. {
  190. //将指针移动到此ResultSet对象的给定行编号
  191. rs.absolute((page.getCurPage()-1)*page.getRowPerPage());
  192. }
  193. inti=0;
  194. while(rs.next()&&i<page.getRowPerPage())
  195. {
  196. Customercu=newCustomer();
  197. cu.setId(rs.getInt("CUM_ID"));
  198. cu.setName(rs.getString("CUM_FULLNAME"));
  199. cu.setAddress(rs.getString("CUM_MAINADDRESS"));
  200. cu.setPhone(rs.getString("CUM_PHONE"));
  201. al.add(cu);
  202. i++;
  203. }
  204. }
  205. catch(Exceptionex)
  206. {
  207. ex.printStackTrace();
  208. }
  209. finally
  210. {
  211. try
  212. {
  213. if(conn!=null)
  214. {
  215. this.conn.close();
  216. }
  217. }
  218. catch(Exceptionex)
  219. {
  220. ex.printStackTrace();
  221. }
  222. }
  223. returnal;
  224. }
  225. }
  226. 4、页面显示
  227. <%@pagelanguage="java"contentType="text/html;charset=GBK"%>
  228. <%@pageimport="java.util.*,bean.SepPage"%>
  229. <%@pageimport="bean.Customer;"%>
  230. <%
  231. ArrayListall=(ArrayList)request.getAttribute("all");
  232. SepPageseppage=(SepPage)request.getAttribute("pagebean");
  233. //获取设置的SepPage参数
  234. %>
  235. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
  236. <htmlxmlns="http://www.w3.org/1999/xhtml">
  237. <head>
  238. <title>客户管理</title>
  239. <linkhref="css/style.css"rel="stylesheet"/>
  240. </head>
  241. <body>
  242. <divclass="top">
  243. 『作者管理』
  244. </div>
  245. <divclass="center">
  246. <formaction="AuthorServlet?status=selectbylike&curPage=1"method="post"style="margin:0;">
  247. <tableclass="fine"cellpadding="0"cellspacing="0">
  248. <tr>
  249. <tdcolspan="3"style="height:40px;">
  250. </td>
  251. </tr>
  252. <tr>
  253. <td>
  254. 序号
  255. </td>
  256. <td>
  257. 客户名称
  258. </td>
  259. <td>
  260. 联系电话
  261. </td>
  262. <td>
  263. 地址
  264. </td>
  265. &nbsp;&nbsp;&nbsp;&nbsp;<td>
  266. 操作
  267. </td>
  268. </tr>
  269. <%
  270. inti=0;
  271. if(all!=null&&all.size()!=0)
  272. {
  273. for(intj=0;j<all.size();j++)
  274. {
  275. i++;
  276. Customercus=(Customer)all.get(j);
  277. intauthorId1=cus.getId();
  278. Stringstr="找不到记录!!!";
  279. if(i==0)
  280. {
  281. out.println("<scrtipt>alert('找不到记录')<script>");
  282. }
  283. else
  284. {%>
  285. <tr>
  286. <td>
  287. <%=i%>
  288. </td>
  289. <td>
  290. <%=cus.getName()%>
  291. </td>
  292. <td>
  293. <%=cus.getPhone()%>
  294. </td>
  295. <td>
  296. <%=cus.getAddress()%>
  297. </td>
  298. <td>
  299. <td><ahref="CustomerServlet?status=selectbyid&id=<%=cus.getId()%>">更新</a>&nbsp;&nbsp;&nbsp;&nbsp;</td>&nbsp;&nbsp;
  300. <td><ahref="CustomerServlet?status=delete&id=<%=cus.getId()%>"onclick="if(confirm('是否删除业务信息?')){returntrue;}else{returnfalse;}">删除</a>&nbsp;&nbsp;&nbsp;&nbsp;</td>
  301. <td><ahref="addCus.jsp">添加</a></td>
  302. </tr>
  303. <%}%>
  304. <%
  305. }//endfor循环
  306. }//endifall!=null
  307. %>
  308. <%
  309. if(seppage!=null){
  310. %>
  311. <tr>
  312. <tdcolspan="3"
  313. style="height:32px;text-align:right;font-weight:bold;padding-right:10px;">
  314. <fontcolor="#FF5BAD">一共有&nbsp;<fontcolor="red"><%=seppage.getAllPages()%></font>&nbsp;页&nbsp;&nbsp;当前在第&nbsp;<font
  315. color="red"><%=seppage.getCurPage()%></font>&nbsp;页&nbsp;&nbsp;&nbsp;&nbsp;</font>
  316. <%
  317. if(seppage.getCurPage()!=1)//不是第一页,则首页,上一页可用
  318. {
  319. %>
  320. <ahref="CustomerServlet?status=selectbyall&curPage=1">首页</a>&nbsp;&nbsp;
  321. <a
  322. href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getCurPage()-1%>">上一页</a>
  323. <%
  324. }
  325. %>
  326. &nbsp;&nbsp;
  327. <%
  328. if(seppage.getCurPage()!=seppage.getAllPages())//不是最后一页,则有下一页和末页
  329. {
  330. %>
  331. <a
  332. href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getCurPage()+1%>">下一页</a>&nbsp;&nbsp;
  333. <a
  334. href="CustomerServlet?status=selectbyall&curPage=<%=seppage.getAllPages()%>">末
  335. 页</a>
  336. <%
  337. }
  338. %>
  339. </td>
  340. </tr>
  341. <%
  342. }
  343. %>
  344. </table>
  345. </form>
  346. <formaction="CustomerServlet?status=selectbylike&curPage=1"method="post"style="margin:0;">
  347. <tableclass="fine"cellpadding="0"cellspacing="0">
  348. <tr>
  349. <tdcolspan="3"style="height:40px;">
  350. <selectstyle="height:20px"name="sel">
  351. <optionselectedvalue="由客户姓名">
  352. 由客户姓名
  353. </option>
  354. <optionvalue="由地址">
  355. 由地址
  356. </option>
  357. </select>
  358. &nbsp;&nbsp;
  359. <inputtype="text"name="in"style="width:200px"/><inputtype="submit"value="查找"/>
  360. </td>
  361. </tr>
  362. </table>
  363. </form>
  364. </div>
  365. </body>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics