/** *//** * <P>外星人是否存在评论系统</p> * @author ceun * 联系作者:<br> * <a href="mailto:ceun@163.com">ceun</a><br> * @version 1.0 2007-01-30 实现基本功能<br> * CommentBean.java * Created on Jan 30, 2007 9:39:19 AM */ package com.ceun.bean; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; import org.jdom.CDATA; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.Text; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; /** *//** *<p> 封装对XML的操作</p> * @author ceun * 联系作者:<br> * <a href="mailto:ceun@163.com">ceun</a><br> * @version 1.0 2007-01-30 实现基本功能<br> */ publicclass CommentBean ...{ private String filepath; private SAXBuilder builder =null; private Document doc =null; public CommentBean() ...{ } /** *//** * 初始化XML文件路径,加载文件 * */ public CommentBean(String path) ...{ this.filepath = path; builder =new SAXBuilder(); try...{ doc = builder.build(filepath); }catch (JDOMException e) ...{ System.out.print("找不到指定的XML文件"); e.printStackTrace(); }catch (IOException e) ...{ System.out.print("找不到指定的文件"); e.printStackTrace(); } } /** *//** * 添加评论 * @param nikename 评论者昵称 * @param comment 评论内容 * @param attitude 评论者的结论(yes-存在,no-不存在) * */ public String addComment(String nikename, String comment, String attitude) ...{ Element root = doc.getRootElement(); Element el =new Element("comment"); Random rand =new Random(); int id = rand.nextInt(10000); el.setAttribute("id", "comment_"+ id); el.setAttribute("attitude", attitude); Element name =new Element("nikename"); CDATA cname =new CDATA(nikename); name.addContent(cname); Element data =new Element("data"); CDATA ctext =new CDATA(comment); data.addContent(ctext); SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date =new Date(); Text tdate =new Text(format.format(date)); Element pubdate =new Element("pubdate"); pubdate.addContent(tdate); el.addContent(name); el.addContent(data); el.addContent(pubdate); root.addContent(el); XMLOutputter outputter =new XMLOutputter("", true, "GB2312"); // 清除comment元素间的空格 outputter.setTrimAllWhite(true); try...{ outputter.output(doc, new FileWriter(filepath)); }catch (IOException e) ...{ System.out.println("指定路径有错"); e.printStackTrace(); } return tdate.getText(); } /** *//** * 删除指定ID的评论 * @param commentId 评论ID * @return 返回操作结果字符串(成功或失败) * */ public String removeComment(String commentId) ...{ Element root = doc.getRootElement(); List comments = root.getChildren(); int size = comments.size(); Element dist =null; for (int i =0; i < size; i++) ...{ Element comment = (Element) comments.get(i); String id = comment.getAttributeValue("id"); if (id.equals(commentId)) ...{ dist = comment; break; } } if (dist !=null) ...{ root.removeContent(dist); XMLOutputter outputter =new XMLOutputter("", true, "GB2312"); // 清除comment元素间的空格 outputter.setTrimAllWhite(true); try...{ outputter.output(doc, new FileWriter(filepath)); }catch (IOException e) ...{ System.out.println("重写文件有出错"); e.printStackTrace(); } return"成功删除指定元素!"; }else return"指定元素不存在!"; } /** *//** * 批量删除评论 * @param commentIdArgs 评论ID数组 * @return 返回操作结果字符串(成功或失败) * */ public String removeComments(String[] commentIdArgs) ...{ Element root = doc.getRootElement(); List comments = root.getChildren(); int size = comments.size(); int len = commentIdArgs.length; List<Element> dist =new ArrayList<Element>(); outer:for (int i =0; i < size; i++) ...{ Element comment = (Element) comments.get(i); String id = comment.getAttributeValue("id"); for (int j =0; j < len; j++) if (id.equals(commentIdArgs[j])) ...{ dist.add(comment); continue outer; } } int dist_size=dist.size(); if (dist_size !=0) ...{ for (int i =0; i < dist_size; i++) root.removeContent(dist.get(i)); XMLOutputter outputter =new XMLOutputter("", true, "GB2312"); // 清除comment元素间的空格 outputter.setTrimAllWhite(true); try...{ outputter.output(doc, new FileWriter(filepath)); }catch (IOException e) ...{ System.out.println("重写文件有出错"); e.printStackTrace(); } return"成功删除指定的元素集合!"; }else return"指定元素集合的不存在!"; } /** *//** * @return the filepath */ public String getFilepath() ...{ return filepath; } /** *//** * @param filepath * the filepath to set */ publicvoid setFilepath(String filepath) ...{ this.filepath = filepath; } /** *//** * @return the builder */ public SAXBuilder getBuilder() ...{ return builder; } /** *//** * @param builder * the builder to set */ publicvoid setBuilder(SAXBuilder builder) ...{ this.builder = builder; } }
3.处理AJAX请求的Servlet AddCommentServlet.java
package com.ceun.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ceun.bean.CommentBean; /** *//** * <p>后台处理Servlet</p> *2007-01-30 * * @author ceun * 联系作者:<br> * <a href="mailto:ceun@163.com">ceun</a><br> * @version 1.0 2007-01-30 实现基本功能<br> * */ publicclass AddCommentServlet extends HttpServlet ...{ /** *//** * serialVersionUID long */ privatestaticfinallong serialVersionUID =1L; /** *//** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{ request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); String nikename = request.getParameter("nn"); String comment = request.getParameter("rsn"); String attitude = request.getParameter("atti"); String filepath = request.getSession().getServletContext().getRealPath( "data/comment.xml"); CommentBean bean =new CommentBean(filepath); String str = bean.addComment(nikename, comment, attitude); out.println(str); } /** *//** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{ doGet(request, response); } }
var Browser =...{}; Browser.isMozilla = (typeof document.implementation !="undefined") && (typeof document.implementation.createDocument !="undefined") && (typeof HTMLDocument !="undefined"); Browser.isIE = window.ActiveXObject ?true : false; Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") !=-1); Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") !=-1); Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") !=-1); String.prototype.trim=function()...{returnthis.replace(/^s+|s+$/g,"");}; function addComment(url)...{ var nn=$F("nn"); var rsn=$F("reason"); var atti=false; var rbgroup=document.cform.elements["exist"]; for(i=0;i<rbgroup.length;i++)...{ if(rbgroup[i].checked) ...{atti=rbgroup[i].value;break;}; } var locateTop=$("locateDiv1").parentNode.offsetTop-50; var locateLeft=$("locateDiv1").parentNode.offsetLeft+150; var alertMsg=""; if(!atti) alertMsg+="<br/>请表明你的态度!"; if(nn.trim()=="") alertMsg+="<br/>昵称不能为空!" if(rsn.trim()=="") alertMsg+="<br/>请写下你的理由!"; var cinfomsg=$("cinfomsg"); if(alertMsg!="")...{ cinfomsg.className="failure"; cinfomsg.innerHTML=alertMsg; showDialog($("cinfoDiv"),locateTop,locateLeft); return; }else...{ cinfomsg.className="success"; cinfomsg.innerHTML="正处理数据中..."; showDialog($("cinfoDiv"),locateTop,locateLeft); } var pars="nn="+nn+"&atti="+atti+"&rsn="+rsn; var http=Ajax.getTransport(); var contentType ="application/x-www-form-urlencoded; charset=utf-8"; http.open("POST",url,true); http.setRequestHeader("Content-Type", contentType); http.onreadystatechange=function()...{ if (http.readyState ==4)...{ if (http.status==200)...{ afterAdd(http); } } } http.send(pars); } function afterAdd(request)...{ hideDialog(); var atti=false; var rbgroup=document.cform.elements["exist"]; for(i=0;i<rbgroup.length;i++)...{ if(rbgroup[i].checked) ...{atti=rbgroup[i].value;break;}; } var nn=$F("nn"); var rsn=$F("reason"); generateCommentList(atti,nn,request.responseText,rsn) $("nn").value=""; $("reason").value=""; } function generateCommentList(atti,nn,date,rsn)...{ var commentLists=$("comment-lists"); var commentlist=document.createElement("div"); commentlist.className="comment-list"; var observerInfo=document.createElement("div"); observerInfo.className="observer-info"; var attitude=(atti=="yes")?"认为存在":"认为不存在"; var info=document.createTextNode("评论者:"+nn+" 观点:"+attitude+""+date); observerInfo.appendChild(info); var observerComment=document.createElement("div"); observerComment.className="observer-comment"; var reason=document.createTextNode(rsn); observerComment.appendChild(reason); commentlist.appendChild(observerInfo); commentlist.appendChild(observerComment); if(commentLists.hasChildNodes())...{ var tmp=commentLists.firstChild; commentLists.insertBefore(commentlist,tmp); }else commentLists.appendChild(commentlist); } function getComments(url)...{ Element.show("process-indicator"); Element.makePositioned("process-indicator"); var top = document.body.scrollTop +200; var left =""; if ($("process-indicator").style.width) ...{ left =window.document.body.scrollWidth/2-100; }else...{ left = parseInt(document.body.clientWidth /2); } var style =...{top:top +"px", left:left +"px"}; Element.setStyle("process-indicator", style); //加个随机数,去除缓存影响 var pars="rd=rd_"+parseInt(Math.random()*10000); var http=Ajax.getTransport(); var contentType ="application/x-www-form-urlencoded; charset=utf-8"; http.open("POST",url,true); http.setRequestHeader("Content-Type", contentType); http.onreadystatechange=function()...{ if (http.readyState ==4)...{ if (http.status==200)...{ afterGetComments(http); } } } http.send(pars); } function afterGetComments(request)...{ //提示框 Element.hide("process-indicator"); var xmldata=request.responseXML; var comments=xmldata.getElementsByTagName("comment"); if(!comments)...{alert("暂无评论");return;} var len=comments.length; for(var i=0;i<len;i++)...{ var atti=comments[i].getAttribute("attitude"); var nn=comments[i].getElementsByTagName("nikename")[0].firstChild.nodeValue; var rsn=comments[i].getElementsByTagName("data")[0].firstChild.nodeValue; var date=comments[i].getElementsByTagName("pubdate")[0].firstChild.nodeValue; generateCommentList(atti,nn,date,rsn) } } //对话框模块 var lastDialog=null; function showDialog(dialog,offsetTop,offsetLeft)...{ if (lastDialog) lastDialog.style.display="none"; dialog.style.top=offsetTop+"px"; dialog.style.left=offsetLeft+"px"; dialog.style.display=""; lastDialog=dialog; document.onmousedown=toHideDialog } function toHideDialog(event)...{ if (!lastDialog) ...{document.onclick=null;return;} var obj=null; if(Browser.isIE) obj=window.event.srcElement; else obj=event.target; hideDialog(); } function hideDialog()...{ if (lastDialog) lastDialog.style.display="none"; lastDialog=null document.onmousedown=null; }