天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

一個jsp+AJAX評論系統(tǒng)第1/2頁

這是一個簡單的評論系統(tǒng),使用了JDOM(這邊使用Jdom-b9),實例使用JSP作為視圖,結(jié)合使用AJAX(用到prototype-1.4),Servlet和JavaBean作為后臺處理,使用xml文件存儲數(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.后臺JavaBean  CommentBean.Java

/** *//**
 * <P>外星人是否存在評論系統(tǒng)</p>
 * @author ceun
 * 聯(lián)系作者:<br>
 *    <a href="mailto:[email protected]">ceun</a><br>
 * @version 1.0 2007-01-30 實現(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> 封裝對XML的操作</p>
 * @author ceun
 * 聯(lián)系作者:<br>
 *    <a href="mailto:[email protected]">ceun</a><br>
 * @version 1.0 2007-01-30 實現(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();
        }
    }
 /** *//**
  * 添加評論
  * @param nikename 評論者昵稱
  * @param comment 評論內(nèi)容
  * @param attitude 評論者的結(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("指定路徑有錯");
            e.printStackTrace();
        }
        return tdate.getText();
    }
/** *//**
 * 刪除指定ID的評論
 * @param commentId 評論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("重寫文件有出錯");
                e.printStackTrace();
            }
            return "成功刪除指定元素!";
        } else
            return "指定元素不存在!";
    }
/** *//**
 * 批量刪除評論
 * @param commentIdArgs 評論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("重寫文件有出錯");
                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
 * 聯(lián)系作者:<br>
 *    <a href="mailto:[email protected]">ceun</a><br>
 * @version 1.0 2007-01-30 實現(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);
    }

}

jsp技術(shù)一個jsp+AJAX評論系統(tǒng)第1/2頁,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产一区二区三区四区五在线观看 | 边摸边吃奶玩乳尖视频 | 校花爽好大快深点h | 小柔的性放荡羞辱日记 | 亚洲中文无码亚洲人在线观看- | 国产精品高清在线观看地址 | 欧美精品成人久久网站 | 午夜精品久久久久久久99蜜桃 | 受被三个攻各种道具PLAY | 亚洲男人的天堂久久精品麻豆 | 体育生爆操 | 国产69精品久久久久乱码 | caoporn免费视频在线 | 久久爱狠狠综合网 | 欧美片内射欧美美美妇 | 老师你奶真大下面水真多 | 天美传媒果冻传媒入口视频 | 国产精品日本欧美一区二区 | 青青热久久综合网伊人 | 最新国产成人综合在线观看 | 囯产精品麻豆巨作久久 | 国产午夜精品理论片影院 | 97在线观看免费视频 | 亚洲色噜噜狠狠网站 | 中文免费视频 | 国产成人啪精视频精东传媒网站 | 最近日本免费观看MV免费 | 小小水蜜桃3视频在线观看 小向美奈子厨房magnet | bt天堂午夜国产精品 | 精品国产自在天天线2019 | 色偷偷888欧美精品久久久 | 欧美一级黄色影院 | china中国gay偷拍 | 国产专区青青草原亚洲 | 亚洲日韩精品AV中文字幕 | 一道本av免费不卡播放 | 99久久夜色精品国产亚洲AV卜 | 热久久视久久精品18 | 亚洲精品www久久久久久久软件 | 久久精品无码成人国产毛 | 夜月视频直播免费观看 |