jsp分页技术

博客针对初学jsp的人,介绍数据库信息在jsp上的分页查询实现方法。先建立数据存放界面,接收servlet返回结果,之后由servlet进行处理,还涉及数据库部分,解决一次性显示大量数据不美观的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很多初学jsp的小伙伴们,都为数据库的信息如何在jsp上面如何显着急,一次性显示数据过于庞大,不美观,无法突出程序员的设计感,
揭晓来,小编就为各位介绍一种分页查询的实现方法。
第一步建立数据存放的界面
st为servlet返回的结果

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link type="text/css" rel="stylesheet"
	href="http://localhost:8080/wust-find/ip/css/index.css" />
</head>
<body>
	<div class="table_div">
		<div class="div_clear">
			<div class="left_top"></div>
			<div class="center_top">
				<div style="float: left">
					<img src="./tab/images/tb.jpg" width="16px" height="16px"
						style="vertical-align: middle" /> <span style="font-weight: bold">你当前的位置</span>:[业务中心]-[失物招领]
				</div>
				<div style="float: right; padding-right: 6px">
					<img width='16' height='16'
						src="http://localhost:8080/wust-find/ip//tab/images/22.gif"
						style="vertical-align: middle" /> <a href="#">新增</a>&nbsp; <img
						width='16' height='16'
						src="http://localhost:8080/wust-find/ip//tab/images/33.gif"
						style="vertical-align: middle" /> <a href="#">修改</a>&nbsp; <img
						width='16' height='16'
						src="http://localhost:8080/wust-find/ip//tab/images/11.gif"
						style="vertical-align: middle" /> <a href="#">删除</a>
				</div>
			</div>
			<div class="right_top"></div>
		</div>
		<div class="div_clear">
			<div class="left_center"></div>
			<div class="center_center">
				<div class="table_content">
					<table cellspacing="0px" cellpadding="0px">
						<thead>
							<tr>
								<th width="16%">标题</th>
								<th width="16%">内容</th>
								<th width="16%">图片</th>

								<th width="20%" style="border-right: none">操作</th>
							</tr>
						</thead>
						<tbody>
							<c:forEach items="${student}" var="st">
								<tr>
									<td>${st.getBiaoti()}</td>
									<td>${st.getMiaoshu()}</td>
									<td><a href="${st.getAdress()}" target="_BLANK">点击查看</a></td>
									<td width="20%" style="border-right: none"><img width='16'
										height='16'
										src="http://localhost:8080/wust-find/ip//tab/images/del.gif"
										style="vertical-align: middle" /> <a href="#">修改</a>&nbsp; <img
										width='16' height='16'
										src="http://localhost:8080/wust-find/ip//tab/images/edt.gif"
										style="vertical-align: middle" /> <a href="#">删除</a></td>
								</tr>
							</c:forEach>
							<tr>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
								<td>&nbsp;</td>

								<td>&nbsp;</td>
								<td>&nbsp;</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
							</tr>
							</br>
						</tbody>
					</table>
				</div>
			</div>
			<div class="right_center"></div>
		</div>
		<div class="div_clear">
			<div class="left_bottom"></div>
			<div class="center_bottom">
				 <span>&nbsp;&nbsp;共 ${pagenum}页 当前 第${page}页</span>  
				<div style="float: right; padding-right: 30px">
					<c:choose>
						<c:when test="${page>1}">
							<a href="Losty?page=${page-1}"><input type="button"
								value="上一页"></a>
						</c:when>
						<c:otherwise>
							<input type="button" value="上一页" disabled="disabled" />
						</c:otherwise>
					</c:choose>
					<c:choose>
						<c:when test="${page!=pagenum}">
							<a href="Losty?page=${page+1}"><input type="button"
								value="下一页"></a>
						</c:when>
						<c:otherwise>
							<input type="button" value="下一页" disabled="disabled" />
						</c:otherwise>
					</c:choose>
					 
					<form action="Losty">
						 <input type="text" name="page" size="1" />  <input type="submit"
							value="跳转" />
					</form>
					 
				</div>
			</div>
			<div class="right_bottom"></div>
		</div>
		 
	</div>

</body>
</html>

接下来是servlet进行处理




package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.lost;
import userdao.Lost;
import userdao.userdao;

@WebServlet("/Losty")
public class Losty extends HttpServlet {
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String page = null;
        String loginname=request.getParameter("loginname");
        page = request.getParameter("page");
        if(page == null || page=="")
            page = "1";
        Lost studao = new Lost();
        request.setAttribute("student",studao.findByPage(Integer.parseInt(page)));
        request.setAttribute("pagenum",studao.userCount()/5+1);//总页数
        request.setAttribute("loginname", loginname);
        request.setAttribute("page", page);//当前页
        request.getRequestDispatcher("ip/index.jsp").forward(request, response);    
        
    }
 }

数据库部分

public ArrayList<lost> findByPage(int page){
	conn=getConnectionn();
        int begin = (page-1) * 5;//每页数据量
        String sql = "select * from lost limit "+begin+",5";
        ResultSet rs = executeQuery(sql,null);
        ArrayList<lost> list = new ArrayList<lost>();
        try {
            while(rs.next()){
                lost st = new lost();
                st.setBiaoti(rs.getString("biaoti"));
                st.setMiaoshu(rs.getString("miaoshu"));
                st.setAdress(rs.getString("adress"));
                list.add(st);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{ close(); } 
        return list;
    }
    public int userCount(){
    	conn=getConnectionn();
        String sql = "select count(*) from lost";//lost为表名
        ResultSet rs =executeQuery(sql, null);
        int count = 0;
        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{ close(); } 
        return count;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值