Home >Java >javaTutorial >How to implement search function in jsp

How to implement search function in jsp

(*-*)浩
(*-*)浩Original
2019-05-18 16:17:2111879browse

JSP method to implement search function: first write a form and include keyword input box and submit button; then the background program is responsible for extracting data from the database, with statements such as "select * from tablename where *** like '%?%';"; Finally, display the results returned by the database to the front page.

How to implement search function in jsp

#When doing background management, when there is a lot of data to be managed, the search function is inevitably needed. This is easier, faster, and saves time and effort. Therefore, search has become an essential part! There are two ways to search:

1. Implement it in the database. By borrowing the functions of the database, you can easily implement the search function. Then what the background needs to do is organize and display the specifications to facilitate customer operations and Check!

2. Display all the content, use industry search, search one by one, until you find those items that satisfy customers! I found the industry search code from the Internet and implemented it in javaScript:

The code is as follows:

In the jsp industry code:

<input type="text" id="searchstr" name="searchstr" class="textbox" size="20">  
<input type="button" value="页内查找" onclick="javascript:findIt();" class="sbttn">

The code in the javascript script:

var DOM = (document.getElementById) ? 1 : 0;  
  var NS4 = (document.layers) ? 1 : 0;  
  var IE4 = 0;  
  if (document.all)  
  {  
  IE4 = 1;  
  DOM = 0;  
  }  
  
  var win = window; 
  var n = 0;  
  
  function findIt() {  
  if (document.getElementById("searchstr").value != "")  
  findInPage(document.getElementById("searchstr").value);  
  }  
  
  
  function findInPage(str) {  
  var txt, i, found;  
  
  if (str == "")  
  return false;  
  
  if (DOM)  
  {  
  win.find(str, false, true);  
  return true;  
  }  
  
  if (NS4) {  
  if (!win.find(str))  
  while(win.find(str, false, true))  
  n++;  
  else  
  n++;  
  
  if (n == 0)  
  alert("未找到指定内容.");  
  }  
  
  if (IE4) {  
  txt = win.document.body.createTextRange();  
  
  for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {  
  txt.moveStart("character", 1);  
  txt.moveEnd("textedit");  
  }  
  
  if (found) {  
  txt.moveStart("character", -1);  
  txt.findText(str);  
  txt.select();  
  txt.scrollIntoView();  
  n++;  
  }  
  else {  
  if (n > 0) {  
  n = 0;  
  findInPage(str);  
  }  
  else  
  alert("未找到指定内容.");  
  }  
  }  
  
  return false;  
  }

This depends on what you want to search for. If it is the data in your database, then it is very simple. You write a form and include keyword input. box and submit button, submit to the background, and then the background program is responsible for extracting data from the database, select * from tablename where *** like '%?%' ;

*** is in your database record Keyword list,? It is the search keyword you passed from the front desk. Finally, just display the results returned by the database to the front page.

In comparison, I personally prefer the first method, although it will bring certain pressure to the database! But the first one is more intuitive, vivid and easy to operate! And from a user perspective, we tend to prefer the first one!

The above is the detailed content of How to implement search function in jsp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn