Using ajax to save data is a very simple event, which not only improves the user experience, but also can be known before many submissions, such as the existence of users and logs, next I will introduce an ajax method to determine whether logs are repeated and saved.
Html page
| The Code is as follows: |
Copy code |
|
<% @ Page language = "java" import = "java. util. *, jform. Allclass" contentType = "text/html; charset = gb2312" %>
<Jsp: useBean id = "log" class = "jBean. InsertMessage" scope = "page"/>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Add logs </title>
<Link rel = "stylesheet" href = "../css/main.css" type = "text/css"/>
<Script type = "text/javascript">
Function check_fir ()
{
If (document. log. log_title.value = "")
{
Window. alert ("Enter the log title! ");
Document. log. log_title.focus ();
Return (false );
}
If (document. log. showmen. value = "")
{
Window. alert ("Enter the log author! ");
Document. log. showmen. focus ();
Return (false );
}
If (document. log. content. value = "")
{
Window. alert ("Enter the log content! ");
Document. log. content. focus ();
Return (false );
}
If (document. log. log_title.length> 2000 | document. log. log_title.length <16)
{
Window. alert ("Enter your username! ");
Document. log. log_title.focus ();
Return (false );
}
}
// Create an XMLHttpRequest object
Function GetO ()
{
Var ajax = false;
Try
{
Ajax = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e)
{
Try
{
Ajax = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (E)
{
Ajax = false;
}
}
If (! Ajax & typeof XMLHttpRequest! = 'Undefined ')
{
Ajax = new XMLHttpRequest ();
}
Return ajax;
}
Function getMyHTML (serverPage, objID ){
Var ajax = GetO ();
// Get an html element and assign a value to the attribute of this element below
Var obj = document. all [objID];
// Set the Request Method and target and set it to asynchronous submission
Ajax. open ("post", serverPage, true );
Ajax. onreadystatechange = function ()
{
If (ajax. readyState = 4 & ajax. status = 200)
{
// InnerHTML is the attribute of the HTML element. If you do not understand the attribute, It is understood as the variable of the HTML element.
// Ajax. responseText is the return value of the server. The value is assigned to the attribute of the element id = log_title_msg.
// The innerHTML attribute or variable indicates the content between a group of start and end tags.
Obj. innerHTML = ajax. responseText;
}
}
// Send the request
Ajax. send (null );
}
Function checkLogTitle ()
{
GetMyHTML ("logrepeat. jsp? Log_title = "+ document. getElementById (" log_title "). value," log_title_msg ");
}
// This function is used to assign the attribute back to the original content when the user's focus is returned from other places to the log_title input box.
Function logTitleMsg (msg)
{
If (msg = 'Log _ title_msg ')
{
Document. all [msg]. innerHTML = "<div style = 'color: red; 'align = 'left'> the system automatically determines whether the log title is repeated. Follow this prompt to avoid publishing duplicate articles. </Div> ";
}
}
</Script>
</Head>
<Body>
<Form action = "addlog. jsp" method = "post" enctype = "multipart/form-data" name = "log" onSubmit = "return check_fir ()">
<Table class = "ta" width = "600" border = "0">
<Tr>
<Td width = "522" height = "189" valign = "top"> <table width = "600" border = "0">
<Tr>
<Td colspan = "2" align = "center"> <strong> Publish logs </strong> </td>
</Tr>
<Tr>
<Td width = "60" align = "left"> log title: </td>
<Td width = "452" align = "left"> <input id = "log_title" name = "log_title" type = "text" size = "40" onBlur = "javaScript: checkLogTitle (); "onFocus =" return logTitleMsg ('Log _ title_msg '); "/>
<Span id = "log_title_msg" class = "intro"> * </span> </td>
</Tr>
<Tr>
<Td align = "left"> log category: </td>
<Td align = "left"> <select name = "class_name">
<%
Collection c = log. showLogClass ();
Iterator it = c. iterator ();
While (it. hasNext ())
{
Allclass log_class = (Allclass) it. next ();
%>
<Option value = "<% = log_class.getClassname () %>"> <% = log_class.getClassname () %> </option>
<% }%>
</Select>
<Span class = "intro"> * </span> </td>
</Tr>
<Tr>
<Td align = "left"> Publisher: </td>
<Td align = "left"> <input name = "showmen" type = "text"/> </td>
</Tr>
<Tr>
<Td align = "left"> Upload File: </td>
<Td align = "left"> <input type = "file" name = "myfile"/>
</Td>
</Tr>
<Tr>
<Td height = "1" colspan = "2" align = "left" bgcolor = "00759b"> </td>
</Tr>
<Tr>
<Td colspan = "2" align = "left"> log Content: <span class = "intro"> (the content can contain up to 2000 words) </span> </td>
</Tr>
<Tr>
<Td colspan = "2" align = "left" valign = "top"> <textarea class = "content" rows = "10" cols = "50" name = "log_content"> </textarea> </td>
</Tr>
<Tr>
<Td colspan = "2" align = "left"> & nbsp; <input name = "Submit" type = "submit" value = "Publish log">
& Nbsp; <input name = "reset" type = "reset" id = "reset" value = "Clear Rewrite"> </td>
</Tr>
</Table> </td>
</Tr>
</Table>
</Form>
</Body>
</Html> |
Check the logrepeat. jsp code on the front-end processing page as follows:
| The Code is as follows: |
Copy code |
<% @ Page language = "java" import = "java. util. Date" contentType = "text/html; charset = gb2312" %>
<Jsp: directive. page import = "com. jspsmart. upload. SmartUpload"/>
<Jsp: useBean id = "addlog" class = "jform. Addlog" scope = "request"/>
<Jsp: useBean id = "insert" class = "jBean. InsertLog" scope = "request"/>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> log upload </title>
</Head>
<Body>
<%
String is = new String (request. getParameter ("log_title"). getBytes ("ISO-8859-1"), "GBK ");
Try {
Int isSave = insert. logRepeat (is );
If (isSave =-1 ){
Out. println ("<div style = 'color: red; 'align = 'left'> duplicate logs! </Div> ");
}
}
Catch (Exception ee) {out. println ("system error ");}
%>
</Body>
</Html> |
The InsertLog. java code of the background processing file is as follows:
| The Code is as follows: |
Copy code |
|
/*
* Log re-Query
*/
Int isSave = 1;
Public int logRepeat (String log_title)
{
ResultSet rs = null;
Statement stm = null;
StringBuffer sb = new StringBuffer ();
// Sb. append ("select * from log_add where log_title ="). append ("'");
// Sb. append ("this is a test log 3"). append ("'");
String sq = "select * from log_add where log_title =? ";
// String SQL = "select * from log_add where log_title = ";
Try {
Stm = dd. con. createStatement ();
Pstm = dd. con. prepareStatement (sq );
Pstm. setString (1, log_title );
Rsw.p;.exe cuteQuery ();
// Rs00000000.exe cuteQuery (sb. toString ());
// Rs00000000.exe cuteQuery (sq );
// Rs. next ();
If (rs. next () isSave =-1;
// System. out. println (sb. toString ());
// System. out. println (log_title );
// System. out. println (dd. con );
}
Catch (Exception e ){
System. out. print (e );
}
Return isSave;
}
|
The above is implemented using js ajax a long time ago. Now we can use jquery + ajax/"target =" _ blank "> jquery ajax to implement it, it makes jquery ajax much easier to create xmlhttp.