package com.wxpn.tutorial.servlet;

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

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

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

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int pagesize = 10;
		String page = request.getParameter("page");
		MessageBoardMgr mgr = new MessageBoardMgr();
		int count=mgr.getCount();// 记录总数
		int totalPageNum=count/pagesize;// 逻辑页数
        // 如果纪录总数除以每页的显示个数,余数大于0,那么
		// 逻辑页数应该为商+1
		if((count%pagesize)>0){
			totalPageNum+=1;
		}
		Collection c = null;
		if (page == null) {
			c = mgr.getAllMessages(pagesize, 1);
		} else {
			c = mgr.getAllMessages(pagesize, Integer.parseInt(page));
		}
		request.setAttribute("totalPageNum",new Integer(totalPageNum));
		request.setAttribute("c", c);
		try {
			request.getRequestDispatcher("/browse.jsp").forward(request,
					response);
		} catch (Throwable t) {
			getServletContext().log(t.getMessage());
		}
	}
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}