servlet+mysql分页

先看看见效果
servlet+mysql分页_第1张图片
自己写的分页page.jsp只需要传入固定的参数即可实现分页

先来看看serlvet

package s.jf3q.com.servlet.xxxx;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import s.jf3q.com.dao.JdbcUtils;
import s.jf3q.com.util.DateUtil;
import s.jf3q.com.util.Page;

@WebServlet("/xxxx")
public class GetIpsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetIpsServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		    String pageNoStr=request.getParameter("pageNo") ;
		    String pageSizeStr=request.getParameter("pageSize");
		    String ip=request.getParameter("ip") ;
		    int pageNo=1;
		    int pageSize=10;
		    if(pageNoStr!=null&&!pageNoStr.isEmpty()&&pageNoStr!=""){
		    	pageNo=Integer.parseInt(pageNoStr); 
		    }
		    if(pageSizeStr!=null&&!pageSizeStr.isEmpty()&&pageSizeStr!=""){
		    	pageSize=Integer.parseInt(pageSizeStr); 
		    }
		    
			JdbcUtils jdbcUtils = new JdbcUtils();  
			jdbcUtils.getConnection();
			 
			String sql="select *  from ips where 1=1 ";
			String pagesSql="select count(id) as pages  from ips where 1=1 ";
			
			List params=new ArrayList();
			List paramsPage=new ArrayList();
			
			if(ip!=null&&!ip.isEmpty()&&ip!=""){
				ip=ip.trim();
				sql=sql+" and ip=?" ;
				pagesSql=pagesSql+" and ip=?" ;
				params.add(ip);
				paramsPage.add(ip);
				request.setAttribute("ip", ip);
		    }
			
			sql=sql+" order  by id desc  limit ?,? ";
			
			params.add((pageNo-1)*pageSize);
			params.add(pageSize);
			
			List li=null;
			
			
			int pages=0;
			long count=0l;
			try {
				li=jdbcUtils.findModeResult(sql, params);
				
				Map pagesmap=jdbcUtils.findSimpleResult(pagesSql, paramsPage);
				if(pagesmap!=null&&!pagesmap.isEmpty()){
					count=(long) pagesmap.get("pages");
					pages=new Page().getPages(count,pageSize);
				}
				 
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("li", li);
			request.setAttribute("pages", pages);
			request.setAttribute("pageSize", pageSize);
			request.setAttribute("pageNo", pageNo);
			request.setAttribute("count", count);
			request.getRequestDispatcher("/WEB-INF/dd.jsp").forward(request, response);
			 
			
	}

	 

}

 
  

request.setAttribute(“li”, li);
request.setAttribute(“pages”, pages);
request.setAttribute(“pageSize”, pageSize);
request.setAttribute(“pageNo”, pageNo);
request.setAttribute(“count”, count);
这些参数要放到request中,page页面需要获取的
分页工具

public class Page {
	public Integer getPages(Long count1,Integer pageSize1) {
		int p=new Integer(count1+"")%pageSize1;
		if(p==0)return  new Integer(count1+"")/pageSize1;
		else return new Integer(count1+"")/pageSize1+1; 
	}
}

dd.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>




ips

 


	
搜索
ip 8小时统计
id ip 访问时间 创建时间 备注
${item.id } ${item.ip } ${item.last_ts } ${item.create_ts } 查看备注
*
<%@ include file="page.jsp"%>
*

接下来看page.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>








	
首页 上一页 ${i } ${i } ${i } ${i } ${i } ${i } ${i } ${i } 下一页 尾页 ,当前是第 ${pageNo } 页

共 ${count} 条数据,共 ${pages} 页,每页显示 条 。跳转第 页,

是不是很简单呢,这个page.jsp可以作为插件使用,传入固定参数即可

如果有不懂的可以联系我抠:1913284695

原文链接http://s.jf3q.com/sjf3q/detail/24.html

你可能感兴趣的:(java)