FileDocCategorySizeDatePackage
AjaxAnywhereSupportServlet.javaAPI DocExample3407Sat Jun 03 07:06:40 BST 2006com.oreilly.ajax.servlet

AjaxAnywhereSupportServlet

public class AjaxAnywhereSupportServlet extends HttpServlet

Fields Summary
private static final long
serialVersionUID
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 = req.getParameter("state");
    String city = req.getParameter("city");
    String function = req.getParameter("function");
    
    if(AAUtils.isAjaxRequest(req))
    {
      HttpSession session = req.getSession();
      if(function.equals("city"))
      {
        AAUtils.addZonesToRefresh(req, "stateswcityList");
        session.setAttribute("stateList", getStates(city));
      }
      else if(function.equals("state"))
      {
        AAUtils.addZonesToRefresh(req, "citiesList");
        session.setAttribute("cityList", getCities(state));
      }
    }
    String url = "/index.jsp";
    ServletContext sc = getServletContext();
    RequestDispatcher rd = sc.getRequestDispatcher(url);
    rd.forward(req, res);
  
private java.util.CollectiongetCities(java.lang.String state)

    ArrayList cityList = new ArrayList();
    Connection con = DatabaseConnector.getConnection();
    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())
      {
        cityList.add(resultSet.getString(1));
      }
    }
    catch(Exception e)
    {
      System.out.println("exception caught getting cities for " + state);

    }
    finally
    {
      if(con != null)
      {
        try
        {
          con.close();
        }
        catch(SQLException e)
        {
        }
      }
    }
    return cityList;
  
private java.util.CollectiongetStates(java.lang.String city)

    ArrayList stateList = new ArrayList();
    Connection con = DatabaseConnector.getConnection();
    try
    {
      Statement statement = con.createStatement();
      String sqlString = "SELECT DISTINCT STATE FROM ZIPCODES where CITY='" + city + "' ORDER BY STATE;";
      ResultSet resultSet = statement.executeQuery(sqlString);
      while (resultSet.next())
      {
        stateList.add(resultSet.getString(1));
      }
    }
    catch(Exception e)
    {
      System.out.println("exception caught getting states from zipcodes table");

    }
    finally
    {
      if(con != null)
      {
        try
        {
          con.close();
        }
        catch(SQLException e)
        {
        }
      }
    }
    return stateList;