package com.utils;
public class Entry {
private int entryID;
private boolean asDraft;
private String title;
private String content;
/**
* For further extension
*/
private boolean setTop;
/**
* For further extension, make it easy to be searched by google
*/
private String staticLinkURL;
public int getEntryID() {
return entryID;
}
public void setEntryID(int entryID) {
this.entryID = entryID;
}
public boolean isAsDraft() {
return asDraft;
}
public void setAsDraft(boolean asDraft) {
this.asDraft = asDraft;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isSetTop() {
return setTop;
}
public void setSetTop(boolean setTop) {
this.setTop = setTop;
}
public String getStaticLinkURL() {
return staticLinkURL;
}
public void setStaticLinkURL(String staticLinkURL) {
this.staticLinkURL = staticLinkURL;
}
}
package com.utils;
import java.util.ArrayList;
import java.util.List;
public class PagingDAO {
public static List<Entry> entryList = new ArrayList<Entry>();
static {
for (int i = 0; i < 116; i++) {
Entry e = new Entry();
e.setEntryID(i);
e.setTitle("The title for the article " + i + " ");
entryList.add(e);
}
}
public static List<Entry> getEntryList(int pageNum, int pageSize) {
System.out.println(pageNum+";"+pageSize);
List<Entry> ret = new ArrayList<Entry>();
int start = (pageNum - 1) * pageSize;
int end = start + pageSize - 1;
System.out.println(start+"-"+end);
if (start >= entryList.size())
return ret;
for (int i = start; i <= end; i++) {
if (i < entryList.size())
ret.add(entryList.get(i));
}
return ret;
}
}
package com.utils;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class PostsServlet
*/
@WebServlet("/PostsServlet")
public class PostsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PostsServlet () {
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
String pageNumberStr = request.getParameter("pageNumber");
int pageNumber = 1;
if(pageNumberStr!=null && !pageNumberStr.isEmpty())
{
pageNumber = Integer.parseInt(pageNumberStr);
}
int pageSize = 10; //分页大小
int totalPosts = PagingDAO.entryList.size(); //总文章数
int totalPages = totalPosts/pageSize + ((totalPosts%pageSize)>0?1:0); //计算得出的总页数
request.setAttribute("pageSize", pageSize);
request.setAttribute("totalPosts", totalPosts);
request.setAttribute("pageNumber", pageNumber);
request.setAttribute("totalPages", totalPages);
List<Entry> entryList = PagingDAO.getEntryList(pageNumber, pageSize);
System.out.println("entryList:"+ entryList.size());
request.setAttribute("entryList", entryList);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.utils;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class PostsServlet
*/
@WebServlet("/PostsServletafter")
public class PostsServletafter extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PostsServletafter() {
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
String pageNumberStr = request.getParameter("pageNumber");
int pageNumber = 1;
if(pageNumberStr!=null && !pageNumberStr.isEmpty())
{
pageNumber = Integer.parseInt(pageNumberStr);
}
int pageSize = 10; //分页大小
int totalPosts = PagingDAO.entryList.size(); //总文章数
int totalPages = totalPosts/pageSize + ((totalPosts%pageSize)>0?1:0); //计算得出的总页数
request.setAttribute("pageSize", pageSize);
request.setAttribute("totalPosts", totalPosts);
request.setAttribute("pageNumber", pageNumber);
request.setAttribute("totalPages", totalPages);
List<Entry> entryList = PagingDAO.getEntryList(pageNumber, pageSize);
System.out.println("entryList:"+ entryList.size());
request.setAttribute("entryList", entryList);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="tssearch" id="tssearch"
action="${pageContext.request.contextPath }/PostsServlet"
method="post">
<div class="im">
<img src="images/bgr.jpg" width="366" height="220" />
</div>
<div id="tab">
<div class="selectedDiv">
<dl class="sec">
<dd>
<select name="selectType"><option value="aid">输入用户ID</option>
<option value="qq">输入用户QQ</option>
<option value="mobile">输入用户 手机号</option>
<option value="zhifubao">输入用户支付宝</option>
<option value="weixin">输入用户微信号</option>
<option value="e_mail">输入用户邮箱地址</option>
<option value="aname">输入用户昵称</option>
<option value="realname">输入用户真实姓名</option></select>
</dd>
<dd>
<input type="text" name="pageNumber"" /><span>${form.errors.searchText }</span>
</dd>
<dd>
<button type="submit">搜 索</button>
</dd>
</dl>
</div>
</div>
<div class="tital">
<h2>选择论坛</h2>
<div class="clear"></div>
<ul class="more">
<li><input type="radio" name="bbsName" value="renren" />人人网</li>
<li><input type="radio" name="bbsName" value="gjhl" />赶集婚恋</li>
<li><input type="radio" name="bbsName" value="aimu5" />爱穆网</li>
<li><input type="radio" name="bbsName" value="wy163" />网易论坛</li>
<li><input type="radio" name="bbsName" value="doc88" />道客巴巴</li>
<!-- ######--->
<li><input type="radio" name="bbsName" value="y4dg" />影视帝国</li>
<li><input type="radio" name="bbsName" value="eastday" />东方网</li>
<li><input type="radio" name="bbsName" value="wy163" />网易论坛</li>
<li><input type="radio" name="bbsName" value="tiexue" />铁血论坛</li>
<li><input type="radio" name="bbsName" value="xinhua" />发展论坛</li>
<li><input type="radio" name="bbsName" value="dayoo" />大洋论坛</li>
<li><input type="radio" name="bbsName" value="people" />强国论坛</li>
<li><input type="radio" name="bbsName" value="tianya" />天涯论坛</li>
<li><input type="radio" name="bbsName" value="voc" />华声论坛</li>
<li><input type="radio" name="bbsName" value="ifeng" />凤凰论坛</li>
<li><input type="radio" name="bbsName" value="sina" />新浪论坛</li>
<li><input type="radio" name="bbsName" value="xilu" />西陆论坛</li>
<li><input type="radio" name="bbsName" value="sinamobile" />新浪微博</li>
<li><input type="radio" name="bbsName" value="youyuan" />有缘网</li>
<li><input type="radio" name="bbsName" value="taonan" />淘男网</li>
<li><input type="radio" name="bbsName" value="supei" />速配网</li>
<li><input type="radio" name="bbsName" value="jnqy" />江南情缘</li>
<li><input type="radio" name="bbsName" value="hongniang" />红娘网</li>
<li><input type="radio" name="bbsName" value="jiayuan" />世纪佳缘</li>
<li><input type="radio" name="bbsName" value="lianpuie" />FaceBook</li>
<li><input type="radio" name="bbsName" value="muslim" />中穆网</li>
<li><input type="radio" name="bbsName" value="muslimwww" />穆斯林在线</li>
<li><input type="radio" name="bbsName" value="zhihu" />知乎</li>
<li><input type="radio" name="bbsName" value="xici" />西祠胡同</li>
<li><input type="radio" name="bbsName" value="tongcheng" />58同城</li>
<li><input type="radio" name="bbsName" value="qianlong" />千龙网</li>
<li><input type="radio" name="bbsName" value="southcn" />南方论坛</li>
<li><input type="radio" name="bbsName" value="mala" />麻辣论坛</li>
<li><input type="radio" name="bbsName" value="szbbs" />深圳论坛</li>
<li><input type="radio" name="bbsName" value="moslem" />星期五穆斯林俱乐部</li>
<li><input type="radio" name="bbsName" value="muslem" />中国穆斯林青年网</li>
<li><input type="radio" name="bbsName" value="pt80" />捌零音乐论坛</li>
<li><input type="radio" name="bbsName" value="mqingy" />穆民情缘论坛</li>
</ul>
<div class="clear"></div>
</div>
<!--/tital-->
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.Iterator"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script type="text/javascript">
function gotoSelectedPage()
{
var x = document.getElementById("navigatorForm");
//alert("Original action: " + x.action)
x.submit();
}
</script>
<form action="${pageContext.request.contextPath }/PostsServlet"
method="post">
<a href="Posts?pageNumber=1">首页</a>
<c:if test="${pageNumber>1}">
<a href="PostsServlet?pageNumber=${pageNumber-1}">上一页</a>
</c:if>
跳转到第 <select name="pageNumber" onchange="gotoSelectedPage();">
<c:forEach begin="1" end="${totalPages}" step="1" var="pageIndex">
<c:choose>
<c:when test="${pageIndex eq pageNumber}">
<option value="${pageIndex}" selected="selected">${pageIndex}</option>
</c:when>
<c:otherwise>
<option value="${pageIndex}">${pageIndex}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>页
<c:if test="${pageNumber<totalPages}">
<a href="PostsServlet?pageNumber=${pageNumber+1}">下一页</a>
</c:if>
<a href="PostsServlet?pageNumber=${totalPages}">末页</a>
</form>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.Iterator"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
page Size : ${pageSize}
<br />
Total Posts: ${totalPosts}
<br />
Total Pages: ${totalPages}
<br />
Current Page: ${pageNumber}
<hr />
<table>
<thead>
<tr align="center">
<td width="10%">Article ID</td>
<td width="70%">Article Title</td>
<td colspan="3">Actions</td>
</tr>
</thead>
<tbody>
<c:forEach items="${entryList}" var="entry">
<tr align="center">
<td>${entry.entryID}</td>
<td>${entry.title}</td>
<td><a href="viewEntry?entryID=${entry.entryID}">View</a></td>
<td><a href="editEntry?entryID=${entry.entryID}">Edit</a></td>
<td><a href="deleteEntry?entryID=${entry.entryID}">Delete</a></td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr align="center">
<td colspan="5">
<jsp:include page="paging_footer.jsp"></jsp:include>
</td>
</tr>
</tfoot>
</table>
<hr/>