JSP+Servlet 实现分页(Tomcat 9)

news/2025/2/26 7:51:55

===========访问地址

项目路径/mulPageSystem

===========java文件;包名org.rain.bean 文件名 PageSystemBean

package org.rain.bean;

public class PageSystemBean {

private String school;

private String home;

public String getSchool() {

return school;

}

public void setSchool(String school) {

this.school = school;

}

public String getHome() {

return home;

}

public void setHome(String home) {

this.home = home;

}

}

===========java文件;包名org.rain.servlet 文件名 MulPage

package org.rain.servlet;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.ServletConfig;

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 org.rain.bean.PageSystemBean;

@WebServlet("/mulPageSystem")

public class MulPage extends HttpServlet {

private static final long serialVersionUID = 1L;

List<PageSystemBean> smallRainPageSystemBeans;

int smallRainTotalRecord=45;

int smallRainSize = 10;

int smallRainCurrent = 1;

@Override

public void init(ServletConfig config) throws ServletException{

smallRainPageSystemBeans=new ArrayList<PageSystemBean>(smallRainTotalRecord);

for(int i=0;i<smallRainTotalRecord;i++){

PageSystemBean bean=new PageSystemBean();

bean.setSchool("大学"+i);

bean.setHome("地址"+i);

smallRainPageSystemBeans.add(i, bean);

}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String smallRainpage = request.getParameter("smallRainpage");

request.getSession().setAttribute("URIRight", "right uri");

HttpSession sms=request.getSession();

if (smallRainpage != null) {

smallRainCurrent = Integer.parseInt(smallRainpage);

request.setAttribute("smallRainpage", smallRainCurrent);

}else {

request.setAttribute("smallRainpage", 1);

smallRainCurrent=1;

}

int smallRainDo = (smallRainCurrent - 1) * smallRainSize;

int totalPageBeans = smallRainPageSystemBeans.size();

int totalPages =totalPageBeans / smallRainSize;

totalPages++;

List<PageSystemBean> smallRainCurrentPageBeans;

if(totalPages==smallRainCurrent){

smallRainCurrentPageBeans = smallRainPageSystemBeans.subList(smallRainDo, totalPageBeans);

}else{

smallRainCurrentPageBeans = smallRainPageSystemBeans.subList(smallRainDo, smallRainDo+smallRainSize);

}

request.setAttribute("smallRainCurrentPageBeans", smallRainCurrentPageBeans);

request.setAttribute("smallRainCurrent", smallRainCurrent);

request.setAttribute("totalPages", totalPages);

request.setAttribute("smallRainURI", request.getServletPath().substring(1));

request.getRequestDispatcher("/MulPageSystemServlet.jsp").forward(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

}

==================jsp文件名 MulPageSystemServlet.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ page import="org.rain.bean.PageSystemBean,java.util.List,java.util.ArrayList" %>

<html>

<head>

<meta charset="UTF-8">

<title>晓雨 JSP+SERVLET 实现分页</title>

</head>

<body>

<h1> 晓雨 JSP+SERVLET 实现分页</h1>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr bgcolor="EFF6EFE">

<td width="50" height="45" ><b>学校名</b></td>

<td width="50" height="45" ><b>家乡名称</b></td>

</tr>

<%

Object smsession=request.getSession().getAttribute("URIRight");

Object smsessionRequest=request.getAttribute("smallRainpage");

if(null!=smsession&&smsessionRequest!=null){

int backColor=0;

List<PageSystemBean> beans=(List<PageSystemBean>)request.getAttribute("smallRainCurrentPageBeans");

for (PageSystemBean smallRainPageBean : beans) {

backColor++;

if(backColor%2==0){

%>

<tr bgcolor="EFFEEFE">

<%}else{%>

<tr >

<%}%>

<td width="50" height="35" ><%=smallRainPageBean.getSchool() %></td>

<td width="50" height="35" ><%= smallRainPageBean.getHome() %></td>

</tr>

<% } %>

<% } %>

</table>

<% Object smsessionTable=request.getSession().getAttribute("URIRight");

Object smsessionrequestTable=request.getAttribute("smallRainpage");

if(null!=smsessionTable&&smsessionrequestTable!=null){

%>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<b>

<%

String smallRainURI=(String)request.getAttribute("smallRainURI");

int smallRainCurrent=(int)request.getAttribute("smallRainCurrent");

int smallRaintotalPages=(int)request.getAttribute("totalPages");

if (smallRainCurrent > 1) { %>

<a href="<%=smallRainURI%>?smallRainpage=<%= smallRainCurrent - 1 %>">上一页</a>

<% } %>

<%

for (int i = 1; i <= smallRaintotalPages; i++) { %>

<% if (i == smallRainCurrent) { %>

<span><%= i %></span>

<% } else { %>

<a href="<%=smallRainURI%>?smallRainpage=<%= i %>"><%= i %></a>

<% } %>

<% } %>

<% if (smallRainCurrent < smallRaintotalPages) { %>

<a href="<%=smallRainURI%>?smallRainpage=<%= smallRainCurrent + 1 %>">下一页</a>

<% } %>

</b>

</table>

<% }

%>

</body>

</html>


http://www.niftyadmin.cn/n/5868336.html

相关文章

Vue的项目创建以及项目目录与组合式API

一.创建Vue 1.Vue-CLI:创建Vue的脚手架工具 2.Create-vue&#xff1a;是Vue官方提供的脚手架之一,底层采用官方自主研发的vite,快捷&#xff0c;开发方便。 3.准备工作:系统中需要安装nodejs环境&#xff0c;在该环境中提供npm包管理器 4.创建Vue项目的命令:npm init vuela…

最新前端框架选型对比与建议(React/Vue/Svelte/Angular)

前端框架选型对比与建议&#xff08;React/Vue/Svelte/Angular&#xff09; 一、核心框架技术特性对比&#xff08;基于最新版本&#xff09; 维度React 19 25Vue 3.5 12Svelte 5 25Angular 19 5核心理念函数式编程、JSX语法、虚拟DOM渐进式框架、组合式API、模板语法编译时框…

SQL注入(order by,limit),seacms的报错注入以及系统库的绕过

1&#xff1a;如果information_schema被过滤了&#xff0c;该怎么绕过 1.1&#xff1a;介绍一下information_schema这个库 information_schema 是一个非常重要的系统数据库&#xff0c;它在SQL标准中定义&#xff0c;并且被许多关系型数据库管理系统&#xff08;RDBMS&#x…

音视频编码和封装格式

文章目录 音视频编码格式音频编码视频编码 音视频封装格式 音视频编码格式 音频编码 音频编码是一种将音频信号转换为数字形式的过程&#xff0c;目的是减少数据中的冗余&#xff0c;以便存储和传输。 音频编码的实质是通过抽样、量化和编码三个步骤&#xff0c;将连续变化的…

爬虫运行后如何保存数据?

爬虫运行后&#xff0c;将获取到的数据保存到本地或数据库中是常见的需求。Python 提供了多种方式来保存数据&#xff0c;包括保存为文本文件、CSV 文件、JSON 文件&#xff0c;甚至存储到数据库中。以下是几种常见的数据保存方法&#xff0c;以及对应的代码示例。 1. 保存为文…

class绑定和style绑定

class绑定 简单对象绑定 <template> <div><p :class"{active:isActive,text-danger:hasError}">Class样式绑定1</p> </div> </template> <script> export default{data(){return{isActive:true,hasError:true,}} } <…

python-leetcode-每日温度

739. 每日温度 - 力扣&#xff08;LeetCode&#xff09; class Solution:def dailyTemperatures(self, temperatures: List[int]) -> List[int]:n len(temperatures)answer [0] * nstack [] # 存储索引for i, temp in enumerate(temperatures):while stack and temperat…

OceanBase + DeepSeek:5分钟免费搭建企业知识库

过去一个月&#xff0c;DeepSeek 在全球范围内引发了热烈讨论。其突破性的 AI 能力使其日流量显著超越 Claude 和 Perplexity&#xff0c;吸引了众多企业和技术专家的高度关注。随着 AI 技术的不断进步&#xff0c;企业正面临一场深刻的智能化变革——如何通过 AI 重构业务&…