This Scenario say how to create xml file dynamically in the name of state we choose. Suppose we are choosing Maharastra in index page, then it will go to a servlet and Create one xml file on that name and send back to  javascript through Ajax. Java Script will parse it and will show the result.

These are the steps

  1. Create index.html
    <html>
    <head>
    <script type="text/javascript" src="includecity.js"></script>
    </head>
    
    <form name="myForm" method="POST" action="city">
    <table>
    	<tr>
    		<td>State:</td>
    		<td><select name="state" id="state" onchange="loadXMLDoc()">
    			<option value="-1">Select state</option>
    			<option value="Odisa" id="Odisa">Odisa</option>
    			<option value="Maharastra" id="Maharastra">Maharastra</option>
    			
    		</select></td>
    	</tr>
    	<tr>
    		<td>City: </td>
    		<td><select name="cities" id="cities" >
    			<option value="">Please select a city</option>
    		</select></td>
    	<tr>
    		<td>
    		<button type="button" value="submit">click me</button>
    </table>
    <div id="message">
    <h1>my test</h1>
    </div>
    </form>
    
    </html>
    
    
    
  2. This is include.js
    function loadXMLDoc()
    {
    	var st=document.getElementById("state");
    	var xmlhttp;
    	var txt,x,xx,i;
    	if (window.XMLHttpRequest)
    	{
    		xmlhttp=new XMLHttpRequest();
    	}
    	else
    	{
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	alert(st.value);
    
    	xmlhttp.onreadystatechange=function()
    	{
    		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			x=xmlhttp.responseXML.documentElement.getElementsByTagName("City");
    			var select;
    			for (i=0;i<x.length;i++)
    			{
    
    				xx=x[i].getElementsByTagName("name");
    				select=	document.getElementById("cities");
    				select.options[i] = new Option(xx[0].firstChild.nodeValue, i);
    			}
    
    		}
    
    	}
    
    	var name=st.value;
    	xmlhttp.open("post","city",true);
    	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send("State="+name);
    }
    
  3. This is the Servlet Class
    package com;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class CitiesServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    	}
    
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		String name=request.getParameter("State");
    		PrintWriter out=response.getWriter();
    		response.setContentType("text/xml");
    		String[] state = {"Odisa","Maharastra"}; 
    		String text="<Not/>";
    
    		for (String s : state) {
    			if(s.equalsIgnoreCase(name)&& s.equals("Maharastra"))
    			{
    				text="<State><City><name>nasik</name></City><City><name>mumbai</name></City><City><name>Pune</name></City></State>";
    				break;
    			}
    			if(s.equalsIgnoreCase(name)&&s.equals("Odisa"))
    			{
    				text="<State><City><name>puri</name></City><City><name>bbsr</name></City><City><name>cuttack</name></City></State>";
    				break;
    			}
    		}
    		out.println(text);
    	}
    
    }
    
    
    
    
  4. web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>dynamic-cities-xml</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <description></description>
        <display-name>CitiesServlet</display-name>
        <servlet-name>CitiesServlet</servlet-name>
        <servlet-class>com.CitiesServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>CitiesServlet</servlet-name>
        <url-pattern>/city</url-pattern>
      </servlet-mapping>
    </web-app>