今天抽空看了看Jquery,觉得里边的ajax封装得很好,就写了个简易的留言本。
这里写一下主要的几个文件,以备日后需要。
源码下载
getComments.java
putComments.java getComments.js putComments.js
1 import java.io.IOException;
2 import java.io.PrintWriter;
3 import java.util.Iterator;
4
5 import javax.servlet.ServletContext;
6 import javax.servlet.ServletException;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import org.dom4j.Document;
12 import org.dom4j.DocumentException;
13 import org.dom4j.Element;
14 import org.dom4j.io.SAXReader;
15
16 public class getComments extends HttpServlet {
17
18 public void doGet(HttpServletRequest request, HttpServletResponse response)
19 throws ServletException, IOException {
20 String rootPath = request.getSession().getServletContext().getRealPath("");
21 response.setContentType("text/html;charset=gbk");
22 PrintWriter out = response.getWriter();
23 SAXReader reader = new SAXReader();
24 try {
25 //reader.read("/Comments.xml");
26 Document doc = reader.read(rootPath+"/Comments.xml");
27 Element root = doc.getRootElement();
28 Iterator itr = root.elementIterator();
29 while(itr.hasNext())
30 {
31 Element elementc = (Element)itr.next();
32
33 Iterator aItr = elementc.elementIterator();
34 while(aItr.hasNext())
35 {
36 Element e = (Element)aItr.next();
37 if(e.getName().equals("name"))
38 {
39 out.print("<p>"+e.getData()+"</p>");
40 }
41 if(e.getName().equals("content"))
42 {
43 out.print("<p class='date'>"+e.getData()+"</p>");
44 }
45 }
46 }
47 } catch (DocumentException e) {
48 e.printStackTrace();
49 }
50 out.flush();
51 out.close();
52 }
53
54 public void doPost(HttpServletRequest request, HttpServletResponse response)
55 throws ServletException, IOException {
56 doGet(request, response);
57 }
58
59 }
60
1 import java.io.IOException;
2 import java.io.PrintWriter;
3
4 import javax.servlet.ServletException;
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.dom4j.Document;
10 import org.dom4j.DocumentException;
11 import org.dom4j.DocumentHelper;
12 import org.dom4j.Element;
13 import org.dom4j.io.OutputFormat;
14 import org.dom4j.io.SAXReader;
15 import org.dom4j.io.XMLResult;
16 import org.dom4j.io.XMLWriter;
17
18 public class putComments extends HttpServlet {
19
20 public void doGet(HttpServletRequest request, HttpServletResponse response)
21 throws ServletException, IOException {
22 request.setCharacterEncoding("UTF-8");
23 String rootPath = request.getSession().getServletContext().getRealPath("");
24 String name = request.getParameter("guestname");
25 String contents = request.getParameter("guestcontent");
26 Element comment = DocumentHelper.createElement("comment");
27 comment.addElement("name").setText(name);
28 comment.addElement("content").setText(contents);
29
30 SAXReader reader = new SAXReader();
31 try {
32 Document doc = reader.read(rootPath+"/Comments.xml");
33 Element root = doc.getRootElement();
34 root.add(comment);
35 OutputFormat format = OutputFormat.createPrettyPrint();
36 XMLResult result = new XMLResult(new java.io.FileOutputStream(rootPath+"/Comments.xml"),format);
37 XMLWriter writer = result.getXMLWriter();
38 doc.setXMLEncoding("GBK");
39 writer.write(doc);
40 } catch (DocumentException e) {
41 e.printStackTrace();
42 }
43 }
44
45 public void doPost(HttpServletRequest request, HttpServletResponse response)
46 throws ServletException, IOException {
47
48 doGet(request, response);
49 }
50
51 }
52
1 function getComments()
2 {
3 $.ajax({
4 type:"post",
5 url:"getComments",
6 cache:false,
7 dataType:"html",
8 beforeSend: function(XMLHttpRequest){
#liststatus<
/span>"<
span style="color: #000000;">).html(<
span style="color: #000000;">"<
span style="color: #000000;">正在读取留言,请稍等<
img src="http://www.blogjava.net/Images/dot.gif" alt="" />");
10 },
11 success: function(data, textStatus){
12 $("#liststatus").hide("slow");
13 $("#commentslist").html(data);
14 //alert("finish");
15 },
16 complete: function(XMLHttpRequest, textStatus){
17 //alert("complete"+"\n textStatus:"+textStatus);
18 },
19 error: function(){
&
nbsp; $(#liststatus<
/span>"<
span style="color: #000000;">).html(<
span style="color: #000000;">"<
span style="color: #000000;">读取留言时发生错误.<
span style="color: #000000;">");
21 }
22
23
24 });
25
26 }
1 function putComments()
2 {
3 $.ajax({
4 type:"post",
5 url:"putComments",
6 dataType:"html",
7 cache:"false",
8 data:"guestname="+$("#guestname").val()+"&guestcontent="+$("#guestcontent").val(),
9 beforeSend: function(XMLHttpRequest){
10 $("#status").html("正在提交留言,请稍候");
11 $("#form").hide("slow");
12 },
13 success: function(data, textStatus){
14 $("#status").html("留言提交成功!");
15 $("#form").show("slow");
16 getComments();
17 },
18 complete: function(XMLHttpRequest, textStatus){
19 //alert("complete"+"\n textStatus:"+textStatus);
20 },
21 error: function(){
22 $("#status").html("提交留言时出现错误!");
23 $("#form").show("slow");
24 }
25 });
26 }