jsp 评论功能 ajax,一个jsp+AJAX评论系统第1/2页

一个jsp+AJAX评论系统第1/2页

更新时间:2007年05月19日 00:00:00   作者:

这是一个简单的评论系统,使用了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;

/** *//**

*

 封装对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 

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 dist = new ArrayList();

outer:for (int i = 0; i 

Element comment = (Element) comments.get(i);

String id = comment.getAttributeValue("id");

for (int j = 0; 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 

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;

/** *//**

后台处理Servlet

*2007-01-30

* * @author ceun

* 联系作者:

*    ceun

* @version 1.0 2007-01-30 实现基本功能

* */

public class AddCommentServlet extends HttpServlet ...{

/** *//**

* serialVersionUID long

*/

private static final long serialVersionUID = 1L;

/** *//**

* The doGet method of the servlet. 

*

* 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. 

*

* 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);

}

}

相关文章

1a1b05c64693fbf380aa1344a7812747.png

下面的代码实现的功能:写一个JSP页,动态生成一个验证码,存储在session作用范围内,并以图像形式返回给客户端显示2014-09-09

4f55910a645b073bc4fc65dc10dc14bd.png

web工程中的web.xml文件有什么作用呢?它是每个web.xml工程都必须的吗?2009-07-07

0ea3c7666119d5615e582f823fb3fad6.png

这篇文章主要介绍了javascript判断文件是否存在实例代码的相关资料,需要的朋友可以参考下2017-01-01

4f96a78db829b1556ff16de21e013c7a.png

这篇文章主要介绍了JSP 开发之Spring BeanUtils组件使用的相关资料,这里提供实例帮助大家理解如何使用Spring BeanUtils组件,需要的朋友可以参考下2017-08-08

8cc1031babc6aff2319f1c6af8544aa0.png

这篇文章主要介绍了Spring @Service 和 @Resource 注解的区别的相关资料,需要的朋友可以参考下2017-03-03

0c932a99bb7b6f23c937db507070cc7b.png

在linux上建jsp環境...2006-10-10

cca732bf65a93ed2ec0ac80c638460fe.png

下载jQuery下来以后,将它导入到workbench中去,就会有很多黄色的警告(感叹号)出现,下面为大家介绍如何解决这个问题2014-05-05

2d9f31f2af7b675a3d153d2b7f1035a7.png

这篇文章主要介绍了jsp与sql语句的混合使用,需要的朋友可以参考下2014-03-03

b452cee8ec5cd9e58ab98eba17281e59.png

JSP由浅入深(1)—— 熟悉JSP服务器...2006-10-10

f4838ec7e2d4da28e0b57d4e852dadd4.png

有关GET方式提交的含有特殊字符的参数JS方面可以使用前面一文中的encode等方法。2008-12-12

最新评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值