一個jsp+AJAX評論系統第1/2頁

來源:互聯網
上載者:User

這是一個簡單的評論系統,使用了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);
}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.