step 1: create table

create table ajax(name varchar(25),pwd varchar(25));

Step 2: insert some values

insert into ajax values('abc','abc');

table is created as follow




Step 3: create user form with ajax validation code

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
function prudhvi()
{
 

var xmlhttp;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
 
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
     // alert("nnnnnn2");
    document.getElementById("id").innerHTML=xmlhttp.responseText;
    }
  }
    //alert("nnnnnn3");
    var a=document.getElementById("name").value;
   // alert(a);
xmlhttp.open("GET","s?q="+a,true);
xmlhttp.send();
}
</script>
</head>

<body>
    <div id="id"></div>
   
        UserName: <input type="text" name="name" id="name" onkeyup="prudhvi()">
     
</body>
</html>

Step 4: write servlet program


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EmployeeServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       
      //  System.out.println("hello from servlet");
        PrintWriter out = response.getWriter();
        String ss=request.getParameter("q");
       // System.out.println(ss+"*****************");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/xxx", "root", "root");
            PreparedStatement ps=con.prepareStatement("select * from ajax where name=?");
            ps.setString(1,ss);
          ResultSet rs=ps.executeQuery();
          if(rs.next()){
              out.println("name is already exist");
          }
          else
                out.println("name doesnot already exist");
             
        }catch(Exception e){
        e.printStackTrace();
        }
    }

   
}

Step 5: generate web.xml file as below by using form and servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>EmployeeServlet</servlet-name>
        <servlet-class>EmployeeServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>EmployeeServlet</servlet-name>
        <url-pattern>/s</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>


Step 6: run project   (before run your project check your database)




after enter some name in form





if we enter existing user name validations are like below






0 comments:

Post a Comment