当前位置: 编程技术>java/j2ee
一个jsp+AJAX评论系统第1/2页
来源: 互联网 发布时间:2014-10-16
本文导语: 这是一个简单的评论系统,使用了JDOM(这边使用Jdom-b9),实例使用JSP作为视图,结合使用AJAX(用到prototype-1.4),Servlet和JavaBean作为后台处理,使用xml文件存储数据。 1.应用目录结构如下: data |--comment.xml js |--prototype.js |--ufo.js(UTF...
这是一个简单的评论系统,使用了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
/** *//**
*
* @author ceun
* 联系作者:
* ceun
* @version 1.0 2007-01-30 实现基本功能
* 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;
/** *//**
*
* @author ceun
* 联系作者:
* ceun
* @version 1.0 2007-01-30 实现基本功能
*/
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
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
/** *//**
*
外星人是否存在评论系统
* @author ceun
* 联系作者:
* ceun
* @version 1.0 2007-01-30 实现基本功能
* 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;
/** *//**
*
封装对XML的操作
* @author ceun
* 联系作者:
* ceun
* @version 1.0 2007-01-30 实现基本功能
*/
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