The background database is Oracle:
-- Create a query information table
Create Table searchinfo
(
ID number not null primary key, -- number
Content varchar2 (100) not null, -- Query content
Count number not null -- number of queries
)
-- Create Sequence
Create sequence seq_searchinfo;
-- Create a stored procedure for data insertion
Create or replace procedure proc_add (vcontent varchar2, vresult out varchar2)
As
Vcount number;
Begin
Select count (*) into vcount from searchinfo where content = vcontent;
If vcount = 0 then
Insert into searchinfo values (seq_searchinfo.nextval, vcontent, 1 );
Else
Update searchinfo set COUNT = count + 1 where content = vcontent;
End if;
Vresult: = 'success ';
Exception
When others then
Vresult: = 'fail ';
End; First you need to import DWR. jar to the WEB-INF \ lib directory, and then configure dwrservlet in the web. xml file
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-Name> DWR-invoker </servlet-Name>
<Servlet-class> UK. Ltd. getahead. DWR. dwrservlet </servlet-class>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> true </param-value>
</Init-param>
</Servlet>
<Servlet>
<Description>
This is the description of my J2EE component
</Description>
<Display-Name>
This is the display name of my J2EE component
</Display-Name>
<Servlet-Name> servletx </servlet-Name>
<Servlet-class> control. servletx </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> DWR-invoker </servlet-Name>
<URL-pattern>/DWR/* </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> servletx </servlet-Name>
<URL-pattern>/servletx </url-pattern>
</Servlet-mapping>
</Web-app>
Then you need to create dwr in the WEB-INF \ lib directory. XML file, declare the classes to be called by JavaScript, and publish methods. Of course, all methods are published by default. It should be noted that if the parameters or return values of class methods are bean, you also need to use the convert label
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype DWR public "-// getahead limited // DTD direct Web remoting 2.0 //" http://getahead.org/dwr/dwr20.dtd ">
<DWR>
<Allow>
<Create creator = "new" javascript = "History">
<Param name = "class" value = "operation. opersearchinfo"/>
<Include method = "gethistory"/>
</Create>
<Convert converter = "Bean" match = "entity. searchinfo">
<Param name = "include" value = "content, Count"/>
</Convert>
</Allow>
</DWR>
After the two XML files are configured, you can enter http: // localhost: 9527/project name/DWR in the address bar for testing. If the test is successful, available classes and methods are displayed.
Create the Controller servletx. java. In this example, it only checks whether the user's search operation is successful and outputs it in the form of a console.
Package control;
Import java. Io. ioexception;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import Operation .*;
Public class servletx extends httpservlet {
Private Static final long serialversionuid = 1l;
Public servletx (){
Super ();
}
Public void destroy (){
Super. Destroy (); // just puts "Destroy" string in log
// Put your code here
}
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
String Model = request. getparameter ("model ");
If (model. Equals ("Search ")){
String content = translation. transcode (request. getparameter ("content "));
Opersearchinfo OBJ = new opersearchinfo ();
If (obj. Search (content )){
System. Out. println ("success ");
} Else {
System. Out. println ("fail ");
}
}
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
This. doget (request, response );
}
Public void Init () throws servletexception {
// Put your code here
}
}
Create the object bean corresponding to the database table, searchinfo. Java File
Package entity;
/***//**
* Search information table objects
* @ Author extraordinary DZ
*
*/
Public class searchinfo {
Private int ID; // number
Private string content; // query the content
Private int count; // number of queries
Public String getcontent (){
Return content;
}
Public void setcontent (string content ){
This. content = content;
}
Public int getcount (){
Return count;
}
Public void setcount (INT count ){
This. Count = count;
}
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
}
Create the class opersearchinfo. Java and method to operate on the database table searchinfo
Package operation;
Import java. SQL .*;
Import java. util .*;
Import dB. database;
Import entity. searchinfo;
/***//**
* This class includes all the operations on the searchinfo table.
* @ Author extraordinary DZ
*
*/
Public class opersearchinfo {
/***//**
* Click the search button and run
* @ Param content
* @ Return
*/
Public Boolean search (string content ){
Boolean flag = false;
Database DB = new database ();
Connection con = dB. getconnection ();
Callablestatement cs = NULL;
Try {
Cs = con. preparecall ("{call proc_add (?,?)} ");
CS. setstring (1, content );
CS. registeroutparameter (2, java. SQL. types. Char );
Cs.exe cute ();
If (CS. getstring (2). Equals ("success ")){
Flag = true;
}
} Catch (exception e ){
System. Out. println ("proc exception" + E. getmessage ());
E. printstacktrace ();
} Finally {
Try {
Con. Close ();
} Catch (exception ex ){
System. Out. println ("disconnection exception" + ex. getmessage ());
Ex. printstacktrace ();
}
}
Return flag;
}
/***//**
* Obtain the first 10 pieces of information similar to the information in the interface text box.
* @ Param content data in the text box
* @ Return similar information
*/
Public arraylist gethistory (string content ){
Database DB = new database ();
Connection con = dB. getconnection ();
Resultset rs = NULL;
Arraylist <searchinfo> aryresult = new arraylist <searchinfo> ();
String SQL = "Select Content, count from searchinfo where content"
+ "Like? And rownum <= 10 order by Count DESC ";
Try {
If (! Content. Equals ("")){
Preparedstatement PSTN = con. preparestatement (SQL );
PSTN. setstring (1, content + "% ");
Rs = pstn.exe cutequery ();
While (Rs. Next ()){
Searchinfo info = new searchinfo ();
Info. setcontent (Rs. getstring (1 ));
Info. setcount (Rs. getint (2 ));
Aryresult. Add (Info );
}
}
} Catch (exception e ){
System. Out. println ("An error occurred while retrieving Historical query information" + E. getmessage ());
E. printstacktrace ();
} Finally {
Try {
Con. Close ();
} Catch (exception ex ){
System. Out. println ("disconnection exception" + ex. getmessage ());
Ex. printstacktrace ();
}
}
Return aryresult;
}
}
The translation class is used to solve the encoding problem of data transmission.
Package operation;
Import java. Io .*;
/***//**
* This class is used to solve the encoding problem.
* @ Author extraordinary DZ
*
*/
Public class translation {
Public translation (){
}
Public static string transcode (string Str ){
String temp = NULL;
If (STR = NULL ){
Temp = "";
}
Try {
Temp = new string (Str. getbytes ("iso8859-1"), "UTF-8 ");
} Catch (unsupportedencodingexception ex ){
}
Return temp;
}
}
Database is used to obtain database connections
Package dB;
Import java. SQL .*;
/***//**
* This class is used to obtain database connections.
* @ Author extraordinary DZ
*
*/
Public class database {
Private connection con;
Private string driver = "oracle. JDBC. Driver. oracledriver ";
Private string url = "JDBC: oracle: thin: @ localhost: 1521: daizhenghenry ";
Private string uid = "daizheng ";
Private string Pwd = "daizheng ";
Public connection getconnection (){
Try {
Class. forname (driver );
Con = drivermanager. getconnection (URL, uid, PWD );
} Catch (exception e ){
System. Out. println ("connection exception" + E. getmessage ());
E. printstacktrace ();
}
Return con;
}
}
Finally, the JSP page
<% @ Page Language = "Java" pageencoding = "UTF-8" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> simulated search engine (from: Network Base Camp http: // www.qqview.com) </title>
<Style type = "text/CSS">
Html
{}{
Filter: progid: DXImageTransform. Microsoft. basicimage (grayscale = 1 );
}
</Style>
<SCRIPT type = 'text/JavaScript 'src = '/ajaxtest/DWR/interface/history. js'> </SCRIPT>
<SCRIPT type = 'text/JavaScript 'src = '/ajaxtest/DWR/engine. js'> </SCRIPT>
<SCRIPT type = 'text/JavaScript 'src = '/ajaxtest/DWR/util. js'> </SCRIPT>
<Script language = "JavaScript">
/** // * Process user-related searches */
Function Change (data ){
// Obtain the number of rows in the table
VaR COUNT = Document. getelementbyid ('tab '). Rows. length;
// Delete all rows if the table has rows
If (count> 0 ){
For (VAR I = count-1; I> = 0; I --){
Document. getelementbyid ('tab '). deleterow (I );
}
}
// If a search record exists
If (data. length> 0 ){
Document. getelementbyid ('related'). style. Display = '';
Document. getelementbyid ('x'). style. Display = '';
For (VAR I = 0; I <data. length; I ++ ){
VaR objtr = Document. getelementbyid ('tab'). insertrow ();
VaR objtd1 = objtr. insertcell (0 );
Objtd1.innerhtml = "<input readonly type = 'text '"
+ "Size = '35' name = 'txthistory 'style = 'border: none; Background: # ffff '"
+ "Value = '" + data [I]. content + "'onmouseover = 'overchangecolor (this )'"
+ "Onmouseleave = 'leavechangecolor (this )'"
+ "Onclick = 'clickhistory (this) '> ";
VaR objtd2 = objtr. insertcell (1 );
Objtd2.innerhtml = "<input type = 'text' name = 'result' readonly"
+ "Size = '15' style = 'border: none; Background: # ffffff; text-align: right '"
+ "Value = '" + data [I]. Count + "result" + "'align = 'right'/> ";
Objtd2.align = 'right ';
}
} Else {
Document. getelementbyid ('related'). style. Display = 'none ';
}
}
/** // * Disable history query records */
Function myclose (){
Document. getelementbyid ('related'). style. Display = 'none ';
}
/** // * Run the command above the search content */
Function overchangecolor (object ){
VaR histories = Document. getelementsbyname ('txthistory ');
For (VAR I = 0; I // If the current cursor stays on a row
If (histories [I]. style. Background = '# ccffcc '){
Histories [I]. style. Background = '# ffff ';
VaR tdobj1 = histories [I]. parentelement; // TD
VaR trobj1 = tdobj1.parentelement; // tr
VaR childobj1 = trobj1.childnodes (1 );
VaR X1 = childobj1.childnodes (0 );
X1.style. Background = '# ffff ';
Break;
}
}
Object. style. Background = '# ccffcc ';
VaR tdobj = object. parentelement; // TD
VaR trobj = tdobj. parentelement; // tr
VaR childobj = trobj. childnodes (1 );
VaR x = childobj. childnodes (0 );
X. style. Background = '# ccffcc ';
}
/** // Execute the command when you move the mouse away from the top of the relevant search content */
Function leavechangecolor (object ){
Object. style. Background = '# ffff ';
VaR tdobj = object. parentelement; // TD
VaR trobj = tdobj. parentelement; // tr
VaR childobj = trobj. childnodes (1); // TD
VaR x = childobj. childnodes (0); // Input
X. style. Background = '# ffff ';
}
/** // * Click the relevant search content to execute */
Function clickhistory (object ){
Document. frm. content. value = object. value;
Document. getelementbyid ('related'). style. Display = 'none ';
FRM. Submit ();
}
/** // * Handle the event by pressing the button in the search box */
Function keyselecthistory (){
VaR nkeycode = Window. event. keycode;
If (nkeycode = 38 | nkeycode = 40 ){
VaR COUNT = Document. getelementbyid ('tab '). Rows. length;
VaR temprowid; // record the row where the cursor is located
VaR flag = false; // identifies whether there are rows that have been discolored
If (count> 0 & (nkeycode = 38 | nkeycode = 40) {// If search information exists
VaR histories = Document. getelementsbyname ('txthistory ');
For (VAR I = 0; I // If the current cursor stays on a row
If (histories [I]. style. Background = '# ccffcc '){
Temprowid = I;
Flag = true;
Break;
}
}
If (! Flag ){
Temprowid = 0;
}
If (nkeycode = 38) {// up key
If (temprowid> 0 ){
Leavechangecolor (histories [temprowid]);
Overchangecolor (histories [temprowid-1]);
Document. frm. content. value = (histories [temprowid-1]). value;
} Else {
Leavechangecolor (histories [0]);
Overchangecolor (histories [count-1]);
Document. frm. content. value = (histories [count-1]). value;
}
} Else if (nkeycode = 40) {// down key
If (temprowid = 0 & histories [0]. style. Background! = '# Ccffcc '){
Overchangecolor (histories [0]);
Document. frm. content. value = histories [0]. value;
} Else if (temprowid <count-1 ){
Leavechangecolor (histories [temprowid]);
Overchangecolor (histories [temprowid + 1]);
Document. frm. content. value = (histories [temprowid + 1]). value;
} Else {
Leavechangecolor (histories [temprowid]);
Overchangecolor (histories [0]);
Document. frm. content. value = histories [0]. value;
}
}
}
} Else {// when the content in the search box changes (manually change it, rather than by using the up/down key)
VaR STR = Document. frm. content. value;
History. gethistory (STR, change );
}
}
</SCRIPT>
</Head>
<Body>
<B> simulated search engine </B>
<Br/>
<Form action = "servletx" name = "frm" method = "Post">
<Br/>
<Br/>
<Input type = "hidden" name = "model" value = "Search"/>
<Input type = "text" size = "55" name = "content"
Onkeyup = "keyselecthistory ()"/>
<Input type = "Submit" value = "Search"/>
<Div id = "related"
Style = "border: 1px solid # f990033; display: 'none'; width: 335;">
<Table id = "tab" cellpadding = "0" border = "0" cellspacing = "0">
</Table>
<A id = "X" href = 'javascript:; 'onclick = 'myclose ()'
Style = "display: none"> close </a>
</Div>
</Form>
</Body>
</Html>
TheArticleReproduced from the network base camp: http://www.qqview.com/Dev/AJAX/200852719810.Html