title: Display Json using jquery ajax as response to post request to jsp page
body: <h2>Code to display  json data</h2>
  
  <pre><code>$(document).ready(function(){
        $("button").click(function(){
            $.ajax({
                url:"jsonCreation.jsp",
                type:'post',
                dataType: 'json',
                success: function(data) {
  
                    alert(eval("data = "+data.phone+";"));
                    $('#email').val(data.email);
                }
            });
  
  
  
        });
      });
  </code></pre>
  
  <h2>Code to create json response</h2>
  
  <pre><code>     &lt;%
  
         try{
  
  
  //connection statenebt
  JSONArray respJson = new JSONArray(); 
  java.sql.ResultSetMetaData rsmd = rs.getMetaData();
  int numColumns = rsmd.getColumnCount();
  
  
  
  
  while (rs.next()) { 
  
      JSONObject obj  = new JSONObject();
      for (int i = 1; i &lt; numColumns + 1; i++) {
  
          String columnName = rsmd.getColumnName(i);
          obj.put(columnName, rs.getString(columnName));
  
      }
      respJson.put(obj);
  
  
    }
  
  
     //respJson.toString();
  out.println(respJson.toString());
  System.out.println(respJson);
  
  }
  catch(Exception e)
  { 
  System.out.println(e);  
  }
  
  %&gt;
  </code></pre>
  
  <h2>Output</h2>
  
  <pre><code>   [{
       "phone":"XXXX",
       "fax":"XXX",
       "website":"www.xxxx.xx",
       "enterpriseName":"XXXXLIMITED",
       "enterpriseId":"1001",
       "factoryAddress":"",
       "isActive":"1",
       "category":"Financial   Services",      
       "chairmanName":    "XXXX",
       "administrativeDept ":"Industries Department",
       "incorporationDate":"XXXXX",
       "email":"XXXX.com",
       "incorporationAct":"Companies Act,1956",
       "mdName":"XXXXX",
       "officeAddress":"XXXXXX",
       "activities":"XXXXX for industries XXXXXXXX"
  }]
  </code></pre>
  
  <h2>How to access from website</h2>
  
  <p>Previously i used this code</p>
  
  <pre><code>$.get("jsonCreation.jsp",function(data,status){
  
            alert(data.phone);
            alert(data.email);
          });
  
  &gt;The output of alert was 
  
  
   undefined
  </code></pre>