JSP and ajax are used to repeat the logs and save the instances.

Source: Internet
Author: User
Tags html page stringbuffer

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.

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.