欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

123

程序员文章站 2024-03-15 15:54:17
...
package servlet;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import DAO.*;
import model.*;
public class Pageshow extends HttpServlet {
	public Pageshow() {
		super();
	}
public void doGet(HttpServletRequest request,HttpServletResponse 	response)throws ServletException, IOException {
	Pagenumber pnum=new Pagenumber();
	pnum.setPageSize(4);//初始化;
	pnum.setShowPage(1);//初始化;
	DBOperation db=new DBOperation();
	pnum.setRowCount((int)(db.rowCount("book")));//总记录数;
	pnum.setPageCount();
	//显示第一页;
	String sql="select top "+pnum.getPageSize()+" * from book ";
	HttpSession session=request.getSession();
	session.setAttribute("result",pnum);
	request.setAttribute("display", db.queryReturnList(sql));
}
public void doPost(HttpServletRequest request, HttpServletResponse	response)throws 	ServletException, IOException {
	doGet(request,response);
	}
}
Pageshow1.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import DAO.*;
import model.Pagenumber;
public class Pageshow1 extends HttpServlet {
	public Pageshow1() {
		super();
	}
	public String getstring(String str){
		try{byte b[]=str.getBytes("ISO-8859-1");
			str=new String(b);
			return str;
			}catch(Exception e){return str;}
	}
        public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		HttpSession session=request.getSession();
		Pagenumber pnum=(Pagenumber)session.getAttribute("result");
		String option=request.getParameter("g");
		option=getstring(option);
		int showPage=1;
		if(option.equals("提交")){//输入页码数;
        	String integer=request.getParameter("showPage");
        	if(integer==null)integer="1";
        	try {showPage=Integer.parseInt(integer);}
        	catch(NumberFormatException e){showPage=1;}
        	pnum.setShowPage(showPage);
        }
	if(option.equals("上一页"))pnum.setShowPage(pnum.getShowPage()-1);
        if(option.equals("下一页"))pnum.setShowPage(pnum.getShowPage()+1);
		DBOperation db=new DBOperation ();
		String sql="SELECT TOP "+pnum.getPageSize()+" * FROM book  					WHERE ID 	NOT IN ( SELECT TOP "
		+pnum.getPageSize()*(pnum.getShowPage()-1)+" ID FROM  book)";
		session.setAttribute("result", pnum);
		request.setAttribute("display", db.queryReturnList(sql));
		RequestDispatcher dispatcher = null;
	   	dispatcher=getServletContext().getRequestDispatcher("/showpage.jsp");
		dispatcher.forward(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		doGet(request,response);
	}
}