日期:2014-05-17 浏览次数:20750 次
<script> $(document).ready( function() { $("#cname").change( function(){ var cnVal=$("#cname").val(); $.ajax({ type:"post", url:"servlet/Test", data : {'cName': $("#cname").val()}, dataType:"json", success:function(data){ alert(data); }}); }); }); </script>
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("GBK"); String cname=request.getParameter("cName"); [color=#FF0000]System.out.println("测试="+cname);[/color]//这一句控制台不打出东西 response.setCharacterEncoding("GBK"); ConnSqlServer co=new ConnSqlServer(); Connection ct=co.connDb(); List list=new ArrayList(); ResultSet rs=null; try { rs=ct.prepareStatement("select classID from classes where className="+cname+"").executeQuery(); while(rs.next()){ String nameCourse=rs.getString("className"); list.add(nameCourse); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONArray json=JSONArray.fromObject(list); PrintWriter pw=response.getWriter(); pw.println(json); pw.flush(); pw.close(); }