用ajax 做个二级联动的菜单,改变第一个下拉框中的值,第二个下拉框跟着改变,前三次请求能正确完成,三次以上则不行,在网上搜的答案都试过了,附jsp和servlet在下面。
package com.pynon.crawler; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.pynon.base.util.MysqlConnections; public class subtype extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); //String supertype = new String(request.getParameter("supertype").getBytes("ISO-8859-1"),"gbk"); String supertype=request.getParameter("supertype"); // System.out.println(supertype); List list = new ArrayList(); Connection conn = MysqlConnections.getConnection(); if (conn != null) { Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt .executeQuery("select id,classtype from dt_talknet where typename='" + supertype + "';"); while (rs.next()) { Map map = new HashMap(); map.put("id", rs.getInt(1)); map.put("classtype", rs.getString(2)); list.add(map); // System.out.println(rs.getInt(1) + " " + // rs.getString(2)); } } catch (SQLException e) { MysqlConnections.freeConnection(conn); e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e1) { e1.getMessage(); } } } if (list.size() > 0) { out.print("<response>"); for (int i = 0; i < list.size(); i++) { Map map = (Map) list.get(i); out.print("<subtypeids>" + map.get("id") + "</subtypeids>"); out.print("<subtypenames>" + map.get("classtype") + "</subtypenames>"); } out.print("</response>"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<body> <fieldset><script type="text/javascript"> var XMLHttpReq; //创建XMLHttpRequest对象 function createXMLHttpRequest() { if(window.XMLHttpRequest) { //Mozilla 浏览器 XMLHttpReq = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE浏览器 try { XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } } function go4() { createXMLHttpRequest(); var supertype = document.getElementById("supertype").value;//获取选中的大类别. var url = "/pynon/subtype?supertype="+supertype+"¶m="+ new Date().getTime(); XMLHttpReq.onreadystatechange = processResponse;//指定响应函数 XMLHttpReq.open("POST",url,true); XMLHttpReq.setRequestHeader("If-Modified-Since","0"); XMLHttpReq.setRequestHeader("Cache-Control","no-cache"); XMLHttpReq.send(null); } function processResponse() { if (XMLHttpReq.readyState == 4) { // 判断对象状态 if (XMLHttpReq.status == 200 || XMLHttpReq.status==0) { // 信息已经成功返回,开始处理信息 updateList(); } else { //页面不正常 window.alert("您所请求的页面有异常。"); } } } function updateList() { clearList(); var subtype = document.getElementById("subtype");//小类别的控件对象 var subtypeids = XMLHttpReq.responseXML.getElementsByTagName("subtypeids"); var subtypenames = XMLHttpReq.responseXML.getElementsByTagName("subtypenames"); var option=null; for(var i=0;i<subtypeids.length;i++) { option = document.createElement("option"); option.value = subtypeids[i].firstChild.nodeValue; option.appendChild(document.createTextNode(subtypenames[i].firstChild.nodeValue)); subtype.appendChild(option); } } function clearList() { var subtype = document.getElementById("subtype"); while(subtype.childNodes.length > 0) { subtype.removeChild(subtype.childNodes[0]); } } </script> <div>分析模板:<select id="supertype" onChange="go4()" style="width: 100px"> <option>请选择类别</option> <% Connection cont = MysqlConnections.getConnection(); if (cont != null) { Statement stmt = null; ResultSet rs = null; try { stmt = cont.createStatement(); rs = stmt .executeQuery("select distinct typename from dt_talknet"); while (rs.next()) { %> <option value="<%=rs.getString("typename")%>"><%=rs.getString("typename")%></option> <% } %> </select> <% if (rs != null) { rs.close(); rs = null; } stmt.close(); stmt = null; MysqlConnections.freeConnection(cont); } catch (SQLException e) { MysqlConnections.freeConnection(cont); e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e1) { e1.getMessage(); } } } %> <select style="width: 200px" id="subtype" name="subtype"> <option value="0">==请选择==</option> </select> </div> <br> <br> <br> </fieldset> </body>
问题补充:
lovewhzlq 那个我现在加了,这个问题到底是为什么呀,帮我看看呀!谢谢啦
问题补充:
三次都正常执行了,按第四次的时候二级类别的就不动了,还是显示的第三次出来的子类别,如果把浏览器关了,重新来,又是只能执行三次