//请求大学数据有ajax var http_request = false; function setProUnis(o){ var url="setShowCoUnis?cid="; url+=o.coId; url+="&proId="; url+=o.id; CreateRequest(url); } function CreateRequest(url){ http_request=false; //开始初始化XMLHttpRequest if(window.XMLHttpRequest){ //Mozilla浏览器 http_request = new XMLHttpRequest(); if(http_request.overrideMimeType){ http_request.overrideMimeType("text/xml"); } }else if(window.ActiveXObject){//IE try{ http_request = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { // TODO: handle exception } } } if(!http_request){//异——创建对象失败 window.alert("不能创建XMLHttpRequest对象实例,请更新浏览器。") return false; } http_request.onreadystatechange = processRequest; //确定发送请求 //window.alert(url); http_request.open("GET", url, true); http_request.send(null); } function processRequest(){ if(http_request.readyState == 4){ if(http_request.status == 200){ //window.alert("statusText = "+http_request.responseText); document.getElementById("uniTbl").innerHTML=http_request.responseText; }else{ window.alert("你请求的界面有异常"); } } }
上面是javascript的部分
下面的是servlet的部分
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); response.setContentType("text/xml;charset=utf-8"); //ie禁用缓存 response.setHeader("Cache-Control", "no-cache"); String cId=request.getParameter("cid"); String proId=request.getParameter("proId"); System.out.println(cId +" "+proId); //取出UniversityServiceInter WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); UniversityServiceInter usi=(UniversityServiceInter) wac.getBean("universityServiceImp"); List list=usi.getObjectList("from University where proId=? and countryId=?", new Object[]{Integer.parseInt(proId),Integer.parseInt(cId)}); String res = "<table width='100%'><tr>"; for (int i = 0; i < list.size(); i++) { University u = (University) list.get(i); res+="<td><li>"; res+="<a onclick='showMyUni(this)' href='javascript:void(0);' class='xh' id='"+u.getId()+"'> "+u.getName()+"</a></li></td>"; if((i+1)%3==0){ res+="</tr><tr>"; } } res+="</tr></table>"; System.out.println(res); out.write(res); out.flush(); out.close(); }