Implementation Code for automatic saving of ajax + jsp drafts

Source: Internet
Author: User

1. form part (index.html)

The first step is to fill in the form page, use a DIV with the ID AutoSaveMsg to display the returned information, and use a CheckBox with the ID Draft_AutoSave to determine whether to save automatically, name the Textarea ID as message. At the same time, in order to meet the needs of multiple users at the same time, with the user name, each user's draft is saved separately. For convenience, here we will remove some modifiers, which looks clear.

<H2> automatically save the draft of the AJAX application
<! -- The default username is NONAME -->

User name:
<Input type = "text" name = "memName" id = "memName"
Size = "20" value = "NONAME" disabled = "true"/>

<! -- Call the Automatic save status setting function in The onclick event of the Automatic save option -->

<Input onclick = "SetAutoSave ();" type = "checkbox" id = "Draft_AutoSave" value = "1" checked = "true"/> auto save?
<Br/>

Content:
<Textarea cols = 40 rows = 8 id = "message"> the edited content is automatically saved for restoration. </textarea> <br/>

<! -- AutoSaveMsg displays the returned information -->
<Div id = "AutoSaveMsg"> </div> <br/>

<Input type = "submit" onclick = "Save ();" value = "Save"/>

<! -- Call the function to restore the last saved draft -->
<Input type = "button" onclick = "AutoSaveRestore ();" value = "Restore"/>
</Div>
</Div>

<! -- Place the JS Code after all objects to avoid the error that the object does not exist when the page is not loaded. -->
| <! -- AJAX -->
<Script type = "text/javascript" src = "ajaxrequest. js"> </script>
<! -- Automatically save the code -->
<Script type = "text/javascript" src = "autosave. js"> </script>


Ii. autosave. jsp ):

// Set global variables first
// FormContent object to save
Var FormContent;
// Display the object of the returned information
Var AutoSaveMsg = document. getElementById ("AutoSaveMsg ");
// User Name
Var memName = document. getElementById ("memName"). value;
// Automatic Storage Interval
Var AutoSaveTime = 10000;
// Timer object
Var AutoSaveTimer;
// First set the Automatic save status
SetAutoSave ();
// Automatically save the Function
Function AutoSave (){
FormContent = document. getElementById ("message ");
// If the content or user name is empty, no processing is performed and a direct response is returned.
If (! FormContent. value |! MemName) return;
// Create an AJAXRequest object
Var ajaxobj = new AJAXRequest;
Ajaxobj. url = "autosave. jsp ";
Ajaxobj. content = "action = AutoSave & memname =" + memName + "& postcontent =" + FormContent. value;
Ajaxobj. callback = function (xmlObj ){
// Display feedback
AutoSaveMsg. innerHTML = xmlObj. responseText;
}
Ajaxobj. send ();
}
// Set the Automatic save status Function
Function SetAutoSave (){
// Is it automatically saved?
If (document. getElementById ("Draft_AutoSave"). checked = true)
// Yes, set the timer
AutoSaveTimer = setInterval ("AutoSave ()", AutoSaveTime );
Else
// No, clear the timer
ClearInterval (AutoSaveTimer );
}
Function AutoSaveRestore () {// restore the last saved draft
AutoSaveMsg. innerHTML = "recovering... please wait ...... "
FormContent = document. getElementById ("message ");
// If the user name is blank, no processing is performed and a direct response is returned.
If (! MemName) return;
// Create an AJAXRequest object
Var ajaxobj = new AJAXRequest;
Ajaxobj. url = "autosave. jsp ";
Ajaxobj. content = "action = Restore & memname =" + memName;
Ajaxobj. callback = function (xmlObj ){
// Display feedback
If (xmlObj. responseText! = ""){
// Restore draft
Var s = xmlObj. responseText. replace (/^ [\ n | \ r \ n] * | [\ n | \ r \ n] * $/g, ''); // remove the first and last empty rows
FormContent. innerText = s;
// Prompt that the user has been restored successfully
AutoSaveMsg. innerHTML = "restored successfully ";
}
}
Ajaxobj. send ();
}
Function Save () {// Save the content to the database.
FormContent = document. getElementById ("message ");
// If the content or user name is empty, no processing is performed and a direct response is returned.
If (! FormContent. value |! MemName) return;
// Create an AJAXRequest object
Var ajaxobj = new AJAXRequest;
Ajaxobj. url = "autosave. jsp ";
Ajaxobj. content = "action = Save & memname =" + memName + "& postcontent =" + FormContent. value;
Ajaxobj. callback = function (xmlObj ){
// Display feedback
AutoSaveMsg. innerHTML = xmlObj. responseText;
}
Ajaxobj. send ();
}
3. autosave. jsp is used to save the draft in the background:
Program code:

<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. util. *" %>
<% @ Page import = "java. io. *" %>

<%
String PostContent, memName, action;
String filename;
File f;
FileWriter fw;
Action = request. getParameter ("action"); // you can call this operation to save or restore a draft.

// Obtain the user name
MemName = request. getParameter ("memname ");

// Obtain the draft content
PostContent = request. getParameter ("postcontent ");

Filename = memName + ". txt"; // Save the draft File
Filename = request. getRealPath ("/temp/" + filename );
If (action. equals ("Save") | action. equals ("AutoSave") {// the two actions are merged. The code saved to the database is not written.
F = new File (filename );
If (! F. exists () // if the file is not saved, create
{
F. createNewFile ();
}
Fw = new FileWriter (filename); // create a FileWrite object and set it to be referenced by the fw object variable
PostContent = new String (PostContent. getBytes ("ISO8859_1"), "UTF-8 ");

Fw. write (PostContent );
Fw. close (); // close the file
Out. println ("last in" + new Date (). toString () + "automatically saved !! 1 ");

} Else if (action. equals ("Restore") {// Restore operation
FileReader fr = new FileReader (filename); // create a FileReader object and set it to be referenced by the fr object variable
BufferedReader br = new BufferedReader (fr); // create a BufferedReader object and set the variable cited by the br object
StringBuffer bf = new StringBuffer ();
String Line;
While (Line = br. readLine ())! = Null) {// read a row of data
Bf. append (Line + "\ n ");
}
Out. print (bf. toString (). trim ());
} Else {
Out. println ("error occurred ");
}

%>

4. Download the AJAX class (ajaxrequest. js.

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.