调用的servlet的代码 ,楼主可以参考下 ,我学习时都用这个 比较好用 ,应该是你要的jsp+servlet+javabean分页package com.xinwen.servlet.user;import java.io.IOException; import java.io.PrintWriter; import java.util.List;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.winwen.util.Page; import com.winwen.util.PageJudge; import com.xinwen.dao.user.UserInfoDao; import com.xinwen.pojo.user.UserInfo; public class ShowUser extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<UserInfo> userlist = new UserInfoDao().showUser(); //你从数据库里面读出来的数据 int pageSize = 1; if (request.getParameter("pageSize") == null) { pageSize = 1; } else { pageSize = Integer.parseInt(request.getParameter("pageSize")); } Page page = new PageJudge().pageJudge(userlist, pageSize, "ShowUser"); request.setAttribute("showUsers", page); request.getRequestDispatcher("user_manage/showUser.jsp").forward( request, response); }} - -额说我回复太快
我的思路是这样的。。 首先在 servlet 取得页面上的当前页数 如: Integer pageIndex = request.getParameter("pageIndex"); 当前也可以取显示多少页,简单化不写了。 然后在 DAO 提供一个方法 , 第一个是当前页数,第二个是取得多少数据 如: List getList(int pageIndex,int pageSize){ // sql 语句大概就这样 sql = "select top pageSize from tb where id not in (select top pageIndex from tb)"; } servlet 根据界面来传入相应的参数进行调整调用 DAO 返回给 界面就 OK 了: 如: 表示每页显示 15 行 request.setParameter(DAO.getList(pageIndex,15 )) ; 页面显示就不用写了吧。
然后在写jdbc取数据的时候只取出自己想要的部分.
不同的数据库实现分页的查询有所差异, 而且有些数据库不支持.
比如sqlserver 就有select top 10 * from user 表示 取user表的所有字段, 但只取前10条记录. 根据这个查询, 你可演化出分页查询.
但不论你实用什么分页,你都要写一个分页的Bean,
主要有,总共行数,页数,每页要显示多少行,等字段
页面可以用Struts标签循环显示数据
也可以用Jstl的<c:foreach></c:foreach>标签
servlet来做控制层,从数据库中取东西
bean实现具体的业务操作
然后在写jdbc取数据的时候只取出自己想要的部分.
然后传回给servlet,再用JSTL显示出来,现在困惑于在list_room类中该怎么调用bean,然后返回给servlet
等候解答...谢谢楼上几位的回答
楼主还没有学习Hibernate吧!~!
没有关系!~!分页还是一样的可以实现!~!
1.你写一个PageBean的实体类!@~!包括(当前页、总页数、总行数、每页显示多少行、List)。这个List就是你要查询的实体的集合,把查询的信息都保存到这个list中,
2.就只用去创建一个存储过程!~!用来查询不同页数的记录!~
3.在数据访问层就直接用那个存储过程,sql = "{call 存储过程名字(参数)}";把查询的结果集保存到PageBean的List中
最后直接返回这个PageBean的对象就可以了!~!
4.直接从Servlet中获取PageBean、或者从JSP中获取这个就可以了!~!
package com.winwen.util;import java.util.List;/**
* @author lov1986 E-mail: [email protected]
* @version 创建时间:Aug 19, 2008 9:22:22 PM 类说明
*/public class Page {
/**
* 当前是第几页
*/
private int curPage; // 当前是第几页 /**
* 一共有多少页
*/
private int maxPage; // 一共有多少页 /**
* 一共有多少条记录(行)
*/
private int maxRowCount; // 一共有多少行 /**
* 每页显示多少行
*/
private int rowsPerPage; // 每页显示多少行 /**
* 当前页面要显示的数据列表(包含数据库返回对象的列表)
*/
private List list; // 当前页面显示的数据 /**
* 提交的表单的名称
*/
private String formName = "pageForm";// 提交的表单名称 /**
* 显示分页的前台页面路径名称
*/
private String target = ""; /**
* 页面显示下一页,上一页
*/
private String pageStr = ""; /**
* 默认每页显示记录数
*/
private static int ROWS_PER_PAGE = 15; /**
* 分页信息初始化,每页显示记录数使用默认值
*
*/
public Page() {
this.curPage = 1;
this.maxPage = 1;
this.maxRowCount = 0;
this.rowsPerPage = ROWS_PER_PAGE;
} /**
* <p>
* 设置当前分页页面要显示的数据对象列表
* </p>
*
* <p>
* 该对象可以是一个实现了 List 接口的任意列表
* </p>
*
* @param list -
* 实现了 List 接口的任意列表
*/
public void setList(List list) {
this.list = list;
} /**
* 获取要在当前分页页面显示的数据对象列表
*
* @return 要显示在前台页面的数据对象列表
*/
public List getList() {
return this.list;
} /**
* 设置记录的总行数(即所有满足条件的纪录总数)
*
* @param maxRowCount -
* 记录的总行数
*/
public void setMaxRowCount(int maxRowCount) {
this.maxRowCount = maxRowCount;
} /**
* 获取记录的最大行数
*
* @return - 满足条件的所有记录条数
*/
public int getMaxRowCount() {
return this.maxRowCount;
} /**
* 设置当前页数(即要显示在前台的当前页数)
*
* @param curPage -
* 当前页数
*/
public void setCurPage(int curPage) {
this.curPage = curPage;
} /**
* 返回在前台显示的当前页数
*
* @return - 当前页数
*/
public int getCurPage() {
return this.curPage;
} /**
* <p>
* 设置每页显示的记录条数
* </p>
* <p>
* 注意:如果每页显示的记录条数小于等于 0 ,则每页显示的记录条数将置为默认值
* </p>
*
* @param rows -
* 每页显示的记录条数
*/
public void setRowsPerPage(int rows) {
if (rows > 0) {
this.rowsPerPage = rows;
} else {
this.rowsPerPage = ROWS_PER_PAGE;
}
} /**
* 设置页面提交表单的名称,默认为 pageForm,在一般情况下,使用默认值就能正常使用
*
* @param formName -
* 提交表单的名称
*/
public void setFormName(String formName) {
this.formName = formName;
} /**
* 设置显示分页的前台页面路径名称
*
* @param target -
* 分页页面路径名称
*/
public void setTarget(String target) {
this.target = target;
} /**
* 获取每页显示的记录条数
*
* @return - 每页显示的记录条数
*/
public int getRowsPerPage() {
return this.rowsPerPage;
} /**
* 根据记录总数和每页显示的记录条数,计算总共有多少页
*
*/
public void setMaxPage() {
if (this.maxRowCount % this.rowsPerPage == 0) {
this.maxPage = this.maxRowCount / this.rowsPerPage;
} else {
this.maxPage = this.maxRowCount / this.rowsPerPage + 1;
}
} /**
* 获取根据记录总数和每页显示的记录数计算得出的总页数
*
* @return - 记录总页数
*/
public int getMaxPage() {
return this.maxPage;
} /**
* 获取前台分页页面提交表单的名称
*
* @return - 提交表单的名称
*/
public String getFormName() {
return this.formName;
} /**
* 获取前台分页页面的路径名称
*
* @return - 前台分页页面的路径名称
*/
public String getTarget() {
return this.target;
} /**
* 获取默认每页显示的记录条数
*
* @return - 默认每页显示的记录条数
*/
public int getDefaultRowsPerPage() {
return ROWS_PER_PAGE;
}
/**
* 根据前台分页页面名称和相关分页信息,获取前台页面提交表单和页面信息的 HTML 代码
*
* @param aUrl -
* 前台分页页面的路径名称
* @return - 前台页面提交表单和页面信息的 HTML 代码
*/
public String getPageStr(String aUrl) { String strFirstPage = "首页";
String strLastPage = "末页";
String strNextPage = "下一页";
String strPrevPage = "上一页";
String strCurrentPage = "页次";
String strGoto = "转至:"; StringBuffer strPages = new StringBuffer(); // 跳转脚本
strPages.append("<script language='JavaScript'>\r\n");
strPages.append("<!--\r\n");
strPages.append("function Jumping(){ \r\n\t");
strPages.append("document." + formName + ".submit();\r\n\t");
strPages.append("return ;\r\n");
strPages.append("}\r\n"); strPages.append("function gotoPage(pagenum){ \r\n\t");
strPages.append("document." + formName
+ ".pageSize.value = pagenum;\r\n\t");
strPages.append("document." + formName + ".submit();\r\n\t");
strPages.append("return ;\r\n");
strPages.append("}\r\n");
strPages.append("-->\r\n");
strPages.append("\r\n</script>\r\n"); strPages.append("<table border=0 align=center>\r\n");
strPages.append("<form name=" + formName + " action=" + aUrl
+ " method=get>\r\n");
strPages.append("<tr>\r\n\t<td>\r\n\t\t");
strPages.append("<b>共 <font color=#930909>" + this.getMaxRowCount()
+ "</font> 条记录</b> \r\n\t"); if (getMaxPage() == 1 || getMaxRowCount() == 0) {
strPages
.append("\t<font face=webdings color=#930909></font><font color=#930909><b>"
+ strFirstPage + "</b></font>\r\n\t");
strPages
.append("\t<font face=webdings color=#930909></font><font color=#930909><b>"
+ strPrevPage + "</b></font>\r\n\t");
strPages
.append("\t<font color=#930909><b>"
+ strNextPage
+ "</b></font><font face=webdings color=#930909></font>\r\n\t");
strPages
.append("\t<font color=#930909><b>"
+ strLastPage
+ "</b></font><font face=webdings color=#930909>:</font>\r\n\t");
} else if (getMaxPage() > 1 && getCurPage() == 1) {
strPages
.append("\t<font face=webdings color=#930909></font><font color=#930909><b>"
+ strFirstPage + "</b></font>\r\n\t");
strPages
.append("\t<font face=webdings color=#930909></font><font color=#930909><b>"
+ strPrevPage + "</b></font>\r\n\t");
strPages.append("\t<a href=\"javascript:gotoPage("
+ (getCurPage() + 1) + ")\"><font color=black><b>"
+ strNextPage
+ "</b></font></a><font face=webdings></font>\r\n\t");
strPages.append("\t<a href=\"javascript:gotoPage(" + getMaxPage()
+ ")\"><font color=black><b>" + strLastPage
+ "</b></font></a><font face=webdings>:</font>\r\n\t");
} else if (getMaxPage() > 1 && getCurPage() < getMaxPage()) {
strPages
.append("\t<font face=webdings></font><a href=\"javascript:gotoPage(1)\"><font color=black><b>"
+ strFirstPage + "</b></font></a>\r\n\t");
strPages
.append("\t<font face=webdings></font><a href=\"javascript:gotoPage("
+ (getCurPage() - 1)
+ ")\"><font color=black><b>"
+ strPrevPage + "</b></font></a>\r\n\t");
strPages.append("\t<a href=\"javascript:gotoPage("
+ (getCurPage() + 1) + ")\"><font color=black><b>"
+ strNextPage
+ "</b></font></a><font face=webdings></font>\r\n\t");
strPages.append("\t<a href=\"javascript:gotoPage(" + getMaxPage()
+ ")\"><font color=black><b>" + strLastPage
+ "</b></font></a><font face=webdings>:</font>\r\n\t");
} else if (getMaxPage() > 1 && getCurPage() == getMaxPage()) {
strPages
.append("\t<font face=webdings></font><a href=\"javascript:gotoPage(1)\"><font color=black><b>"
+ strFirstPage + "</b></font></a>\r\n\t");
strPages
.append("\t<font face=webdings></font><a href=\"javascript:gotoPage("
+ (getCurPage() - 1)
+ ")\"><font color=black><b>"
+ strPrevPage + "</b></font></a>\r\n\t");
strPages
.append("\t<font color=#930909><b>"
+ strNextPage
+ "</b></font><font face=webdings color=#930909></font>\r\n\t");
strPages
.append("\t<font color=#930909><b>"
+ strLastPage
+ "</b></font><font face=webdings color=#930909>:</font>\r\n\t");
} strPages
.append("\t<font color=black><b>"
+ strGoto
+ "</b></font>\r\n\t\t<select name='pageSize' onchange='Jumping()'>"); for (int i = 1; i <= this.getMaxPage(); i++) {
if (i == this.getCurPage()) {
strPages.append("\r\n\t\t\t<option selected value=" + i + ">第 "
+ i + " 页</option>");
} else {
strPages.append("\r\n\t\t\t<option value=" + i + ">第 " + i
+ " 页</option>");
}
} strPages.append("\r\n\t\t</select>\r\n\t"); strPages
.append("\t<font color=black><b>"
+ strCurrentPage
+ ":</b></font><font color=#930909><b>"
+ getCurPage()
+ "</b></font>\r\n\t\t<font color=black><b>/</b></font>\r\n\t\t<font color=#930909><b>"
+ getMaxPage()
+ "</b></font><font color=black><b> 页 </b></font>"); strPages.append("\r\n\t\r\n</form>\r\n</table>"); return strPages.toString();
} /**
* <p>
* 根据Page 类的 target 属性和分页相关信息,获取前台页面提交表单和页面信息的 HTML 代码
* </p>
* <p>
* 注意:使用该方法之前,必须设置 Page 类的 target属性
* </p>
*
* @return - 前台页面提交表单和页面信息的 HTML 代码
*/
public String getPageStr() {
return this.getPageStr(target);
} /**
* 打印 Page 对象的当前信息
*/
public String toString() {
return "当前是第 " + curPage + " 页;共有 " + maxPage + " 页,共 " + maxRowCount
+ " 条记录(行);每页 " + rowsPerPage + " 条记录(行)";
}
}
package com.winwen.util;import java.util.List;/**
* @author lov1986 E-mail: [email protected]
* @version 创建时间:Aug 19, 2008 9:23:30 PM 类说明
*/
public class PageJudge { public Page pageJudge(List list, int pageSize, String servletname) {
Page page = new Page();
page.setTarget(servletname); //调用的 servlet名字
page.setFormName("pageform");
page.setRowsPerPage(12);//显示行数
page.setMaxRowCount(list.size());
page.setCurPage(pageSize);
page.setMaxPage(); // 判断数据是否正好显示
if (page.getMaxPage() == page.getCurPage()
&& page.getMaxRowCount() % page.getRowsPerPage() != 0) {
list = list.subList(
(page.getCurPage() - 1) * page.getRowsPerPage(), page
.getMaxRowCount());
} else {
list = list.subList(
(page.getCurPage() - 1) * page.getRowsPerPage(), page
.getCurPage()
* page.getRowsPerPage());
}
page.setList(list); return page;
}}
hibernate和数据库分页和显示有什么必然得联系吗?
import java.io.PrintWriter;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.winwen.util.Page;
import com.winwen.util.PageJudge;
import com.xinwen.dao.user.UserInfoDao;
import com.xinwen.pojo.user.UserInfo;
public class ShowUser extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response); } /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<UserInfo> userlist = new UserInfoDao().showUser(); //你从数据库里面读出来的数据
int pageSize = 1;
if (request.getParameter("pageSize") == null) {
pageSize = 1;
} else {
pageSize = Integer.parseInt(request.getParameter("pageSize"));
}
Page page = new PageJudge().pageJudge(userlist, pageSize,
"ShowUser");
request.setAttribute("showUsers", page);
request.getRequestDispatcher("user_manage/showUser.jsp").forward(
request, response);
}} - -额说我回复太快
[email protected]
如: Integer pageIndex = request.getParameter("pageIndex");
当前也可以取显示多少页,简单化不写了。 然后在 DAO 提供一个方法 , 第一个是当前页数,第二个是取得多少数据
如: List getList(int pageIndex,int pageSize){
// sql 语句大概就这样
sql = "select top pageSize from tb where id not in (select top pageIndex from tb)";
} servlet 根据界面来传入相应的参数进行调整调用 DAO 返回给 界面就 OK 了:
如: 表示每页显示 15 行
request.setParameter(DAO.getList(pageIndex,15 )) ; 页面显示就不用写了吧。
谢谢楼上的回答...我写了一个分页的PageBean,然后有个数据操作的DataBean,一个控制跳转的servlet,前台jsp显示
整理好了发到blob,欢迎大家评价(现在我有两种方式的分页,一个是前端时间写在servlet中,现在把他分出来了)
但不论你实用什么分页,你都要写一个分页的Bean