sudarshan jadhav wrote:
How the response from ajax get refresh in jsp page when selecting country dropdown related state dropdown updated value cann't see in dropdown the old value is display and when eclipse is restarted the logic is work for first time and second time again same thing happen

In my jsp code two dropdown depend first is country and second is state when country select related to state is display but when the state assign a flag in database is updated to true next time
visit same jsp page country dropdown select the state dropdown flag is assign to state which is display but actually real thing is that the flag set value of state is not display in state dropdown
The ajax response is not refresh the dropdown i am using one country.jsp and one servlet state.java file is follow :
<script>
var xmlHttp;
var xmlHttp;
function showState(str) {
if (typeof XMLHttpRequest != "undefined") {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp == null) {
alert("Browser does not support XMLHTTP Request");
return;
}
//alert(str);
var url = "state";
url += "?count=" + str;
xmlHttp.onreadystatechange = stateChange;
//alert("open method");
xmlHttp.open("GET", url, true);
//alert("after open method");
//alert(url+str);
xmlHttp.send();
//alert("send");
}
function stateChange() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
//alert("befor res");
document.getElementById("state").innerHTML = xmlHttp.responseText;
document.getElementById("city").innerHTML = "";
//alert("after res");
}
}
function showCity(str) {
var sid = document.getElementById("country").value;
if (typeof XMLHttpRequest != "undefined") {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp == null) {
alert("Browser does not support XMLHTTP Request");
return;
}
var url = "city";
url += "?count=" + str + "&sid=" + sid;
xmlHttp.onreadystatechange = stateChange1;
xmlHttp.open("Post", url, true);
xmlHttp.send(null);
}
function stateChange1() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
document.getElementById("city").innerHTML = xmlHttp.responseText;
}
}
</script>
country.jsp
<table >
<tr>
<td >Select Station</td>
<td ><select name='country' id="country" onchange="showState(this.value)" style="width: 222px;">
<option value="none">Select</option>
<%
HttpSession sess = request.getSession();
String cid = sess.getAttribute("cid").toString();
sess.setAttribute("cid", cid);
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/country", "root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from country");
while (rs.next()) {
%>
<option value="<%=rs.getString("id")%>"><%=rs.getString("cname")%></option>
<%
}
%>
</select></td>
</tr>
<br>
<tr>
<td align="left">Select State</td>
<td style="width: 215px;">
<div id='state'></div>
</td>
</tr>
</table>
state.java
protected void doGet(
HttpServletRequest request,
HttpServletResponse response) throws
ServletException,
IOException {
String sid = request.getParameter("count");
HttpSession sess = request.getSession();
String cid=sess.getAttribute("cid").toString();
sess.setAttribute("cid",cid);
response.setContentType("text/html");
System.out.println("In state cid"+cid);
System.out.println("In state java");
PrintWriter pw =null;
pw=response.getWriter();
pw.println("<select id='info' name='state' onchange='showCity(this.value);' style='width:222'><option value='-1'>Select</option>");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=null;
con= DriverManager.getConnection(
"jdbc:mysql://localhost:3306/country", "root", "root");
Statement stmt=null;
stmt= con.createStatement();
ResultSet rs=null;
rs= stmt.executeQuery("select * from state where id='"+id+"' and flag='Deactivate'");
while (rs.next()) {
pw.println("<option value='" + rs.getString("id")+ "'>"+rs.getString("sname")+"</option>");
System.out.println("In state mid:"+rs.getString("sid")+"In state Mname"+rs.getString("sname"));
}
pw.println("</select>");
pw.close();
pw=null;
} catch (Exception e) {
System.out.println(e);
}
}