Searching button using Ajax | How Ajax use in Search and combo list populate in application in Java

Hi Friend
There is Scenario in my application where in Jsp page there is Search button and two combo list ,when we search for department by entering any starting  value populate the all department starting from enter string, and when we select the department . all doctor list will populate doctor list in particular department,
when we select doctor name from list detail of doctor list display in Jsp page



Code:
    DoctorDetail.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
var httpRequest;
function getDepartment()
    {
   
    var searchDep=document.loginform.attachToCaseNo.value;
    if (searchDep==""){
        alert("Please enter Departement to search..");
        document.loginform.attachToCaseNo.focus();
        return;
    }
    var searchKey = "mac";
    searchDep = searchDep.toUpperCase();
    //alert(searchDep);
    var url = "getDep?fano="+searchDep+"&searchKey="+searchKey;
//alert (url);
    if (window.ActiveXObject)
    {

        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {

        httpRequest = new XMLHttpRequest();
    }
    
    httpRequest.open("GET", url, true);
    httpRequest.onreadystatechange = function() {processRequest(); } ;
    httpRequest.send(null); 

    }
 function processRequest()
{
    if (httpRequest.readyState == 4)
    {
        if(httpRequest.status == 200)
       
        {
//alert(httpRequest.responseText)
         var FanoListXML = httpRequest.responseXML.getElementsByTagName("FanoList")[0];
         var comboSize=FanoListXML.childNodes.length;
           
            if (comboSize==0) {
                alert("Please Enter Departement Number");
              
                document.loginform.attachToCaseNo.value="";
                document.loginform.attachToCaseNo.focus();
               var CrListXML = new Array();
                    //updateHTML4(CrListXML);
                FanoListXML=new Array();
                updateHTML(FanoListXML);

                return;
            }
            updateHTML(FanoListXML);
           
        }
        else
        {
            alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
        }
    }
}
 

function updateHTML(FanoListXML)
{

   var len = window.document.loginform.case_id_in_temp.options.length;

    while(len>0) {
        len = len -1;
        window.document.loginform.case_id_in_temp.options[len] = null;
    }//end while       

var CrListXML = new Array();
window.document.loginform.case_id_in_temp.options[0] = new Option("Select","0");

      for (loop = 0; loop < FanoListXML.childNodes.length; loop++) {

       var SNS = FanoListXML.childNodes[loop];
       var fanoId = SNS.getElementsByTagName("FanoId")[0];
       var fanos = SNS.getElementsByTagName("Fano")[0];

        eval(window.document.loginform.case_id_in_temp).options[loop+1] = new Option(fanos.childNodes[0].nodeValue,fanoId.childNodes[0].nodeValue);
 

      
       }
    window.document.loginform.case_id_in_temp.value=window.document.loginform.case_id_in_temp.options[1].value ;
    updateHTML4(CrListXML);
}

//for complainant_respondent name filter
function getDoclist()
    {

    var searchFano=document.loginform.case_id_in_temp.value;
            //searchFano = searchFano.toUpperCase();

         var cr = "C";
           var url = "getDoc?fano="+searchFano+"&cr="+cr;


     if (window.ActiveXObject)
    {
   // alert("jegan");
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {
    //alert("jegan1");
        httpRequest = new XMLHttpRequest();
    }
    
    httpRequest.open("GET", url, true);
    httpRequest.onreadystatechange = function() {processRequest1(); } ;
    httpRequest.send(null); 

    }
 function processRequest1()
{
    if (httpRequest.readyState == 4)
    {
        if(httpRequest.status == 200)
       
        {

            var CrListXML = httpRequest.responseXML.getElementsByTagName("CrList")[0];

            updateHTML1(CrListXML);
        }
        else
        {
            alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
        }
    }
}
   
function updateHTML4(CrListXML)
{


   var len = window.document.DealAssistForm.complntNameTemp.options.length;

    while(len>0) {
        len = len -1;
        window.document.DealAssistForm.complntNameTemp.options[len] = null;
}//end while       
// alert(StateNamesXML.childNodes.length);

  window.document.loginform.complntNameTemp.options[0] = new Option("Select","0");
   document.loginform.complntName.value="";
   document.loginform.complntEmail.value="";
   document.loginform.complntFaxNo.value="";
  

return;
}



function updateHTML1(CrListXML)
{

   var len = window.document.loginform.complntNameTemp.options.length;

    while(len>0) {
        len = len -1;
        window.document.loginform.complntNameTemp.options[len] = null;
    }//end while       
// alert(StateNamesXML.childNodes.length);

window.document.loginform.complntNameTemp.options[0] = new Option("Select","0");

      for (loop = 0; loop < CrListXML.childNodes.length; loop++) {

       var SNS = CrListXML.childNodes[loop];
       var crId = SNS.getElementsByTagName("CrId")[0];
       var crn= SNS.getElementsByTagName("CrName")[0];

    eval(window.document.loginform.complntNameTemp).options[loop+1] = new Option(crn.childNodes[0].nodeValue,crId.childNodes[0].nodeValue);
    //window.document.DealAssistForm.fano.options[loop+1] = new Option(fanos,fanoId);

       }
   document.loginform.complntName.value="";
   document.loginform.complntEmail.value="";
   document.loginform.complntFaxNo.value="";
   document.loginform.complntAddress.value="";
  
   
}


//For Getting details of Complainant and Respondent
function getDoclistdetail()
    { //alert("kumud");
    //document.loginform.case_id_in.value=document.loginform.case_id_in_temp.value;
    var fano = document.loginform.case_id_in_temp.value;
    var crSeq=document.loginform.complntNameTemp.value;
            //searchFano = searchFano.toUpperCase();
//alert(crSeq);
if (crSeq=="0") {
alert("Please Select Doctor Name");
return;
}
         var cr = "C";
           var url = "getDocDetail?fano="+fano+"&crSeq="+crSeq+"&cr="+cr;


     if (window.ActiveXObject)
    {
   // alert("jegan");
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {
    //alert("jegan1");
        httpRequest = new XMLHttpRequest();
    }
    
    httpRequest.open("GET", url, true);
    httpRequest.onreadystatechange = function() {processRequest2(); } ;
    httpRequest.send(null); 

    }
 function processRequest2()
{
    if (httpRequest.readyState == 4)
    {
        if(httpRequest.status == 200)
       
        {
//alert(httpRequest.responseText)
            var CrListXML = httpRequest.responseXML.getElementsByTagName("CrList")[0];

            updateHTML2(CrListXML);
        }
        else
        {
            alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
        }
    }
}
   

function updateHTML2(CrListXML)
{


       for (loop = 0; loop < CrListXML.childNodes.length; loop++) {

       var SNS = CrListXML.childNodes[loop];

       var crName = SNS.getElementsByTagName("CrName")[0];
               crName = crName.childNodes[0].nodeValue;

     
       var crMail = SNS.getElementsByTagName("CrMail")[0];
            crMail=crMail.childNodes[0].nodeValue;          
       var crFax = SNS.getElementsByTagName("CrFax")[0];
               crFax = crFax.childNodes[0].nodeValue;
           
      
                if (crName == "N/A") {
                        window.document.loginform.complntName.value = "";
                        } else {
                        window.document.loginform.complntName.value = crName;
                        }
       
                if (crMail=="N/A") {
                        window.document.loginform.complntEmail.value = "";
                        } else {
                        window.document.loginform.complntEmail.value = crMail;
                        }
                if (crFax=="N/A") {
                    window.document.loginform.complntFaxNo.value = "";
                } else {
                    window.document.loginform.complntFaxNo.value = crFax;
                }
               
               

       }
   
   
}       

</script>

</head>
<body>


<form name="loginform" method="post">

<table align="center">

<tr>
<td align="center">Enter Departement: <input type="text" name="attachToCaseNo"/><a href="javascript:getDepartment()"><b>Search</b></a> <br/>
                   Select Department:<select name="case_id_in_temp" onchange="getDoclist()">
                                    <option value="">select</option>
                                    </select> <br/>
                 Select Doctor: <select name="complntNameTemp" onchange="getDoclistdetail()">
                                 <option value="">select</option>
                                </select>
</td>                             

</tr>

<tr>
<td align="center">
Doctor Name :<input type="text" name="complntName" value=""/><br/>
Doctor Dep :<input type="text" name="complntFaxNo" value=""/><br/>
Doctor Email :<input type="text" name="complntEmail" value=""/><br/>

</td>


</tr>

</form>



</body>
</html>


Now we create a servlet for every call mention in Jsp page through using AJAX
getDep.java

package com.doc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class getDep
 */
public class getDep extends HttpServlet {
    private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getDep() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(
            HttpServletRequest requestObj,
            HttpServletResponse responseObj)
            throws IOException {
            //set the content type
            responseObj.setContentType("text/xml");

            responseObj.setHeader("Cache-Control", "no-cache");

            //get the PrintWriter object to write the html page
            PrintWriter writer = responseObj.getWriter();

            //get parameters store into the hashmap
            HashMap paramsMap = new HashMap();
            Enumeration paramEnum = requestObj.getParameterNames();
            while (paramEnum.hasMoreElements()) {
                String paramName = (String) (paramEnum.nextElement());
                paramsMap.put(paramName, requestObj.getParameter(paramName));
            }

            String fano = (String) paramsMap.get("fano");
            // for Daily Order, Court Hearing Modification, ,Cause Title, deal Assist , Notices search key
            String searchKey = (String) paramsMap.get("searchKey");
           
            String cond = "";

            ArrayList fanoDtoh = new ArrayList();
            ArrayList caseNos = new ArrayList();
            ArrayList dtoh = new ArrayList();
            ArrayList comboList = new ArrayList();
            Connection con = null;
            //ArrayList districtList=new ArrayList();
            try {
                DAO crDao = new DAO();

                caseNos = crDao.loadCaseListAjax(fano, searchKey);
//                if (caseNos.size()==0) {
//                    requestObj.setAttribute("CasesCombo",comboList);
//                }
            } catch (Exception e) {
                System.out.println(e);
            }

            Iterator it = caseNos.iterator();

            writer.println("<FanoList>");
            while (it.hasNext()) {

                org.apache.struts.util.LabelValueBean l = (org.apache.struts.util.LabelValueBean) it.next();
                String temp = l.getValue();
                writer.println("<FanoD>");

                writer.println("<FanoId>" + l.getValue() + "</FanoId>");
                //writer.println("<Fano>" + l.getLabel() + "</Fano>");
                writer.println("<Fano><![CDATA["+ l.getLabel()+"]]></Fano>");
                writer.println("</FanoD>");

            }
            //    writer.println("<Profile><![CDATA[" + "Jegan1" + "]]></Profile>");
            //close the write
            writer.println("</FanoList>");
            writer.close();
        }

}

getDocDetail.java
package com.doc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class getDocDetail
 */
public class getDocDetail extends HttpServlet {
    private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getDocDetail() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(
            HttpServletRequest requestObj,
            HttpServletResponse responseObj)
            throws IOException {
            //set the content type
            responseObj.setContentType("text/xml");

            responseObj.setHeader("Cache-Control", "no-cache");

            //get the PrintWriter object to write the html page
            PrintWriter writer = responseObj.getWriter();

            //get parameters store into the hashmap
            HashMap paramsMap = new HashMap();
            Enumeration paramEnum = requestObj.getParameterNames();
            while (paramEnum.hasMoreElements()) {
                String paramName = (String) (paramEnum.nextElement());
                paramsMap.put(paramName, requestObj.getParameter(paramName));
            }
            String fano = (String) paramsMap.get("fano");
            String crSeq = (String) paramsMap.get("crSeq");
            String cr = (String) paramsMap.get("cr");
            String cond = "";

            ArrayList fanoDtoh = new ArrayList();
            ArrayList caseNos = new ArrayList();
            ArrayList dtoh = new ArrayList();
           
            try {
            DAO daDao = new DAO();

            DocForm da=daDao.loadCaseListAjax2(fano, crSeq);
                   
                   
                String crname = da.getDocName();
                if (crname==null) {
                    crname="";
                }
               
               
               
                String crmail = da.getDocMail();
                if (crmail==null) {
                    crmail="";
                }
               
                String crfax = da.getDocFax();
                if (crfax==null) {
                    crfax="";
                }
               
  
                if (crname.equals("")) {
                    crname = "N/A";
                }
               
               
                if (crmail.equals("") ) {
                    crmail = "N/A";

                }
                if (crfax.equals("")) {
                    crfax = "N/A";
                }
               
               
                writer.println("<CrList>");
               
                writer.println("<CrDesc>");
               
                writer.println("<CrName><![CDATA[" + crname + "]]></CrName>");
               
               
                writer.println("<CrMail><![CDATA[" + crmail + "]]></CrMail>");
               
                writer.println("<CrFax><![CDATA[" + crfax + "]]></CrFax>");
               
               
                writer.println("</CrDesc>");
                writer.println("</CrList>");
                writer.close();
               
            } catch (Exception e) {
                System.out.println(e);
            }
                finally
                {
                   
                   
                }
             
        }
}


GetDoc.java
package com.doc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.util.LabelValueBean;

public class getDoc extends HttpServlet {

    protected void doGet(HttpServletRequest requestObj, HttpServletResponse responseObj)
       throws IOException
{
//set the content type
responseObj.setContentType("text/xml");
responseObj.setHeader("Cache-Control", "no-cache");
PrintWriter writer = responseObj.getWriter();
HashMap paramsMap = new HashMap();
Enumeration paramEnum = requestObj.getParameterNames();
while(paramEnum.hasMoreElements())
{
String paramName = (String)(paramEnum.nextElement());
paramsMap.put(paramName, requestObj.getParameter(paramName));
}

String fano= (String)paramsMap.get("fano");
String cr = (String)paramsMap.get("cr");
ArrayList dtoh = new ArrayList();


//ArrayList districtList=new ArrayList();
try
{
DAO   daDao = new DAO();               
dtoh = daDao.loadCaseListAjax1(fano,cr);
}
catch(Exception e)
{
System.out.println(e);
}

Iterator it =dtoh.iterator();

writer.println("<CrList>");
while(it.hasNext())
{
   
LabelValueBean l=(LabelValueBean)it.next();
String temp=l.getValue();
writer.println("<CrDesc>");

writer.println("<CrId>"+ l.getValue()+"</CrId>");
writer.println("<CrName><![CDATA["+ l.getLabel()+"]]></CrName>");
writer.println("</CrDesc>");


}
writer.println("</CrList>");
writer.close();                    
}        

}



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.