一个jsp+AJAX评论系统第1/2页
程序员文章站
2022-03-26 09:08:12
这是一个简单的评论系统,使用了jdom(这边使用jdom-b9),实例使用jsp作为视图,结合使用ajax(用到prototype-1.4),servlet和javabea...
这是一个简单的评论系统,使用了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.后台javabean commentbean.java
/** *//**
* <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>
*/
public class 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
*/
public void setfilepath(string filepath) ...{
this.filepath = filepath;
}
/** *//**
* @return the builder
*/
public saxbuilder getbuilder() ...{
return builder;
}
/** *//**
* @param builder
* the builder to set
*/
public void 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>
* */
public class addcommentservlet extends httpservlet ...{
/** *//**
* serialversionuid long
*/
private static final long 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
*/
public void 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
*/
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception ...{
doget(request, response);
}
}
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.后台javabean commentbean.java
/** *//**
* <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>
*/
public class 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
*/
public void setfilepath(string filepath) ...{
this.filepath = filepath;
}
/** *//**
* @return the builder
*/
public saxbuilder getbuilder() ...{
return builder;
}
/** *//**
* @param builder
* the builder to set
*/
public void 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>
* */
public class addcommentservlet extends httpservlet ...{
/** *//**
* serialversionuid long
*/
private static final long 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
*/
public void 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
*/
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception ...{
doget(request, response);
}
}
1