AJAX DATA FETCH IN JSP :
JAVA SCRIPT FUNCTION TO CALL AJAX:
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var keys = document.assignNewTask.team.value
var urls = "Emp_Fetch.jsp?ok=" + keys
if
(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function ()
{
removeall();
if (xmlhttp.readyState == 4)
{
z = 0;
var roott = xmlhttp.responseXML.documentElement;
var ress =
roott.getElementsByTagName("membername")[z].childNodes[0].nodeValue;
while (ress != null)
{
addoptions(ress)
z++
var ress =
roott.getElementsByTagName("membername")[z].childNodes[0].nodeValue;
}
}
function removeall()
{
var ct = document.assignNewTask.name.length;
for (i = ct; i >= 0; i--)
{
document.assignNewTask.name.options[i]
= null;
}
}
function addoptions(reslt)
{
// alert(reslt);
var ct1 =
document.createElement("OPTION");
ct1.text = reslt;
ct1.value = reslt;
document.assignNewTask.name.options.add(ct1);
}
}
xmlhttp.open("GET", urls, true);
xmlhttp.send();
}
</script>
CALLING AJAX FUNCTION IN SELECT TAG:
SELECT TEAM :
<select
id="team" name="team"
onchange="loadXMLDoc()">
<option value="">---Select Team---</option>
<%
String name =
"";
String gid="0";
int size=0;
Connection
con1 = Database_Connection1.Database_Connection2();
Statement
stmg=con1.createStatement();
ResultSet
rslen=stmg.executeQuery("select length(groupid) as size from teamleads
where Tid='"+Tid+"'");
while(rslen.next())
{
size=rslen.getInt("size");
}
con1.close();
for(int
i=0;i<size;i++)
{
ResultSet rs =
stm.executeQuery(" select gnanme,gid from groups where gid=(select
SPLIT_STR(groupid,',',"+i+") from teamleads where
Tid='"+Tid+"')");
while (rs.next())
{
name =
rs.getString("gnanme");
gid=rs.getString("gid");
%>
<option
value="<%=gid%>"><%=name%></option>
<%}
}%>
</select>
Employee name :
<select
name="name"> //AJAX
DROPDOWN
<option
value="">--Choose Employee Name--</option>
</select>
JSPSERVLET:-
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page
import="com.pack.Database_Connection1"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page
import="javax.naming.InitialContext"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.Context"%>
<%@ page contentType="text/html"
pageEncoding="UTF-8"%>
<%
response.setContentType("text/xml");
try
{
String gid =
request.getParameter("ok");
List<String>
memnames=new ArrayList<String>();
String
name = "";
int size =
0;
Connection
con1 = Database_Connection1.Database_Connection2();
Statement
stmg = con1.createStatement();
ResultSet
rslen = stmg.executeQuery("select length(membersid) as size from groups
where gid='" + gid + "'");
while
(rslen.next()) {
size =
rslen.getInt("size");
System.out.println("GIVEN SIZE.."+size);
}
for (int i =
0; i <=size; i++)
{
ResultSet rs = stmg.executeQuery(" select membername from members
where memid=(select SPLIT_STR(membersid,','," +i+ ") from groups
where gid='" + gid + "')");
while (rs.next()) {
memnames.add(rs.getString(1));
}
}
System.out.println("SIZE .."+memnames.size());
if(memnames.size()>1)
{
System.out.println("GREATER
VALE..");
out.println("<members>");
for(int
i=0;i<memnames.size();i++)
{
out.println("<membername>" + memnames.get(i) +
"</membername>");
}
out.println("</members>");
}
else
{
System.out.println("GREATER VALE other..");
out.println("<members>");
out.println("<membername>" + memnames.get(0) +
"</membername>");
out.println("</members>");
}
stmg.close();
con1.close();
}
catch(Exception ex)
{
System.out.println("ERROR.."+ex.getMessage());
}
%>
No comments:
Post a Comment