package com.wxpn.tutorial.ec.servlet;

import java.io.IOException;
import java.util.Collection;

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

import com.wxpn.tutorial.ec.bean.ECMemberMgr;

/**
 * Servlet implementation class for Servlet: UserListServlet
 */
public class UserListServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public UserListServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Pragma","No-cache");
		response.setHeader("Cache-Control","no-cache");
		response.setDateHeader("Expires", 0);
		
		int pagesize = 10;
		String page = request.getParameter("page");
		ECMemberMgr mgr = new ECMemberMgr();
		int count = mgr.getCount();// 记录总数
		int totalPageNum = count / pagesize;// 逻辑页数
		// 如果纪录总数除以每页的显示个数,余数大于0,那么
		// 逻辑页数应该为商+1
		if ((count % pagesize) > 0) {
			totalPageNum += 1;
		}
		if (page == null) {
			page = "1";
		}
		request.setAttribute("totalPageNum", new Integer(totalPageNum));
		Collection c = mgr.getAll(pagesize, Integer.parseInt(page));
		request.setAttribute("c", c);
		try {
			request.getRequestDispatcher("/member/userlist.jsp").forward(
					request, response);
		} catch (Throwable t) {
			getServletContext().log(t.getMessage());
		}
	}
}