RefreshModelListServlet.java
上传用户:shen332233
上传日期:2021-09-03
资源大小:7478k
文件大小:5k
源码类别:

Ajax

开发平台:

Java

  1. package ajaxbook.chap4;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8. public class RefreshModelListServlet extends HttpServlet {
  9.     private static List availableModels = new ArrayList();
  10.     
  11.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  12.     throws ServletException, IOException {
  13.         response.setContentType("text/html;charset=UTF-8");
  14.         
  15.         int modelYear = Integer.parseInt(request.getParameter("modelYear"));
  16.         String make = request.getParameter("make");
  17.         
  18.         StringBuffer results = new StringBuffer("<models>");
  19.         MakeModelYear availableModel = null;
  20.         for(Iterator it = availableModels.iterator(); it.hasNext();) {
  21.             availableModel = (MakeModelYear)it.next();
  22.             if(availableModel.modelYear == modelYear) {
  23.                 if(availableModel.make.equals(make)) {
  24.                     results.append("<model>");
  25.                     results.append(availableModel.model);
  26.                     results.append("</model>");
  27.                 }
  28.             }
  29.         }
  30.         results.append("</models>");
  31.         
  32.         response.setContentType("text/xml");
  33.         response.getWriter().write(results.toString());
  34.     }
  35.     
  36.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  37.     throws ServletException, IOException {
  38.         processRequest(request, response);
  39.     }
  40.     
  41.     public void init() throws ServletException {
  42.         availableModels.add(new MakeModelYear(2006, "Dodge", "Charger"));
  43.         availableModels.add(new MakeModelYear(2006, "Dodge", "Magnum"));
  44.         availableModels.add(new MakeModelYear(2006, "Dodge", "Ram"));
  45.         availableModels.add(new MakeModelYear(2006, "Dodge", "Viper"));
  46.         availableModels.add(new MakeModelYear(1995, "Dodge", "Avenger"));
  47.         availableModels.add(new MakeModelYear(1995, "Dodge", "Intrepid"));
  48.         availableModels.add(new MakeModelYear(1995, "Dodge", "Neon"));
  49.         availableModels.add(new MakeModelYear(1995, "Dodge", "Spirit"));
  50.         availableModels.add(new MakeModelYear(1985, "Dodge", "Aries"));
  51.         availableModels.add(new MakeModelYear(1985, "Dodge", "Daytona"));
  52.         availableModels.add(new MakeModelYear(1985, "Dodge", "Diplomat"));
  53.         availableModels.add(new MakeModelYear(1985, "Dodge", "Omni"));
  54.         availableModels.add(new MakeModelYear(1970, "Dodge", "Challenger"));
  55.         availableModels.add(new MakeModelYear(1970, "Dodge", "Charger"));
  56.         availableModels.add(new MakeModelYear(1970, "Dodge", "Coronet"));
  57.         availableModels.add(new MakeModelYear(1970, "Dodge", "Dart"));
  58.         availableModels.add(new MakeModelYear(2006, "Chevrolet", "Colorado"));
  59.         availableModels.add(new MakeModelYear(2006, "Chevrolet", "Corvette"));
  60.         availableModels.add(new MakeModelYear(2006, "Chevrolet", "Equinox"));
  61.         availableModels.add(new MakeModelYear(2006, "Chevrolet", "Monte Carlo"));
  62.         availableModels.add(new MakeModelYear(1995, "Chevrolet", "Beretta"));
  63.         availableModels.add(new MakeModelYear(1995, "Chevrolet", "Camaro"));
  64.         availableModels.add(new MakeModelYear(1995, "Chevrolet", "Cavalier"));
  65.         availableModels.add(new MakeModelYear(1995, "Chevrolet", "Lumina"));
  66.         availableModels.add(new MakeModelYear(1985, "Chevrolet", "Cavalier"));
  67.         availableModels.add(new MakeModelYear(1985, "Chevrolet", "Chevette"));
  68.         availableModels.add(new MakeModelYear(1985, "Chevrolet", "Celebrity"));
  69.         availableModels.add(new MakeModelYear(1985, "Chevrolet", "Citation II"));
  70.         availableModels.add(new MakeModelYear(1970, "Chevrolet", "Bel Air"));
  71.         availableModels.add(new MakeModelYear(1970, "Chevrolet", "Caprice"));
  72.         availableModels.add(new MakeModelYear(1970, "Chevrolet", "Chevelle"));
  73.         availableModels.add(new MakeModelYear(1970, "Chevrolet", "Monte Carlo"));
  74.         availableModels.add(new MakeModelYear(2006, "Pontiac", "G6"));
  75.         availableModels.add(new MakeModelYear(2006, "Pontiac", "Grand Prix"));
  76.         availableModels.add(new MakeModelYear(2006, "Pontiac", "Solstice"));
  77.         availableModels.add(new MakeModelYear(2006, "Pontiac", "Vibe"));
  78.         availableModels.add(new MakeModelYear(1995, "Pontiac", "Bonneville"));
  79.         availableModels.add(new MakeModelYear(1995, "Pontiac", "Grand Am"));
  80.         availableModels.add(new MakeModelYear(1995, "Pontiac", "Grand Prix"));
  81.         availableModels.add(new MakeModelYear(1995, "Pontiac", "Firebird"));
  82.         availableModels.add(new MakeModelYear(1985, "Pontiac", "6000"));
  83.         availableModels.add(new MakeModelYear(1985, "Pontiac", "Fiero"));
  84.         availableModels.add(new MakeModelYear(1985, "Pontiac", "Grand Prix"));
  85.         availableModels.add(new MakeModelYear(1985, "Pontiac", "Parisienne"));
  86.         availableModels.add(new MakeModelYear(1970, "Pontiac", "Catalina"));
  87.         availableModels.add(new MakeModelYear(1970, "Pontiac", "GTO"));
  88.         availableModels.add(new MakeModelYear(1970, "Pontiac", "LeMans"));
  89.         availableModels.add(new MakeModelYear(1970, "Pontiac", "Tempest"));
  90.     }
  91.     private static class MakeModelYear {
  92.         private int modelYear;
  93.         private String make;
  94.         private String model;
  95.         
  96.         public MakeModelYear(int modelYear, String make, String model) {
  97.             this.modelYear = modelYear;
  98.             this.make = make;
  99.             this.model = model;
  100.         }
  101.     }
  102. }