jsp+servlet+javabean (MVC)分页

  最近遇到了分页的问题,以前说真的没仔细研究过了,这几天仔细高了一下,基本原理高清楚了,上次我发了一个jsp的分页程序了,其实那个还有很多地方完善,这两天总结了一下,用MVC摸式改进了一下,还是强调一点我一直都觉的JSP不要在页面写代码,因为影响分工了,所以我一直在编程都努力的在做了,但是这些代码我只是完成了功能,还有代码优化的地方,我没去做,这个批评一下自己了,不过因为最近真的好多事了,说真的由于我的电脑硬盘坏了,好多东西我都从头来学,真的好郁闷了,其实这些我以前都做出来了,但是电脑挂了,没办法了,从头来啊!不乱砍了,首先是我的页面代码如下:

<%@ page language="java" import="java.util.*,java.sql.*"
 pageEncoding="gb2312"%>
 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

 
 
 


  
        ${news2.newsTitle}
          
 

      
              首页
              上一页
             

              
              下一页
              最后一页
             

              第${showpage}页
              一共${pagecount}页
 
 

 

servlet代码如下:

package org.servlet;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.util.ContactBean;
import org.util.PageBean;

public class PageServlet extends javax.servlet.http.HttpServlet {

 public void doGet(HttpServletRequest

 request, HttpServletResponse response)
   throws javax.servlet.ServletException, java.io.IOException {
  response.setContentType("text/html");
  try {
   PageBean pagebean = new PageBean();
   ContactBean contact = new ContactBean();
   int pagecount = pagebean.countPage();//获取总共的页数
   String topage = request.getParameter("topage");//获取当前的页码
   if (Integer.parseInt(topage) > pagecount) {  //判断当前的页码是否越界,如果是的话就进行处理
    topage =String.valueOf(pagecount);
   } else if (Integer.parseInt(topage) <= 0) {
    topage = "1";
   }
   ArrayList news = contact.getData(topage, pagecount);
   request.getSession().setAttribute("pagecount", pagecount);
   request.getSession().setAttribute("showpage", topage);//将当前的页码返回给页面,这样进行计算
   request.getSession().setAttribute("news1", news);
   response.sendRedirect("test1.jsp");
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)

 throws javax.servlet.ServletException, java.io.IOException {
  doGet(request, response);
 }
}

javabean代码如下:

package org.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;

import org.bean.NewsBean;

public class ContactBean {
 private Connection conn;
 public Connection getConnection() {
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager
     .getConnection(
       "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gbk",
       "root", "123456");
  } catch (SQLException ex) {
   System.out.println(ex.getMessage() + "路径错误");
  } catch (ClassNotFoundException ex) {
   System.out.println(ex.getMessage() + "驱动错误");
  }
  return conn;
 }

 /** *返回要查询的记录数 */
 public int getAvailableCount() throws Exception {
  int ret = 0;
  String strSql = "select count(*) from news";
  Connection conn = this.getConnection();
  Statement stmt = conn.createStatement();
  ResultSet rset = stmt.executeQuery(strSql);
  while (rset.next()) {
   ret = rset.getInt(1);
  }
  return ret;
 }
    /*获取查寻的记录数*/
 public ArrayList getData(String topage, int pagecount)
   throws NumberFormatException, SQLException {
  String sql = "select * from news";
  Statement state = null;
  ResultSet rs = null;
  int pagesize = 3;
  int showpage = 1;
  ArrayList list = new ArrayList();
  int j = 0;
  try {
   Connection conn = this.getConnection();
   state = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
     ResultSet.CONCUR_READ_ONLY);
   rs = state.executeQuery(sql);
  } catch (Exception e) {
   System.out.println("exception");
  }
  if (!rs.next()) {
   System.out.println("no records!");
  } else {
   rs.last();
   if (topage != null) {
    showpage = Integer.parseInt(topage);
    if (showpage > pagecount) {
     showpage = pagecount;
    } else if (showpage <= 0) {
     showpage = 1;
    }
   }
   rs.absolute((showpage - 1) * pagesize + 1);
   for (int i = 1; i <= pagesize; i++) {
    NewsBean news = new NewsBean();
    news.setNewsContent(rs.getString(2));
    news.setNewsTitle(rs.getString(3));
    list.add(j, news);
    ++j;
    if (!rs.next())
     break;
   }

  }
  return list;
 }

}

package org.util;


public class PageBean {
 public int maxPage; // 一共有多少页
 public int maxRowCount; // 一共有多少行
 public int pagesize = 3; // 每页多少行
 public PageBean() {
 }

 public int countPage() throws Exception {
 int maxRowCount=this.MaxCount();
  if (maxRowCount % this.pagesize == 0) {
   maxPage = maxRowCount / this.pagesize;
  } else {
   maxPage =maxRowCount/ this.pagesize + 1;
  }
  return maxPage;
 }


 public int MaxCount() throws Exception {
  ContactBean contact=new ContactBean();
  this.maxRowCount = contact.getAvailableCount(); // 得到总行数
        return maxRowCount;
 }

}
其实这两个JAVABEAN可以写成一个了,并且很多代码可以提出来,做成一个复用性强的代码了,这里我结合我这几天上网查资料的感受,那些分页多半是只能作者能用了,读者就用不了。所以我这里写了全部代码了,也不知道别人看不看的懂了!

你可能感兴趣的:(java基础)