国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JSP > 正文

一個(gè)jsp+AJAX評(píng)論系統(tǒng)第1/2頁(yè)

2024-09-05 00:21:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這是一個(gè)簡(jiǎn)單的評(píng)論系統(tǒng),使用了JDOM(這邊使用Jdom-b9),實(shí)例使用JSP作為視圖,結(jié)合使用AJAX(用到prototype-1.4),Servlet和JavaBean作為后臺(tái)處理,使用xml文件存儲(chǔ)數(shù)據(jù)。
1.應(yīng)用目錄結(jié)構(gòu)如下:
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.后臺(tái)JavaBean CommentBean.java

/** *//**
* <P>外星人是否存在評(píng)論系統(tǒng)</p>
* @author ceun
* 聯(lián)系作者:<br>
* <a href="mailto:ceun@163.com">ceun</a><br>
* @version 1.0 2007-01-30 實(shí)現(xiàn)基本功能<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> 封裝對(duì)XML的操作</p>
* @author ceun
* 聯(lián)系作者:<br>
* <a href="mailto:ceun@163.com">ceun</a><br>
* @version 1.0 2007-01-30 實(shí)現(xiàn)基本功能<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();
}
}
/** *//**
* 添加評(píng)論
* @param nikename 評(píng)論者昵稱
* @param comment 評(píng)論內(nèi)容
* @param attitude 評(píng)論者的結(jié)論(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("指定路徑有錯(cuò)");
e.printStackTrace();
}
return tdate.getText();
}
/** *//**
* 刪除指定ID的評(píng)論
* @param commentId 評(píng)論ID
* @return 返回操作結(jié)果字符串(成功或失敗)
* */
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("重寫文件有出錯(cuò)");
e.printStackTrace();
}
return "成功刪除指定元素!";
} else
return "指定元素不存在!";
}
/** *//**
* 批量刪除評(píng)論
* @param commentIdArgs 評(píng)論ID數(shù)組
* @return 返回操作結(jié)果字符串(成功或失敗)
* */
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("重寫文件有出錯(cuò)");
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請(qǐng)求的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>后臺(tái)處理Servlet</p>
*2007-01-30
* * @author ceun
* 聯(lián)系作者:<br>
* <a href="mailto:ceun@163.com">ceun</a><br>
* @version 1.0 2007-01-30 實(shí)現(xiàn)基本功能<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);
}

}

當(dāng)前1/2頁(yè) 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 同江市| 通河县| 田东县| 扶余县| 青铜峡市| 莒南县| 长垣县| 高阳县| 姜堰市| 井陉县| 汝州市| 岳西县| 襄汾县| 阿尔山市| 阿坝| 成安县| 秀山| 上杭县| 渝北区| 那曲县| 卢氏县| 建昌县| 凤翔县| 连云港市| 从江县| 苏州市| 山阳县| 濉溪县| 华坪县| 云霄县| 仙桃市| 英德市| 土默特左旗| 湘乡市| 和平区| 甘南县| 永宁县| 吴忠市| 乌恰县| 崇义县| 南乐县|