jsp中的动态下拉列表。哪个来自mysql

时间:2014-03-30 08:45:37

标签: java mysql jsp

我在JSP中有动态下拉列表。国家,州,市。我有代码。但问题是,下拉值来自MySQL并且不显示值。 三个jsp文件country.jsp,state.jsp,city.jsp 推荐链接http://www.roseindia.net/answers/viewqa/Ajax/15250-DropDown-in-ajax+jsp.html 提前谢谢。

<%@ 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">
<%@page import="java.sql.*"%>
<html>
<head>  
<script language="javascript" type="text/javascript">  
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;
  } 
  var url="state.jsp";
  url +="?count=" +str;
  xmlHttp.onreadystatechange = stateChange;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  }

  function stateChange(){   
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
  document.getElementById("state").innerHTML=xmlHttp.responseText   
  }   
  }

  function showCity(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;
  } 
  var url="city.jsp";
  url +="?count=" +str;
  xmlHttp.onreadystatechange = stateChange1;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  }
  function stateChange1(){   
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
  document.getElementById("city").innerHTML=xmlHttp.responseText   
  }   
  }
  </script>  
  </head>  
  <body>  
  <select name='country' onchange="showState(this.value)">   
        <%
        try{
     Class.forName("com.mysql.jdbc.Driver").newInstance();  
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo1","root","root"); 
     Statement stmt = con.createStatement();  
     ResultSet rs = stmt.executeQuery("Select * from country");


     while(rs.next()){
         %>
         <%out.print("rs.getString('countryname')"); %>
          <option value="<%=rs.getString(1)%>"><%=rs.getString("countryname")%></option>  
          <%
     }
     %><% 
     }
    catch(ClassNotFoundException e){

    }%>
      </select>  
      <br>  
      <div id='state'>  
      <select name='state' >  
      <option value='-1'></option>  
      </select>  
      </div>  

      <div id='city'>  
      <select name='city' >  
      <option value='-1'></option>  
      </select>  
      </div>
      </body> 
      </html>

0 个答案:

没有答案