jsp里用Ajax做的select的两级级联,带数据库。

jsp里用Ajax做的select的两级级联,带数据库。
jsp里的两个select:
< select name = " lei "  onchange = " callServer(this.options[this.selectedIndex].text) " >
<%
rs
= stmt.executeQuery( " select dfl from dhyfl " );
while (rs.next()){
%>
< option ><%= rs.getString( " dfl " %></ option >
<%
}
%>
</ select >< select name = " smallLei " ></ select >

jsp底部的Ajax:
< script language = " javascript " >
var xmlHttp
= false ;
try  {
   xmlHttp 
=   new  XMLHttpRequest();
catch  (trymicrosoft) {
     
try  {
       xmlHttp 
=   new  ActiveXObject( " Msxml2.XMLHTTP " );
     } 
catch  (othermicrosoft) {
       
try  {
         xmlHttp 
=   new  ActiveXObject( " Microsoft.XMLHTTP " );
       } 
catch  (failed) {
          
       }  
     }
   }
function callServer(smallLei){
    var url
= " <%=request.getContextPath()%>/smallLei?lei= "   +  smallLei;
    xmlHttp.open(
" get " ,url, " true " );
    xmlHttp.onreadystatechange 
= upsmlei;
    xmlHttp.send(
null );
}
function upsmlei(){
    
if (xmlHttp.readyState  ==   4 ){
        
if (xmlHttp.status == 200 ){
            var smlei 
=  document.all.smallLei;
            smlei.options.length
= 0 ;
            var arr
= xmlHttp.responseText.split( " | " );
            
for (var i = 0 ;i < arr.length - 1 ;i ++ ){
                smlei.add(
new  Option(arr[i],arr[i]));
            }
        }
    }
}
</ script >

Ajax的servlet:
package  cqrx.ajax;

import  java.io.IOException;
import  java.io.PrintWriter;
import  java.sql.ResultSet;
import  java.sql.Statement;

import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  cqrx.zgdsc.Conn;

public   class  SmallLei  extends  HttpServlet {

    
public  SmallLei() {
        
super ();
    }

    
public   void  destroy() {
        
super .destroy();
    }

    
public   void  doGet(HttpServletRequest request, HttpServletResponse response)
            
throws  ServletException, IOException {

                response.setContentType(
" text/html;charset=gb2312 " );
        PrintWriter out 
=  response.getWriter();
             String lei 
=  request.getParameter( " lei " );
             lei 
=   new  String(lei.getBytes( " ISO8859-1 " ),  " GB2312 " );
             out.print(lei);
             Conn c 
=   new  Conn();
             Statement stmt 
=   null ;
             ResultSet rs 
=   null ;
             
try  {
             stmt 
=  c.getConn().createStatement();
             String sql 
=   " select sfl from shyfl where dfl =' "   +  lei  +   " ' " ;
             rs 
=  stmt.executeQuery(sql);
             String str 
=   "" ;
             
while  (rs.next()) {
             str 
=  str  +  rs.getString( 1 +   " | " ;
             }
             out.print(str);
            
             } 
catch  (Exception e) {
             System.out.print(e.getMessage());
        } 
finally  {
            out.flush();
            out.close();
        }
    }

    
public   void  doPost(HttpServletRequest request, HttpServletResponse response)
            
throws  ServletException, IOException {
        doGet(request, response);
    }

    
public   void  init()  throws  ServletException {
    }

}

你可能感兴趣的:(jsp里用Ajax做的select的两级级联,带数据库。)