Dynamic City, State Loading based on Country Selection 
 Without Reloading the Page 

In previous chapter i explained Database connection with Ajax. 
Now we will discuss how to load City and State on particular Country selection using Combo Box or Drop Down Box
Use Servlets or Struts for Connecting Database the response from action should be in xml format.
Check Below link to create Servlet class
http://ajaxtutorialonline.blogspot.com/2010/09/steps-to-connect-ajax-with-database-to.html.


Configure Web.xml 
-------------------------
<servlet>
<servlet-name>DynamicLoading</servlet-name>
<servlet-class>com.CountryLoad</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DynamicLoading</servlet-name>
<url-pattern>/Country</url-pattern>
</servlet-mapping>
 public class DynamicLoading extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
  {
     String val = request.getParameter("id");
     String stor=null;
     Connection con = null;
     PreparedStatement pst =null;
     ResultSet rs=null;
     response.setContentType("text/xml");
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     pst = con.prepareStatement("select State from Country where Country=?");
     pst.setString(1,val);
     response.getWriter().write("<Country>");
     while(rs.next())
     {
      stor =rs.getString(1);
      response.getWriter().write("<state>"+stor+"</state>");
     }
response.getWriter().write("</Country>");
  con.close();
  }catch(SQLException e)
   {
    
   }
}
}
0 comments