Simple Search Engine Like Google using AJAX


Simple Search Engine like Google using AJAX 

This tutorial for those peoples who are looking for the sample code for a simple search engine like google. Here i am, showing my own code for a simple search engine. Here are two files one for client side file which has one text box for your query and second one is the back end code for searching data from database.

This example in HTML and JSP with AJAX support.
So you need to have the knowledge of JSP and AJAX.

search.html

<html>
<head>
<title>Search Anything</title>
<script type="text/javascript">
function GetBObject()
{
 var http;
 if(window.XMLHttpRequest)
  http=new XMLHttpRequest();
 else
  http=new ActiveXObject("Microsoft.XMLHTTP");

 return http;
}
var https=GetBObject();
function Search(obj)
{
 var URL="Search.jsp?s="+obj;
 https.open("GET",URL);
 https.onreadystatechange=handleSearch;
 https.send(null);
}
function handleSearch()
{
 if(https.status==200&&https.readyState==4)
 {
  document.getElementById("show").innerHTML=https.responseText;
 }
}
</script>
</head>
<body>

  <input type="text" style="border-radius:5px;height:50px;width:400px;margin-left:200px;margin-top:200px;font-size:40px;" onkeyup="Search(this)" placeholder="Search Anything">
  <div id="show"></div>
</body>
</html>


Search.jsp

<%@page language="java" import="java.sql.*" %>
<%
String search=request.getParameter("s");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:data");
ResultSet rst;
Statement stm=con.createStatement();
rst=stm.executeQuery("select title,url,description from searchdata where title like '"+search+"%'");
String data="<div>";
while(rst.next())
{
 data+="<table><tr><td>"+rst.getString(1)+"</td></tr><tr><td>"+rst.getString(2)+"</td></tr><tr><td>"+rst.getString(3)+"</td></tr></table>";
}
data+="</div>";
out.print(data);
%>

{ 0 comments... read them below or add one }

Post a Comment