码迷,mamicode.com
首页 > Web开发 > 详细

jsp:使用jsp完成数据的分页显示

时间:2018-01-07 14:26:40      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:oge   des   generated   class   forward   connect   ref   writer   []   

一,使用jsp将数据库内的数据。分页显示到页面

1,创建一个servelt处理后台数据,和数据库的数据

private static final long serUid=1L;
    //加载驱动
    static final String jdbc="com.mysql.jdbc.Driver";
    //要连接的数据库url
    static final String db_url="jdbc:mysql://localhost:3306/test";
    //数据库用户名
    static final String user="db";
    //数据库密码
    static final String pass="1743721";
    int currentPage=1;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
        Connection conn=null;
        Statement stmt=null;
        //显示的数据的格式
        response.setContentType("text/html;charset=UTF-8");
        
        PrintWriter out =response.getWriter();
        try {
            Class.forName(jdbc);
            conn=DriverManager.getConnection(db_url,user,pass);
            stmt=conn.createStatement();
            String sql=null;
            sql="select * from t_message where 1=1 order by s_date desc";
            int pageSize=5;
            int d=3;
            String re=null;
        //接收url中的参数 re
=request.getQueryString(); //System.out.print(re); if(re!=null){ String[] ee=re.split("="); if("cupg".equals(ee[0])){ re=ee[1]; int r=Integer.valueOf(re).intValue(); d=r; currentPage=r; }else{ re=ee[1]; int bt=Integer.valueOf(re).intValue(); currentPage=bt; if(bt<=3){ d=3; }else{ d=bt; } } } if(currentPage<=0){ d=3; currentPage=1; } int start=(currentPage-1)*pageSize;
        //获取数据进行分页处理 String limitsql
="select * from("+sql+")pp limit "+start+","+pageSize; ResultSet rs=stmt.executeQuery(limitsql); List<User> userlist=new ArrayList<User>(); User user=null; while(rs.next()){
        //创建一个对象存入数据 user
=new User(); user.setMessage_id(rs.getInt("message_id")); user.setTitle(rs.getString("title")); user.setName(rs.getString("name")); user.setS_date(rs.getString("s_date")); user.setS_des(rs.getString("s_des")); user.setL_des(rs.getString("l_des")); userlist.add(user); } request.setAttribute("nubtn",d); request.setAttribute("cupg", currentPage); request.setAttribute("args", userlist); request.getRequestDispatcher("/qh/message.jsp").forward(request, response); //关闭通道 rs.close(); conn.close(); stmt.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }

2,在页面接收并处理得到的数据

    <div class="column">
        <h1 class="column-tit"><span><a href="#" title="首页">首页</a> > <a href="#" title="留言回复">留言回复</a></span>留言回复</h1>
        <div class="mailbox mt20">

                   <% ArrayList userlist=(ArrayList)request.getAttribute("args");
              //遍历的到的数据
for(int i=0;i<userlist.size();i++){ User user=(User)userlist.get(i); %> <div class="online-content clearfix"> <div class="icon fl"> <img src="../images/online.png" alt=""> </div> <div class="question fr"> <p>留言主题:<%=user.getTitle()%> </p> <p class="name"><span>网友:<%=user.getName()%></span><span><%=user.getS_date()%></span><span><a target="_blank" href="#">我要留言</a></span></p> <div class="message-i "> <p><strong>留言详情:</strong><%=user.getS_des()%></p> </div> <div class="answer"> <p><strong>执法监察总队回复:</strong><%=user.getL_des()%></p> </div> </div> </div> <% } %> <div class="page">
          
            //分页按钮的处理 <%int currentPage=(Integer)request.getAttribute("cupg"); int cugg=3; Integer d=0; int nubtn=(Integer)request.getAttribute("nubtn"); if(nubtn>=3)cugg=nubtn; String a=request.getQueryString(); if(a!=null){ String[] ee=a.split("="); a=ee[1]; d=Integer.parseInt(a); } %> <div style="display: none;" id="yc"><%=a%></div> <ul id="mu"><li class="num"><a href="/MysqlTest/servlet/GetMysql?cupg=1">首页</a></li> <li class="num"><a href="/MysqlTest/servlet/GetMysql?cupg=<%=currentPage-1%>">上一页</a></li> <li class="num page-active"><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg-2%>"><%=cugg-2%></a></li> <li class="num" style=""><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg-1%>"><%=cugg-1 %></a></li> <li class="num"><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg %>"><%=cugg %></a></li> <li class="num" ><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg+1%>"><%=cugg+1%></a></li> <li class="num"><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg+2%>"><%=cugg+2%></a></li> <li class="page-speciall">...</li><li class="num">34</li> <li class="num"><a href="/MysqlTest/servlet/GetMysql?cupg=<%=currentPage+1%>" >下一页</a></li> <li class="num"><a href=>尾页</a></li></ul> </div> </div> </div>

 

jsp:使用jsp完成数据的分页显示

标签:oge   des   generated   class   forward   connect   ref   writer   []   

原文地址:https://www.cnblogs.com/dybe/p/8227638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!