aspose file tools*
The moose likes Struts and the fly likes how the response from ajax in jsp page get refresh when two dropdown fetch from database dependent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how the response from ajax in jsp page get refresh when two dropdown fetch from database dependent" Watch "how the response from ajax in jsp page get refresh when two dropdown fetch from database dependent" New topic
Author

how the response from ajax in jsp page get refresh when two dropdown fetch from database dependent

sudarshan jadhav
Ranch Hand

Joined: May 28, 2012
Posts: 42
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);
}
}

sudarshan jadhav
Ranch Hand

Joined: May 28, 2012
Posts: 42
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);
}
}

Hello sir please help me it's urgent My logic is work in chrome browser but not working in IE because in my script the 1st time the script is work but next time in myscript showstate function call is pass but the url to city is not pass but ajax response from previous running value from database is shown but in chrome it is work but in IE it is not work Please help me
Thank you in advance
sudarshan jadhav
Ranch Hand

Joined: May 28, 2012
Posts: 42

My problem is solve in i have use one jsp and one servlet in servlet file i am use response.setHeader("Cache-Control", "no-cache"); that code is clean a cahe in jsp page response.
In my jsp page add head tage with
<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform, pre-check=0, post-check=0, private");
response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0);
%>
this is clear the cache from jsp page.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how the response from ajax in jsp page get refresh when two dropdown fetch from database dependent