<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="javax.xml.parsers.*"%>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.*" %>

<%!
int PageSize = 6;
int Page = 3;
int totalPage = 1;
int totalRecord = 1;
NodeList links = null;
String str = "";

public String ShowOnePage(NodeList nl, int Page, int PageSize) {
      for(int iPage=(Page-1)*PageSize; iPage<=Page*PageSize; iPage++) {
		 try {
			 if(iPage < totalRecord) {
        	     Element el = (Element)links.item(iPage);
	        	 str += showOneNode( el );
				 str += "<hr>";
			 }
		 }catch(Exception e) {            // 如果产生异常，表示无此节点，退出for循环
		     break;
		 }
	  }
	  return str;
}
	
// 显示单个节点子模块
public String showOneNode( Element el ) {
String tt = "";
try {
tt += "<table bgColor=#acacff width=80% border=1 borderColorDark=#ffffff borderColorLight=#000000 cellspacing=1 cellpadding=4 align=center>";	
tt += "<TR>";
tt += "<TD width=30% >留言编号</TD>";
tt += "<TD>" + el.getElementsByTagName("id").item(0).getFirstChild().getNodeValue() + "</TD>";
tt += "</TR>";
tt += "<TR>";
tt += "<TD>留言者姓名</TD>";
tt += "<TD>" + el.getElementsByTagName("username").item(0).getFirstChild().getNodeValue() + "</TD>";
tt += "</TR>";
tt += "<TR>";
tt += "<TD>留言时间</TD>";
tt += "<TD>" + el.getElementsByTagName("time").item(0).getFirstChild().getNodeValue() + "</TD>";
tt += "</TR>";
tt += "<TR>";
tt += "<TD>主页</TD>";
String tmp = el.getElementsByTagName("homepage").item(0).getFirstChild().getNodeValue();
tt += "<TD><A href=" + tmp + ">" + tmp + "</A></TD>";
tt += "</TR>";
tt += "<TR>";
tt += "<TD>Email</TD>";
tmp = el.getElementsByTagName("email").item(0).getFirstChild().getNodeValue();
tt += "<TD><A href=mailto:" + tmp + ">" + tmp + "</A></TD>";
tt += "</TR>";
tt += "<TR>";
tt += "<TD>留言内容</TD>";
tt += "<TD>" + el.getElementsByTagName("context").item(0).getFirstChild().getNodeValue() + "</TD>";
tt += "</TR>";
tt += "</table>";
}catch(Exception e) {
    System.out.println("解析XML文件时发生错误！");
    System.out.println( e.toString() );
}
	return tt;
}
%>
<html>
<head>
	<title>基于XML的留言板</title>
	<style type="text/css">
	<!--
	td { font-family: "宋体"; font-size: 9pt; text-decoration: none}
	a { font-family: "宋体"; font-size: 9pt; color: #FF0000; text-decoration: none}
	a:hover { color: #FFFF00; text-decoration: underline}
	-->
	</style>
</head>
<body bgcolor="#FFFFFF">
<img src="guestbook.gif">
<h2 ALIGN="CENTER">基于XML的留言板</h2>
<hr>
<center>

<A href="AddMessage.htm" style="font-size:16pt">我要留言</A>
<%
   String filename = getServletConfig().getServletContext().getRealPath ( "mylist.xml" );
   try {
   	  // 为解析XML文件创建DOM对象
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder=factory.newDocumentBuilder();
      Document doc=builder.parse(new File( filename ));
      // 文档初始化
      doc.normalize();
      // 得到节点集合
      links =doc.getElementsByTagName("list");
	  totalRecord = links.getLength();
   }catch(Exception e) {
       out.println("访问XML文件出错！");
	   return;
   }
	if(totalRecord % PageSize ==0) totalPage = totalRecord / PageSize;  // 如果是当前页码的整数倍
	else  totalPage = (int) Math.floor( totalRecord / PageSize ) + 1;   // 如果最后还空余一页
	if(totalPage == 0) totalPage = 1;
	try {
		if(request.getParameter("Page")==null || request.getParameter("Page").equals("")) 
		   Page = 1;
		else
		   Page = Integer.parseInt(request.getParameter("Page"));
	} catch(java.lang.NumberFormatException e) {  // 捕获用户从浏览器地址拦直接输入类似于Page=sdfsdfsdf这样的错误页码所造成的异常
		   Page = 1;
	}
	if(Page < 1)  Page = 1;
	if(Page > totalPage) Page = totalPage;
	out.println(ShowOnePage(links, Page, PageSize));
%>
<form Action="index.jsp" Method="GET">
<% 
   if(Page != 1) {
      out.println("<A HREF=index.jsp?Page=1>第一页</A>");
      out.println("<A HREF=index.jsp?Page=" + (Page-1) + ">上一页</A>");
   }
   if(Page != totalPage) {
      out.println("<A HREF=index.jsp?Page=" + (Page+1) + ">下一页</A>");
      out.println("<A HREF=index.jsp?Page=" + totalPage + ">最后一页</A>");
   }
%>
  <p>输入页数：<input TYPE="TEXT" Name="Page" SIZE="3"> 页数:<font COLOR="Red"><%=Page%>/<%=totalPage%></font> 
  </p>
</form>
</center>
</body>
</html>
