首先,假设有三个数据表,一个是school,一个是college,一个是subject。
对应的choseSelect.html代码如下:
然后,对应的有三个Servlet类,一个是SchoolXML,一个是CollegeXML,一个是Subject。
SchoolXML.java文件如下:- package com.songhao.ajax;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.songhao.DB.SchoolDao;
- import com.songhao.bean.School;
- /**
- * Servlet implementation class SchoolXML
- */
- public class SchoolXML extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public SchoolXML() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- doPost(request,response);
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- response.setContentType("text/xml");
- response.setCharacterEncoding("utf-8");
- PrintWriter out=response.getWriter();
- out.println("<response>");
- //连接数据库,将所有的学校取出来
- SchoolDao schDao=new SchoolDao();
- List<School> schools=new ArrayList<School>();
- try{
- schDao.getConn();
- schools=schDao.getAllSchool();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- schDao.closeAll();
- }
- for(int i=0;i<schools.size();i++){
- out.println("<school>");
- out.println("<schoolname>"+schools.get(i).getName()+"</schoolname>");
- out.println("<schoolvalue>"+schools.get(i).getId()+"</schoolvalue>");
- out.println("</school>");
- }
- out.println("</response>");
- out.flush();
- out.close();
- }
- }
复制代码 其它两个文件与他基本类似,希望对你有帮助!!
|