FileDocCategorySizeDatePackage
JWPSelectServlet.javaAPI DocExample2887Sat Jun 03 07:06:40 BST 2006com.oreilly.ajax.servlet

JWPSelectServlet

public class JWPSelectServlet extends HttpServlet

Fields Summary
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    doPost(req, res);
  
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    String state = (String) req.getParameter("state");
    if (state != null)
    {
      res.setContentType("text/xml");
      res.setHeader("Cache-Control", "no-cache");
      res.getWriter().write(getCities(state));
    }
  
private java.lang.StringgetCities(java.lang.String state)

    Connection con = DatabaseConnector.getConnection();
    StringBuffer sb = new StringBuffer("<select name=\"citySelected\">");
    try
    {
      Statement statement = con.createStatement();
      String sqlString = "SELECT DISTINCT CITY FROM ZIPCODES WHERE STATE='"
          + state + "' ORDER BY CITY;";
      ResultSet resultSet = statement.executeQuery(sqlString);
      while (resultSet.next())
      {
        sb.append("<option>" + resultSet.getString(1) + "</option>\n");
      }
    }
    catch (Exception e)
    {
      System.out.println("exception caught getting cities for " + state);

    }
    finally
    {
      sb
          .append("</select> <ajax:event ajaxRef=\"CitySelectForm/citySelectionChange\"/>");
      if (con != null)
      {
        try
        {
          con.close();
        }
        catch (SQLException e)
        {
        }
      }
    }
    return sb.toString();
  
private java.lang.StringgetStates(java.lang.String city)

    Connection con = DatabaseConnector.getConnection();
    StringBuffer sb = new StringBuffer("<select>");
    try
    {
      Statement statement = con.createStatement();
      String sqlString = "SELECT DISTINCT STATE FROM ZIPCODES where CITY='"
          + city + "';";
      ResultSet resultSet = statement.executeQuery(sqlString);
      while (resultSet.next())
      {
        sb.append("<option>" + resultSet.getString(1) + "</option>\n");
      }
    }
    catch (Exception e)
    {
      System.out.println("exception caught getting states from zipcodes table");

    }
    finally
    {
      sb.append("</select>");
      if (con != null)
      {
        try
        {
          con.close();
        }
        catch (SQLException e)
        {
        }
      }
    }
    return sb.toString();