今天写了个jsp的分页程序,以前在做jsp项目的时候,分页采用的是“首页,尾页,上一页,下一页”的形式,虽然分页没有问题,但总感觉不够友好,现在很多论坛都采用这种模式,即根据用户请求的页面,列出请求页面和该页面的前几页和后几页,看上去比较舒服,今天就模仿“编程中国论坛”的分页风格练了练,后台查询数据库的分页代码与之前做过的基本没有变化,主要是在jsp页面上多了些判断,效果实现了,至于效率我就不好说了,如果哪位看过下面代码的朋友有什么好方法,还望大家能够一起交流,共同进步。
该程序采用了MVC设计模式,代码中的ServletX为总控制器,根据model值将请求转至相应模块,后台数据库为Oracle,由于emp表中数据只有15条,所以我每页只显示一条记录,效果如图:

1,用户登录,若登录成功转到main.jsp页面,以下为用户控制器代码:
package controls;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import entitys.User;
import routines.Translation;
import operater.*;
import java.util.*;


public class UserServlet extends HttpServlet
{


/** *//**
* Constructor of the object.
*/

public UserServlet()
{
super();
}


/** *//**
* Destruction of the servlet. <br>
*/

public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/** *//**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException
{
String event = request.getParameter("event");

if(event.equals("login"))
{
String userName = Translation.transCode(request.getParameter("userName"));
String password = Translation.transCode(request.getParameter("password"));
User user = new User();
user.setUserName(userName);
user.setPassword(password);
OperUser obj = new OperUser();

if(obj.isExist(user))
{
this.mySet(request,"1");
request.getRequestDispatcher("main.jsp").forward(request, response);
return;

}else
{
System.out.println("失败");
}
}
}

private void mySet(HttpServletRequest request,Object pageNo)
{
OperEmp emp = new OperEmp();
//向请求中存储关于分页的信息
ArrayList aryInfo = emp.getEmp(pageNo);
request.setAttribute("data", aryInfo.get(0));
request.setAttribute("pageNo", aryInfo.get(1));
request.setAttribute("pageCount", aryInfo.get(2));
request.setAttribute("rowsCount", aryInfo.get(3));
ArrayList aryNumber = new ArrayList();

for(int i=1;i<=10;i++)
{
aryNumber.add(new Integer(i));
}
request.setAttribute("number", aryNumber);
ArrayList aryNumberR = new ArrayList();

for(int i=9;i>=0;i--)
{
aryNumberR.add(new Integer(i));
}
request.setAttribute("numberR", aryNumberR);
//向请求中存储关于职位的信息
ArrayList aryJob = emp.getJob();
request.setAttribute("job", aryJob);
//向请求中存储关于部门编号的信息
OperDept dept = new OperDept();
ArrayList aryDeptno = dept.getDeptno();
request.setAttribute("deptno", aryDeptno);
}


/** *//**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
