时间:2021-05-25
这是一个简单的评论系统,使用了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>
*<ahref="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>
*<ahref="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>
*<ahref="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下一页阅读全文
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
jsp+ajax实现无刷新,鼠标离开文本框即验证用户名,操作如下:新建一个输入页面,起名为input.jsp,复制代码代码如下:jsp+ajax实现无刷新_鼠标
本文实例讲述了jsp+ajax实现无刷新上传文件的方法。分享给大家供大家参考,具体如下:列表页:selectaddress.jspjs页:ajax_edit.j
本文主要是介绍了运用JSP+ajax实现分类查询功能的实例代码,分享给大家供大家参考,具体如下:这次是对小学期关于大学毕业生信息管理系统课程设计中遇到的一些问题
1.前台jsp,新建一个下拉控件2.js部分,建一个function方法,利用ajax,指向'getAllTypes.action'的servlet部分,获取传
我们浏览内容的时候弹出一个对话框,显示Therewasaproblemwiththerequest,是因为我们用了帝国官方提供的内容页评论AJAX分页显示插件,