分类: Java
2009-11-16 14:44:31
--- servlet----
package com.ajax;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxServlet extends HttpServlet {
String[][] hebeiArr;
String[][] beijingArr;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("ajaxServlet onload .....");
hebeiArr = new String[3][2];
beijingArr = new String[3][2];
String[][] tmpArr = null;
hebeiArr[0][0] = "11";
hebeiArr[0][1] = "石家庄";
hebeiArr[1][0] = "12";
hebeiArr[1][1] = "廊坊";
hebeiArr[2][0] = "13";
hebeiArr[2][1] = "秦皇岛";
beijingArr[0][0] = "01";
beijingArr[0][1] = "昌平";
beijingArr[1][0] = "02";
beijingArr[1][1] = "通州";
beijingArr[2][0] = "03";
beijingArr[2][1] = "大兴";
String id = req.getParameter("id");
if(id.equals("hb")){
tmpArr = hebeiArr;
} else if(id.equals("bj")){
tmpArr = beijingArr;
}
StringBuffer sb = new StringBuffer();
sb.append("
System.out.println(tmpArr.length);
for(int i=0; i
}
sb.append("
resp.setContentType("text/xml");
resp.setHeader("Cache-Control", "no-cache");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.print(sb.toString());
System.out.println(sb.toString());
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
-----------------------
----- jsp-------
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
%>
---------web.xml---------
xmlns:xsi=""
xsi:schemaLocation="
">
----------------------