We can create an AJAX example in Java which checks whether the given email id exists in the database or not.
Steps to create email finder example using AJAX in Java
You need to follow following steps:
- Create table in database
- load the org.json.jar file
- Create input form
- Create server side page to search employee using name
Create table in database
In this example, we are using oracle 10g database. Here, we have created a table “user100” which has following data.

Load the org.json.jar file
download this example, we have included the org.json.jar file inside the WEB-INF/lib directory.
Create input form
In this page, we have created a form that gets input from the user to find email. When you click on the check availability button, it tells whether email id is available or not.index.html
<!DOCTYPE html>
<html>
<head>
<title>Email Finder Example</title>
<script>
var request;
function sendInfo(){
var email=document.vinform.email.value;
var url="emailfinder.jsp?email="+email;
if(window.XMLHttpRequest){
request=new XMLHttpRequest();
}
else if(window.ActiveXObject){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
try{
request.onreadystatechange=getInfo;
request.open("GET",url,true);
request.send();
}catch(e){alert("Unable to connect to server");}
}
function getInfo(){
if(request.readyState==4){
var val=request.responseText;
document.getElementById('mylocation').innerHTML=val;
}
}
</script>
</head>
<body>
<marquee><h1>AJAX Email Checker Example</h1></marquee>
<form name="vinform">
<input type="email" name="email" placeholder="enter email"/>
<input type="button" onclick="sendInfo()" value="Check Availability"/>
<span id="mylocation"></span>
</form>
</body></html>
Create server side page to process the request
In this jsp page, we are writing the database code to search email id.emailfinder.jsp
<%@ page import="java.sql.*" %>
<%
String email=request.getParameter("email");
if(email.contains("@")&&email.contains(".")){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement("select * from user100 where email=?");
ps.setString(1,email);
ResultSet rs=ps.executeQuery();
if(rs.next()){
out.print("Unavailable! <img src='unchecked.gif'/>");
}else{
out.print("Available! <img src='checked.gif'/>");
}
}catch(Exception e){out.print(e);}
}else{
out.print("Invalid email!");
}
%>
Output


Leave a Reply