人脉不是你认识多少人,而是有多少人认识你!关键有多少人认可你!人脉不是你和多少人打过交道,而是有多少人愿意,主动和你打交道!人脉不是有多少人在面前吹捧你,而是有多少人在背后称赞你!早安!
这是一个简单的评论系统,使用了JDOM(这边使用Jdom-b9),实例使用JSP作为视图,结合使用AJAX(用到prototype-1.4),Servlet和JavaBean作为后台处理,使用xml文件存储数据。 1.应用目录结构如下:
data
|--comment.xml
js
|--prototype.js
|--ufo.js(UTF-8格式)
css
|--ufo.css
images
|--loading.gif
ufo.jsp(UTF-8格式)
WEB-INF
|-lib
|-jdom.jar
|-classes
...
|-web.xml /*********************************************
*Author:Java619
*Time:2007-02-14
**********************************************/
2.后台JavaBeanCommentBean.java /***//**
*<P>外星人是否存在评论系统</p>
*@authorceun
*联系作者:<br>
*<arel="nofollow noopener noreferrer" href="mailto:ceun@163.com">ceun</a><br>
*@version1.02007-01-30实现基本功能<br>
*CommentBean.java
*CreatedonJan30,20079:39:19AM
*/
packagecom.ceun.bean; importjava.io.FileWriter;
importjava.io.IOException;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importjava.util.Random; importorg.jdom.CDATA;
importorg.jdom.Document;
importorg.jdom.Element;
importorg.jdom.JDOMException;
importorg.jdom.Text;
importorg.jdom.input.SAXBuilder;
importorg.jdom.output.XMLOutputter; /***//**
*<p>封装对XML的操作</p>
*@authorceun
*联系作者:<br>
*<arel="nofollow noopener noreferrer" href="mailto:ceun@163.com">ceun</a><br>
*@version1.02007-01-30实现基本功能<br>
*/
publicclassCommentBean...{
privateStringfilepath; privateSAXBuilderbuilder=null; privateDocumentdoc=null; publicCommentBean()...{ }
/***//**
*初始化XML文件路径,加载文件
**/
publicCommentBean(Stringpath)...{
this.filepath=path;
builder=newSAXBuilder();
try...{
doc=builder.build(filepath);
}catch(JDOMExceptione)...{
System.out.print("找不到指定的XML文件");
e.printStackTrace();
}catch(IOExceptione)...{
System.out.print("找不到指定的文件");
e.printStackTrace();
}
}
/***//**
*添加评论
*@paramnikename评论者昵称
*@paramcomment评论内容
*@paramattitude评论者的结论(yes-存在,no-不存在)
**/
publicStringaddComment(Stringnikename,Stringcomment,Stringattitude)...{
Elementroot=doc.getRootElement(); Elementel=newElement("comment");
Randomrand=newRandom();
intid=rand.nextInt(10000);
el.setAttribute("id","comment_"+id);
el.setAttribute("attitude",attitude); Elementname=newElement("nikename");
CDATAcname=newCDATA(nikename);
name.addContent(cname); Elementdata=newElement("data");
CDATActext=newCDATA(comment);
data.addContent(ctext); SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Datedate=newDate();
Texttdate=newText(format.format(date));
Elementpubdate=newElement("pubdate");
pubdate.addContent(tdate); el.addContent(name);
el.addContent(data);
el.addContent(pubdate);
root.addContent(el);
XMLOutputteroutputter=newXMLOutputter("",true,"GB2312");
//清除comment元素间的空格
outputter.setTrimAllWhite(true);
try...{
outputter.output(doc,newFileWriter(filepath));
}catch(IOExceptione)...{
System.out.println("指定路径有错");
e.printStackTrace();
}
returntdate.getText();
}
/***//**
*删除指定ID的评论
*@paramcommentId评论ID
*@return返回操作结果字符串(成功或失败)
**/
publicStringremoveComment(StringcommentId)...{
Elementroot=doc.getRootElement();
Listcomments=root.getChildren();
intsize=comments.size();
Elementdist=null;
for(inti=0;i<size;i++)...{
Elementcomment=(Element)comments.get(i);
Stringid=comment.getAttributeValue("id");
if(id.equals(commentId))...{
dist=comment;
break;
}
}
if(dist!=null)...{
root.removeContent(dist);
XMLOutputteroutputter=newXMLOutputter("",true,"GB2312");
//清除comment元素间的空格
outputter.setTrimAllWhite(true);
try...{
outputter.output(doc,newFileWriter(filepath));
}catch(IOExceptione)...{
System.out.println("重写文件有出错");
e.printStackTrace();
}
return"成功删除指定元素!";
}else
return"指定元素不存在!";
}
/***//**
*批量删除评论
*@paramcommentIdArgs评论ID数组
*@return返回操作结果字符串(成功或失败)
**/
publicStringremoveComments(String[]commentIdArgs)...{
Elementroot=doc.getRootElement();
Listcomments=root.getChildren();
intsize=comments.size();
intlen=commentIdArgs.length;
List<Element>dist=newArrayList<Element>();
outer:for(inti=0;i<size;i++)...{
Elementcomment=(Element)comments.get(i);
Stringid=comment.getAttributeValue("id"); for(intj=0;j<len;j++)
if(id.equals(commentIdArgs[j]))...{
dist.add(comment);
continueouter;
}
}
intdist_size=dist.size();
if(dist_size!=0)...{
for(inti=0;i<dist_size;i++)
root.removeContent(dist.get(i));
XMLOutputteroutputter=newXMLOutputter("",true,"GB2312");
//清除comment元素间的空格
outputter.setTrimAllWhite(true);
try...{
outputter.output(doc,newFileWriter(filepath));
}catch(IOExceptione)...{
System.out.println("重写文件有出错");
e.printStackTrace();
}
return"成功删除指定的元素集合!";
}else
return"指定元素集合的不存在!";
} /***//**
*@returnthefilepath
*/
publicStringgetFilepath()...{
returnfilepath;
} /***//**
*@paramfilepath
*thefilepathtoset
*/
publicvoidsetFilepath(Stringfilepath)...{
this.filepath=filepath;
} /***//**
*@returnthebuilder
*/
publicSAXBuildergetBuilder()...{
returnbuilder;
} /***//**
*@parambuilder
*thebuildertoset
*/
publicvoidsetBuilder(SAXBuilderbuilder)...{
this.builder=builder;
}
} 3.处理AJAX请求的ServletAddCommentServlet.java packagecom.ceun.servlet; importjava.io.IOException;
importjava.io.PrintWriter; importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse; importcom.ceun.bean.CommentBean;
/***//**
*<p>后台处理Servlet</p>
*2007-01-30
**@authorceun
*联系作者:<br>
*<arel="nofollow noopener noreferrer" href="mailto:ceun@163.com">ceun</a><br>
*@version1.02007-01-30实现基本功能<br>
**/
publicclassAddCommentServletextendsHttpServlet...{ /***//**
*serialVersionUIDlong
*/
privatestaticfinallongserialVersionUID=1L; /***//**
*ThedoGetmethodoftheservlet.<br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequest
*therequestsendbytheclienttotheserver
*@paramresponse
*theresponsesendbytheservertotheclient
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException...{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control","no-cache"); PrintWriterout=response.getWriter();
Stringnikename=request.getParameter("nn"); Stringcomment=request.getParameter("rsn");
Stringattitude=request.getParameter("atti");
Stringfilepath=request.getSession().getServletContext().getRealPath(
"data/comment.xml");
CommentBeanbean=newCommentBean(filepath);
Stringstr=bean.addComment(nikename,comment,attitude);
out.println(str);
} /***//**
*ThedoPostmethodoftheservlet.<br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalsto
*post.
*
*@paramrequest
*therequestsendbytheclienttotheserver
*@paramresponse
*theresponsesendbytheservertotheclient
*@throwsServletException
*ifanerroroccurred
*@throwsIOException
*ifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException...{ doGet(request,response);
} }
12下一页阅读全文
以上就是一个javascriptp+AJAX评论系统第1/2页。人类因有梦想而伟大,行动是梦想的开端。更多关于一个javascriptp+AJAX评论系统第1/2页请关注haodaima.com其它相关文章!