<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="javax.xml.transform.*" %>
<%@ page import="javax.xml.transform.dom.DOMSource" %>
<%@ page language="java" import="javax.xml.parsers.*"%>
<%@ page import="javax.xml.transform.stream.StreamResult" %>
<%!
String str = "";
int totalRecord = 1;
NodeList links = null;
Document doc = null;
%>

<%
request.setCharacterEncoding("GBK");
// 取得各栏资料
String Name     = request.getParameter("Name");
String Email    = request.getParameter("Email");
String HomePage = request.getParameter("HomePage");
String Memo     = request.getParameter("Memo");
String Time     = (new Date()).toString();
String ID       = "";
String filename = getServletConfig().getServletContext().getRealPath ( "mylist.xml" );
try {
   	  // 为解析XML文件创建DOM对象
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder=factory.newDocumentBuilder();
      doc=builder.parse(new File( filename ));
      // 文档初始化
      doc.normalize();
      // 得到节点集合
      links =doc.getElementsByTagName("list");
	  totalRecord = links.getLength();
	  Element el = (Element)links.item( totalRecord - 1 );
	  // 自动计算出新增元素的ID值
	  ID = "" + (Integer.parseInt(el.getElementsByTagName("id").item(0).getFirstChild().getNodeValue()) + 1);
}catch(Exception e) {
       out.println("访问XML文件出错！");
	   return;
}

// 判断使用者是否正确填写留言
if(Name == null || Name.equals("") || Email == null || Email.equals("") || Memo == null || Memo.equals("")) {
   out.println("<BODY BGCOLOR='#FFFFFF' BACKGROUND='bg.jpg'>");
   out.println("<CENTER><FONT SIZE='6'><B>请将表单填入完整！</B></FONT><BR>");
   out.println("姓名、E-mail、留言请务必填写！");
   return;		// 停止处理以下程序
}
// 下面构造一组新的节点
try{
    Element root = doc.getDocumentElement();
    Node node_list = doc.createElement("list");    
    Node node_id = (Node) doc.createElement("id");
    Node tmp1 = doc.createTextNode("tmp1");
    tmp1.setNodeValue( ID );
    node_id.appendChild(tmp1);
    Node node_name = doc.createElement("username");
    Node tmp2 = doc.createTextNode("tmp2");
    tmp2.setNodeValue( Name );
    node_name.appendChild(tmp2);
    Node node_time = doc.createElement("time");
    Node tmp3 = doc.createTextNode("tmp3");
    tmp3.setNodeValue( Time );
    node_time.appendChild(tmp3);
    Node node_homepage = doc.createElement("homepage");
    Node tmp4 = doc.createTextNode("tmp4");
    tmp4.setNodeValue( HomePage );
    node_homepage.appendChild(tmp4);
    Node node_email = doc.createElement("email");
    Node tmp5 = doc.createTextNode("tmp5");
    tmp5.setNodeValue( Email );
    node_email.appendChild(tmp5);
    Node node_context = doc.createElement("context");
    Node tmp6 = doc.createTextNode("tmp6");
    tmp6.setNodeValue( Memo );
    node_context.appendChild(tmp6);
    node_list.appendChild( node_id ); 
    node_list.appendChild( node_name );
    node_list.appendChild( node_time );
    node_list.appendChild( node_homepage );
    node_list.appendChild( node_email );
    node_list.appendChild( node_context );
    root.appendChild(node_list);
	TransformerFactory tFactory =TransformerFactory.newInstance();
	Transformer transformer = tFactory.newTransformer();
	DOMSource source = new DOMSource(doc);
	StreamResult result = new StreamResult(new java.io.File( filename ));
	transformer.transform(source, result);    
  }catch(Exception e){
    e.printStackTrace();
}
response.sendRedirect("index.jsp");	 // 跳转到查看留言页面
%>
