Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Tuesday, 30 July 2013

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

LinkWithin

Related Posts Plugin for WordPress, Blogger...