Page 1/2 of a JSP + Ajax comment System

Source: Internet
Author: User

This is a simple comment system, using the JDOM (here using Jdom-b9), the instance uses JSP as the view, combined with Ajax (using prototype-1.4), Servlet and JavaBean as the background processing, use an XML file to store data.
1. The application directory structure is as follows:
Data
| -- Comment. xml
JS
| -- Prototype. js
| -- UFO. js (UTF-8 format)
CSS
| --Ufo.css
Images
| --Loading.gif
UFO. jsp (UTF-8 format)
WEB-INF
|-Lib
|-JDOM. Jar
|-Classes
...
|-Web. xml

/*************************************** ******
* Author: java619
* Time: 2007-02-14
**************************************** ******/

2. Background JavaBean commentbean. Java

/***//**
* <P> presence of aliens in the comment System </P>
* @ Author ceun
* Contact Author: <br>
* <A href = "mailto: ceun@163.com"> ceun </a> <br>
* @ Version 1.0 basic functions <br>
* Commentbean. Java
* Created on Jan 30,200 7 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> encapsulate XML Operations </P>
* @ Author ceun
* Contact Author: <br>
* <A href = "mailto: ceun@163.com"> ceun </a> <br>
* @ Version 1.0 basic functions <br>
*/
Public class commentbean ...{
Private string filepath;

Private saxbuilder builder = NULL;

Private document DOC = NULL;

Public commentbean ()...{

}
/***//**
* Initialize the XML file path and load the file
**/
Public commentbean (string path )...{
This. filepath = path;
Builder = new saxbuilder ();
Try ...{
Doc = builder. Build (filepath );
} Catch (jdomexception e )...{
System. Out. Print ("the specified XML file cannot be found ");
E. printstacktrace ();
} Catch (ioexception e )...{
System. Out. Print ("the specified file cannot be found ");
E. printstacktrace ();
}
}
/***//**
* Add comments
* @ Param nikename comments nickname
* @ Param Comment comment
* @ Param attitude comments conclusion (yes-yes, no-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 = 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 ");
// Clear spaces between comment Elements
Outputter. settrimallwhite (true );
Try ...{
Outputter. Output (Doc, new filewriter (filepath ));
} Catch (ioexception e )...{
System. Out. println ("the specified path is incorrect ");
E. printstacktrace ();
}
Return tdate. gettext ();
}
/***//**
* Delete A comment with a specified ID
* @ Param commentid comment ID
* @ Return returns the operation result string (successful or failed)
**/
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 ");
// Clear spaces between comment Elements
Outputter. settrimallwhite (true );
Try ...{
Outputter. Output (Doc, new filewriter (filepath ));
} Catch (ioexception e )...{
System. Out. println ("An error occurred while rewriting the file ");
E. printstacktrace ();
}
Return "the specified element is deleted successfully! ";
} Else
Return "the specified element does not exist! ";
}
/***//**
* Batch delete comments
* @ Param commentidargs comment ID Array
* @ Return returns the operation result string (successful or failed)
**/
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 ");
// Clear spaces between comment Elements
Outputter. settrimallwhite (true );
Try ...{
Outputter. Output (Doc, new filewriter (filepath ));
} Catch (ioexception e )...{
System. Out. println ("An error occurred while rewriting the file ");
E. printstacktrace ();
}
Return "the specified Element Set is deleted successfully! ";
} Else
Return "the specified Element Set does not exist! ";
}

/***//**
* @ 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. servlet addcommentservlet. Java for processing Ajax requests

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> backend processing servlet </P>
* 2007-01-30
** @ Author ceun
* Contact Author: <br>
* <A href = "mailto: ceun@163.com"> ceun </a> <br>
* @ Version 1.0 basic functions <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
* 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 );
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.