DAO类(后续无需改变)

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.Category;
import util.DBUtil; public class CategoryDAO {
public int getTotal(){
String sql="select count(*) from category";
int total=0;
try(Connection c=DBUtil.getConnection();
Statement s=c.createStatement();) {
ResultSet rs=s.executeQuery(sql);
while(rs.next()){
total=rs.getInt(1);
}
} catch (Exception e) {
// TODO: handle exception
}
return total;
} public List<Category> list(int start,int count){
List<Category> categories=new ArrayList<Category>();
String sql="select * from category order by id limit ?,?";
try (Connection c=DBUtil.getConnection();
PreparedStatement ps=c.prepareStatement(sql);){
ps.setInt(1, start);
ps.setInt(2, count);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Category category=new Category();
category.setId(rs.getInt(1));
category.setName(rs.getString(2));
categories.add(category);
}
} catch (Exception e) {
// TODO: handle exception
}
return categories;
}
}

效果一:列表全显示

Servlet类

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import bean.Category;
import dao.CategoryDAO; public class CategoryListServlet extends HttpServlet{
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
List<Category>categories=new CategoryDAO().list(0,Short.MAX_VALUE【可变】);
request.setAttribute("categories【可变】", categories);
request.getRequestDispatcher("/admin/category/categoryList.jsp【可变】").forward(request, response);
}
}

JSP代码

<table class="table table-striped  table-bordered table-hover  table-condensed">
<thead>【下面td内容可变】
<tr class="success">
<td>ID</td>
<td>图片</td>
<td>分类名称</td>
<td>编辑</td>
<td>删除</td>
</tr>
</thead>
<tbody>
<c:forEach items="${categories【可变,取决前面servlet】}" var="categorie【可变】">
<tr>
<td>${categorie.id【可变】}</td>
<td><img height="40px" src="img/category/${categorie.id【可变】}.jpg"></td>
<td>${categorie.name【可变】}</td>
<td><a href="categoryEdit?id=${categorie.id【可变】}"><span class="glyphicon glyphicon-edit"></span></a></td>
<td><a href="categoryDelete?id=${categorie.id【可变】}"><span class="glyphicon glyphicon-remove"></span></a></td>
</tr>
</c:forEach>
</tbody>
</table>

效果二:只显示前5条数据

Servlet类

public class CategoryListServlet extends HttpServlet{
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
int start=0;//【可变】
int count=5;//【可变】
List<Category>categories=new CategoryDAO().list(start,count); request.setAttribute("categories【可变】", categories);
request.getRequestDispatcher("/admin/category/categoryList.jsp").forward(request, response);
}
}

JSP代码

//和上一个效果图的JSP代码一样

效果三:可显示首页和末页

Servlet类

public class CategoryListServlet extends HttpServlet{
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
int start=0;//起始页的start值
int count=5;//【可变】 try {//如果请求中不带参数start,必须要进行try块的错误处理
start=Integer.parseInt(request.getParameter("start"));
} catch (NumberFormatException e) {
// TODO: handle exception
}
int total=new CategoryDAO().getTotal();//总数
int last=0;//末页的start值
if(0==total%count)
last=total - count;
else
last=total - total % count; List<Category>categories=new CategoryDAO().list(start,count);//【可变】
request.setAttribute("last", last);//把last传递给jsp
request.setAttribute("categories【可变】", categories);//把categories传递给jsp
request.getRequestDispatcher("/admin/category/categoryList.jsp【可变】").forward(request, response);
}
}

JSP代码

<table class="table table-striped  table-bordered table-hover  table-condensed">
<thead>【可变】
<tr class="success">
<td>ID</td>
<td>图片</td>
<td>分类名称</td>
<td>编辑</td>
<td>删除</td>
</tr>
</thead>
<tbody>
<c:forEach items="${categories【可变】}" var="categorie【可变】">
<tr>
<td>${categorie.id【可变】}</td>
<td><img height="40px" src="img/category/${categorie.id【可变】}.jpg"></td>
<td>${categorie.name【可变】}</td>
<td><a href="【可变】categoryEdit?id=${categorie.id【可变】}"><span class="glyphicon glyphicon-edit"></span></a></td>
<td><a href="【可变】categoryDelete?id=${categorie.id【可变】}"><span class="glyphicon glyphicon-remove"></span></a></td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td colspan="6" align="center">
<!-- 点击超链接时,相当于提交了一个带有参数start的请求 -->
<a href="categoryList【可变】?start=0">[首页]</a>
<a href="categoryList【可变】?start=${last}">[末页]</a>
</td>
</tr>
</tfoot>
</table>

效果四:可显示首页和末页、下一页和上一页

Servlet类

public class CategoryListServlet extends HttpServlet{
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
int start=0;//起始页的start值
int count=5;//【可变】 try {//如果请求中不带参数start,必须要进行try块的错误处理
start=Integer.parseInt(request.getParameter("start"));
} catch (NumberFormatException e) {
// TODO: handle exception
}
int total=new CategoryDAO().getTotal();//总数
int last=0;//末页的start值
if(0==total%count)
last=total - count;
else
last=total - total % count; int pre=start-count;//上一页的start值
pre=pre>0?pre:0;//边界处理
int next=start+count;//下一页的start值
next=next<last?next:last;//边界处理 List<Category>categories=new CategoryDAO().list(start,count);//【可变】
request.setAttribute("pre", pre);//把last传递给jsp
request.setAttribute("next", next);//把last传递给jsp
request.setAttribute("last", last);//把last传递给jsp
request.setAttribute("categories【可变】", categories);//把categories传递给jsp
request.getRequestDispatcher("/admin/category/categoryList.jsp【可变】").forward(request, response);
}
}

JSP代码

<table class="table table-striped  table-bordered table-hover  table-condensed【可变】">
<thead>
<tr class="success【可变】">
<td>ID</td>
<td>图片</td>
<td>分类名称</td>
<td>编辑</td>
<td>删除</td>
</tr>
</thead>
<tbody>
<c:forEach items="${categories【可变】}" var="categorie【可变】">
<tr>
<td>${categorie.id【可变】}</td>
<td><img height="40px" src=【可变】"img/category/${categorie.id}.jpg"></td>
<td>${categorie.name【可变】}</td>
<td><a href=【可变】"categoryEdit?id=${categorie.id}"><span class="glyphicon glyphicon-edit"【可变】></span></a></td>
<td><a href=【可变】"categoryDelete?id=${categorie.id}"><span class="glyphicon glyphicon-remove"【可变】></span></a></td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td colspan="6" align="center">
<!-- 点击超链接时,相当于提交了一个带有参数start的请求 -->
<a href="categoryList?start=0">[首页]</a>
<a href="categoryList?start=${pre}">[上一页]</a>
<a href="categoryList?start=${next}">[下一页]</a>
<a href="categoryList?start=${last}">[末页]</a>
</td>
</tr>
</tfoot>
</table>

效果五:可显示有序页

Servlet类

public class CategoryListServlet extends HttpServlet{
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
int start=0;//起始页的start值
int count=5;//【可变】 try {//如果请求中不带参数start,必须要进行try块的错误处理
start=Integer.parseInt(request.getParameter("start"));
} catch (NumberFormatException e) {
// TODO: handle exception
}
int total=new CategoryDAO().getTotal();//总数
int last=0;//末页的start值
if(0==total%count)
last=total - count;
else
last=total - total % count; int page=0;
if(0==total%count)
page=total % count;
else
page=total % count+1; List<Category>categories=new CategoryDAO().list(start,count);//【可变】
request.setAttribute("last", last);//把count传递给jsp
request.setAttribute("page", page);//把page传递给jsp
request.setAttribute("count", count);//把count传递给jsp
request.setAttribute("categories【可变】", categories);//把categories传递给jsp
request.getRequestDispatcher("/admin/category/categoryList.jsp【可变】").forward(request, response);
}
}

JSP代码

<table class=【可变】"table table-striped  table-bordered table-hover  table-condensed">
<thead>【可变】
<tr class=【可变】"success">
<td>ID</td>
<td>图片</td>
<td>分类名称</td>
<td>编辑</td>
<td>删除</td>
</tr>
</thead>
<tbody>
<c:forEach items="${categories【可变】}" var="categorie【可变】">
<tr>
<td>${categorie.id【可变】}</td>
<td><img height="40px" src=【可变】"img/category/${categorie.id}.jpg"></td>
<td>${categorie.name【可变】}</td>
<td><a href=【可变】"categoryEdit?id=${categorie.id}"><span class="glyphicon glyphicon-edit"></span></a></td>
<td><a href=【可变】"categoryDelete?id=${categorie.id}"><span class="glyphicon glyphicon-remove"></span></a></td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td colspan="6" align="center">
<!-- 点击超链接时,相当于提交了一个带有参数start的请求 -->
<a href=【可变】"categoryList?start=0">[1]</a>
<c:forEach var="i" begin="2" end="${page-2}">
<a href=【可变】"categoryList?start=${(i-1)*count}">[${i}]</a>
</c:forEach>
<a href=【可变】"categoryList?start=${last}">[${page-1}]</a>
</td>
</tr>
</tfoot>
</table>

效果五:可显示首页、上一页、中间页、下一页、末页

Servlet类

public class CategoryListServlet extends HttpServlet{
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
int start=0;//起始页开始值
int count=5;//【可变】 try {//如果请求中不带参数start,必须要进行try块的错误处理
start=Integer.parseInt(request.getParameter("start"));
} catch (NumberFormatException e) {
// TODO: handle exception
}
int total=new CategoryDAO().getTotal();//总数
int last=0;//末页开始值
if(0==total%count)
last=total - count;
else
last=total - total % count; int pre=start-count;//上一页开始值
pre=pre>0?pre:0;//边界处理
int next=start+count;//下一页开始值
next=next<last?next:last;//边界处理 int page=0;//总页数
if(0==total%count)
page=total % count;
else
page=total % count+1; List<Category>categories=new CategoryDAO().list(start,count);//【可变】
request.setAttribute("pre", pre);//把pre传递给jsp
request.setAttribute("next", next);//把next传递给jsp
request.setAttribute("last", last);//把last传递给jsp
request.setAttribute("page", page);//把page传递给jsp
request.setAttribute("count", count);//把count传递给jsp
request.setAttribute("categories【可变】", categories【可变】);//把categories传递给jsp
request.getRequestDispatcher("/admin/category/categoryList.jsp"【可变】).forward(request, response);
}
}

JSP代码

<tfoot>
<tr>
<td colspan="6" align="center">
<!-- 点击超链接时,相当于提交了一个带有参数start的请求 -->
<a href=【可变】"categoryList?start=0">[首页]</a>
<a href="categoryList?start=${pre}">[上一页]</a>
<!-- 中间页 -->
<a href=【可变】"categoryList?start=0">[1]</a>
<c:forEach var="i" begin="2" end="${page-2}">
<a href=【可变】"categoryList?start=${(i-1)*count}">[${i}]</a>
</c:forEach>
<a href=【可变】"categoryList?start=${last}">[${page-1}]</a>
<!-- 中间页 -->
<a href=【可变】"categoryList?start=${next}">[下一页]</a>
<a href=【可变】"categoryList?start=${last}">[末页]</a>
</td>
</tr>
</tfoot>

模板:分页JSP(结合Servlet)的更多相关文章

  1. 修改 MyEclipse 中的 jsp 和 servlet 模板

    找到  MyEclipse/Common/plugins/com.genuitec.eclipse.wizards_9.0.0.me201211011550.jar 这个文件(wizards后面的数字 ...

  2. jsp学习--如何定位错误和JSP和Servlet的比较

    一.如何查找JSP页面中的错误  JSP页面中的JSP语法格式有问题,导致其不能被翻译成Servlet源文件,JSP引擎将提示这类错误发生在JSP页面中的位置(行和列)以及相关信息.JSP页面中的JS ...

  3. Jsp与servlet之间页面跳转及参数传递实例(转)

    原网址:http://blog.csdn.net/ssy_shandong/article/details/9328985 11. jsp与servlet之间页面跳转及参数传递实例 分类: Java ...

  4. 图解JSP与Servlet的关系

      Servlet是Java提供的用于开发Web服务器应用程序的一个组件,运行在服务器端,由Servlet容器所管理,用于生成动态的内容.Servlet是平台独立的Java类,编写一个Servlet, ...

  5. SpringMVC内容略多 有用 熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器、过滤器等Web组件以及MVC架构模式进行Java Web项目开发的经验。

    熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器.过滤器等Web组件以及MVC架构 ...

  6. 快速分页:jsp标签pager-taglib

    一:简介 Pager-taglib,支持多种风格的分页显示.实际上它是一个Jsp标签库,为在JSP上显示分页信息而设计的一套标签,通过这些标签的不同的组 合,会形成多种不一样的分页页面,风格各异.它既 ...

  7. JSP(Java Servlet Page)

    一.简介 HTML HTML擅长显示一个静态的网页,但是不能调用Java程序. Servlet Servlet擅长调用Java程序和后台进行交互,但是它不擅长显示一个完整的HTML页面. 我们希望创建 ...

  8. 【转】(超详细)jsp与servlet之间页面跳转及参数传递实例

    初步学习JavaEE,对其中jsp与Servlet之间的传值没弄清楚,查看网上资料,发现一篇超详细的文章,收获大大,特此记录下来.具体链接:http://blog.csdn.net/ssy_shand ...

  9. JSP和Servlet的中文乱码处理

    JSP和Servlet的中文乱码处理 前几天学习了JSP和Servlet中有关中文乱码的一些问题,写成了博客,今天进行更新一下.应该是可以解决日常的乱码问题了.现在作以下总结希望对需要的人有所帮助.我 ...

  10. 一、JSP、servlet、SQL三者之间的数据传递(前台与后台数据交互)

    背景: 目前业界很流行的MVC(model-view-control)开发模式,理解为 模型是Bean, 视图是 Html/Jsp, 控制是Servlet, 关联数据库的Dao web的运行机制: 数 ...

随机推荐

  1. js 实现图片瀑布流效果,可更改配置参数 带完整版解析代码[waterFall.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS实现图片瀑布流效果 页面需求 1 ...

  2. CSS样式2

    1.css重用 <style> .cl{ ... } .c2{ ... } .c{ ... } </style> <div class='c c2'></di ...

  3. CYQ.Data 轻量数据层之路 使用篇-MProc 存储过程与SQL 视频[最后一集] H (二十八)

    2019独角兽企业重金招聘Python工程师标准>>> 说明: 本次录制主要为使用篇:CYQ.Data 轻量数据层之路 使用篇五曲 MProc 存储过程与SQL(十六)   的附加视 ...

  4. rabbitMQ消息队列原理

    MQ:Message Queue,消息队列,是一种应用程序对应用程序的通信方法:应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们. 1      rabbitMQ入 ...

  5. Linux从入门到精通系列之NFS

    网络文件系统(NFS,Network File System)是一种将远程主机上的分区(目录)经网络挂载到本地系统的一种机制,通过对网络文件系统的支持,用户可以在本地系统上像操作本地分区一样来对远程主 ...

  6. winform练习-通过遍历Control容器中的对象统一委托事件-楼盘选择器

    1.窗体布局如下,一个label标签内容如下,一个btnSave按钮,用于保存,其他九个按钮用于选择楼盘. 2. 按钮存于Control容器中,编写方法遍历容器中的button,通过条件过滤掉不是bu ...

  7. CodeForces - 1245 B - Restricted RPS(贪心)

    Codeforces Round #597 (Div. 2) Let nn be a positive integer. Let a,b,ca,b,c be nonnegative integers ...

  8. 从零开始制作数据集所需要的所有python脚本

    最近一直在做图片数据集,积累了很多心得.我把我所使用的python脚本全部拿出来,当然这些脚本大部分网上都有,只不过比较分散. 我已经把所有代码上传到github上,觉得写的好的话,请给我一个star ...

  9. 禅道部署(基于 Linux)

    1. 查看 Linux 服务器是 32位 还是 64位 的 getconf LONG_BIT 2. 禅道开源版安装包下载 下载站点1:# wget http://sourceforge.net/pro ...

  10. spark系列-8、Spark Streaming

    参考链接:http://spark.apache.org/docs/latest/streaming-programming-guide.html 一.Spark Streaming 介绍 Spark ...