jsp用户登陆分页显示

本文介绍了一个简单的分页查询实现方案,包括如何计算总页数、当前页数及展示数据的具体步骤。通过设定每页显示记录数量,结合用户请求的页码,从数据库中获取相应范围的数据,并提供上下页导航。

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

分页算法:

pageSize指定的,每页显示多少条记录

pageNow是用户选择的,希望显示第几页

rowCount是从表中查询得到,一共多少条记录

pageCount计算出来的,一共多少页

//计算pageCount

 <pre name="code" class="java"> if(rowCount%pageSize==0){
 pageCount=rowCount/pageCount;
  }else{
 pageCount=rowCount/pageCount+1;
  }


</pre><pre name="code" class="java"></pre>wel.jsp<pre name="code" class="html"><%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'wel.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   登陆成功!<%=request.getParameter("user") %> <br>
   <a href="login.jsp">返回重新登录</a>
   <hr>
   <h1>用户列表</h1>
   <%
    //
    int pageSize=2;
    int pageNow=1;//默认第一页
    int rowCount=0;
    int pageCount=0;
    //接受用户希望现实的页数(pageNow)
    String s_pageNow=request.getParameter("pageNow");
    if(s_pageNow!=null){
    //
    pageNow=Integer.parseInt(s_pageNow);
    }
    //加载驱动
  Class.forName("com.mysql.jdbc.Driver");
  //获得连接
  Connection ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/myDB","root","123");
  //创建statement对象
  Statement stmt=ct.createStatement();
  //
  ResultSet rs=stmt.executeQuery("select count(*) from login");
  //注意!
  if(rs.next()){
  rowCount=rs.getInt(1);
  }
  //计算pageCount
  if(rowCount%pageSize==0)
	  {
	  pageCount=rowCount/pageSize;
	  }else{
  pageCount=rowCount/pageSize+1;
  }
  //查询需要显示的记录mysql
  rs=stmt.executeQuery("select * from login limit " + (pageNow - 1)  
                    * pageSize + "," + pageSize);
    //显示
    %>
    <table border="1">
    <tr>
    <td>用户id</td>
    <td>用户名字</td>
    <td>用户密码</td>
    </tr>  
    <%
    while(rs.next()){
    %>
    <tr><td><%=rs.getInt(1) %></td>
    <td><%=rs.getString(2) %></td>
    <td><%=rs.getString(3) %></td></tr>
   <% 
   }  
   %>  
    </table>
    <%
    
    //超链接
    if(pageNow!=1){
    out.println("<a href=wel.jsp?pageNow="+(pageNow-1)+">上一页</a>");
    }
    for(int i=1;i<=pageCount;i++){
    out.println("<a href=wel.jsp?pageNow="+i+">["+i+"]</a>");
    }
    //如果是最后一页,不能执行最后一页
   
    if(pageNow!=pageCount){
    out.println("<a href=wel.jsp?pageNow="+(pageNow+1)+">下一页</a>");
    }
     %>
   
  </body>
</html>


loginCl.jsp

<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'loginCl.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <%
  //接受用户名和密码
  String u=request.getParameter("username");
  String p=request.getParameter("passwd");
  //数据库中验证
  //加载驱动
  Class.forName("com.mysql.jdbc.Driver");
  //获得连接
  Connection ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/myDB","root","123");
  //创建statement对象
  Statement stmt=ct.createStatement();
  //
  ResultSet rs=stmt.executeQuery("select passwd from login where username='"+u+"'and passwd='"+p+"'");
  if(rs.next()){
  //简单验证
  if(rs.getString(1).equals(p)){  
  //
  response.sendRedirect("wel.jsp?username="+u);
	  }else{
	  //密码不合法
	  response.sendRedirect("login.jsp?errNo=1");
	  }
  }else{
  //用户名不合法
   response.sendRedirect("login.jsp?errNo=2");
  }
   %>
    <br>
  </body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值