Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Ajax Selection With Ajax and JSP

Have Programming Question? Ask it here!
 
 
 


moh Dah
Selection With Ajax and JSP
2 Answer(s)      2 years and 4 months ago
Posted in : Ajax


I am working at a jsp page using ajax for country , state, city selection. so if he select country it will populate the state and city selection (both). After selecting country if he select city it will populate the state selection and if he select the state it will populate the city. I am doing it through two jsp pages only. Country1.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>

Insert title here

      </head>  
      <body>  
      <table border="1">
      <tr><th>Country</th><th>State</th><th>City</th></tr>
      <tr><td>
      <select id='count' name='country' onchange="showState()">  
       <option value="none">Select</option>
       <%
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection con = DriverManager.getConnection("jdbc:odbc:senior");
       Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
       ResultSet rs = stat.executeQuery("Select country from hotel");

       while(rs.next())
    {
           String co=rs.getString("country");

           %>

        <option value="co"><%=co%></option>
  <%
  }
 %>
  </select> 
  </td>
  <td id='change'><select id='stat' name='state' >  
  <option value='-1'></option>  
  </select>
  <select id='cit' name='city' >  
  <option value='-1'></option>  
  </select>

  </tr>
  </table>
  </body> 
  </html>


**Country2.jsp**



 <%@page import="java.sql.*"%>
<%
String country=request.getParameter("count");
String city=request.getParameter("city");
String state=request.getParameter("state");
 String buffer="<select id='stat' name='state' onchange='showCity();'><option value='-1'>Select</option>"; 
 String buffer2="<select id='cit' name='city' onchange='showstate();'><option value='-1'>Select</option>";  
 try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection con = DriverManager.getConnection("jdbc:odbc:senior");
     Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
 ResultSet rs=null;
 ResultSet rs2=null;
 if(country!=null && city==null && state==null ){
     rs = stmt.executeQuery("Select state from hotel where country='"+country+"' ");

     while(rs.next()){
           buffer=buffer+"<option value='"+rs.getString("state")+"'>"+rs.getString("state")+"</option>"; 
           }

     rs = stmt.executeQuery("Select city from hotel where country='"+country+"' ");
     while(rs.next()){
           buffer2=buffer2+"<option value='"+rs.getString("city")+"'>"+rs.getString("city")+"</option>"; 
           }


 }
 else
     if(country!=null && city!=null && state==null ){
     rs = stmt.executeQuery("Select state from hotel where country='"+country+"' AND city='"+city+"' "); 
     while(rs.next()){
           buffer=buffer+"<option value='"+rs.getString("state")+"'>"+rs.getString("state")+"</option>";  
           }

     }
 else
     if(country!=null && city==null && state!=null ){
     rs = stmt.executeQuery("Select city from hotel where country='"+country+"' AND tate='"+state+"' ");
       while(rs.next()){
           buffer2=buffer2+"<option value='"+rs.getString("city")+"'>"+rs.getString("city")+"</option>";  
           }

     } 
 buffer=buffer+"</select>";
 buffer2=buffer2+"</select>";
 buffer=buffer+buffer2;
 response.getWriter().println(buffer); 

 }
 catch(Exception e){
     System.out.println(e);
 }

 %>

The database is called senior on Microsoft access , with table called hotel(ID,Country,State,city).


Advertisement
View Answers

April 2, 2012 at 10:50 AM


1)country.jsp:

 <%@page import="java.sql.*"%>
 <html>
      <head>  
      <script language="javascript" type="text/javascript">  
      var xmlHttp  
      var xmlHttp
      function showState(str){
      if (typeof XMLHttpRequest != "undefined"){
      xmlHttp= new XMLHttpRequest();
      }
      else if (window.ActiveXObject){
      xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
      }
      if (xmlHttp==null){
      alert("Browser does not support XMLHTTP Request")
      return;
      } 
      var url="state.jsp";
      url +="?count=" +str;
      xmlHttp.onreadystatechange = stateChange;
      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);
      }

      function stateChange(){   
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
      document.getElementById("state").innerHTML=xmlHttp.responseText   
      }   
      }

      function showCity(str){
      if (typeof XMLHttpRequest != "undefined"){
        xmlHttp= new XMLHttpRequest();
        }
      else if (window.ActiveXObject){
        xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
      if (xmlHttp==null){
      alert("Browser does not support XMLHTTP Request")
      return;
      } 
      var url="city.jsp";
      url +="?count=" +str;
      xmlHttp.onreadystatechange = stateChange1;
      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);
      }
      function stateChange1(){   
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
      document.getElementById("city").innerHTML=xmlHttp.responseText   
      }   
      }
      </script>  
      </head>  
      <body>  
      <select name='country' onchange="showState(this.value)">  
       <option value="none">Select</option>  
    <%
 Class.forName("com.mysql.jdbc.Driver").newInstance();  
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from country");
 while(rs.next()){
     %>
      <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>  
      <%
 }
     %>
      </select>  
      <br>  
      <div id='state'>  
      <select name='state' >  
      <option value='-1'></option>  
      </select>  
      </div>  

      <div id='city'>  
      <select name='city' >  
      <option value='-1'></option>  
      </select>  
      </div>
      </body> 
      </html>

2)state.jsp:

<%@page import="java.sql.*"%>
<%
String country=request.getParameter("count");  
 String buffer="<select name='state' onchange='showCity(this.value);'><option value='-1'>Select</option>";  
 try{
 Class.forName("com.mysql.jdbc.Driver").newInstance();  
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from state where countryid='"+country+"' ");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>";  
   }  
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer); 
 }
 catch(Exception e){
     System.out.println(e);
 }

 %>


April 2, 2012 at 10:51 AM


continue..

3)city.jsp:

<%@page import="java.sql.*"%>
<%
String state=request.getParameter("count");  
 String buffer="<select name='city'><option value='-1'>Select</option>";  
 try{
 Class.forName("com.mysql.jdbc.Driver").newInstance();  
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
 Statement stmt = con.createStatement();  
 ResultSet rs = stmt.executeQuery("Select * from city where stateid='"+state+"' ");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(2)+"'>"+rs.getString(3)+"</option>";  
   }  
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer); 
 }
 catch(Exception e){
     System.out.println(e);
 }
 %>

We have created 3 dependent dropdown. You can create the fourth one similarly.

For the above code, we have used 3 database tables:

1)country

CREATE TABLE `country` (                                 
           `countryid` bigint(255) NOT NULL auto_increment,       
           `countryname` varchar(255) default NULL,               
           PRIMARY KEY  (`countryid`)                             
     )

2)state

CREATE TABLE `state` (                                   
          `stateid` bigint(255) NOT NULL auto_increment,         
          `countryid` int(255) default NULL,                     
          `state` varchar(255) default NULL,                     
          PRIMARY KEY  (`stateid`)                               
        )

3)city

CREATE TABLE `city` (                                    
          `cityid` bigint(255) NOT NULL auto_increment,          
          `stateid` int(255) default NULL,                       
          `city` varchar(255) default NULL,                      
          PRIMARY KEY  (`cityid`)                                
        )


Related Tutorials/Questions & Answers:
Selection With Ajax and JSP
Selection With Ajax and JSP  I am working at a jsp page using ajax for country , state, city selection. so if he select country it will populate the state and city selection (both). After selecting country if he select city
Selection based on other selection in jsp
selection on same jsp page such that when someone select a category only the products...Selection based on other selection in jsp  I am trying to create a jsp page. Where I am having a select list containing category name and for each
Advertisements
checkbox selection in jsp
checkbox selection in jsp  hey guys i am working on a web based project using jsp. In my project i am having 9 check boxes in 3 rows in the same form. I want to select i check box from each row and also i want to avoid many
jsp & ajax
jsp & ajax  how to enable or disable textbox using radio buttons by using jsp and ajax? plz help me.... i m new in jsp & ajax
jsp and ajax
jsp and ajax  how to enable or disable textbox using radio buttons by using jsp and ajax
ajax jsp - Ajax
ajax jsp  multiple combo with ajax using jsp?  Hi friend, I am sending you a link. This link will help you. Please visit for more information. http://www.roseindia.net/jsp/comboSelect.shtml Thanks
Ajax with jsp - Ajax
Ajax with jsp  multiple combo boxes with ajax in jsp?  Hi friend, I am sending you a link. I hope that, this link will help you. Please visit for more information. http://www.roseindia.net/jsp
Modifying a single row of database based on selection in jsp
Modifying a single row of database based on selection in jsp  Hi guys...://www.roseindia.net/answers/viewqa/JSP-Servlet/18899-data-grid-with-edit-and-delete-options-at-each-row-.html http://www.roseindia.net/answers/viewqa/JSP
using jsp's....and ajax - Ajax
using jsp's....and ajax  Hi, i need code using ajax .....in a text box when i enter an alphabet i should get list of words starts with the alphabet given in the text box
Drop-down text selection in jsp/html
Drop-down text selection in jsp/html  Hi, I am trying to create JSP page where I have two drop-downs. There are two-sets of data - First set Categories- Movies, Books, Music and the Second set Genres- Action,Comedy,Romantic
Syntax for image selection Urgent - JSP-Servlet
Syntax for image selection Urgent  Respected Sir/Madam, I am using the following syntax to know whether radio button has been clicked or not: for(var i=0;i  Hi friend, now show the question no 2129 Here
jsp - Ajax
jsp  I'm very new in using the jsp and ajax, i have some JSP pages and i need to add some ajax features in my jsp page, for the example i need.... How can I implement this and then add this to my jsp page? please
ajax code with jsp - Ajax
ajax code with jsp  hi , Sorry for insufficient data.i m making some correction on question. I have to perform some calculation on my jsp page. i want to do it automatically (using ajax in jsp). problem is - i hv two
Image Selection Reply Urgent.. - JSP-Servlet
Image Selection Reply Urgent..  Respected Sir/Madam, I am R.Ragavendran.. Thanks for yuor fast response.. Sir actually I will be much more convenient if you provide me with coding.. I dont know to work with images as i
Program Arrow selection Most Urgent - JSP-Servlet
Program Arrow selection Most Urgent  Respected Sir/Madam, I am R.Ragavendran.. I got your coding which is highly appreciable.. As per my requiremennt,there are image buttons for each row in the database.. But i am
ajax and jsp code - Ajax
ajax and jsp code  can u please give me the code for retriving the data from database using ajax our requriment is if i select country name in listbox display the corresponding all the states. using jsp and ajax   
How to create textbox on combo value selection using javacsript in jsp?
How to create textbox on combo value selection using javacsript in jsp?  dynamically create textbox on combo value selection. when select multiple values then create multiple textboxes
JSP and AJAX very urgent - Ajax
JSP and AJAX very urgent  Hi Team, This is Ragavendran.R.. I have a very basic doubt in AJAX. While Using AJAX, of course, there will be tag involved in JSP page. But in my current project, I am using too many tags
Jsp and ajax
Jsp and ajax  Hi I am using jsp and ajax.I am retrieving...; function ajax(str){ var xmlHttp; var i; var config=document.getElementById..."; url=url+"?q="+config+"&q1="+config1; xmlHttp.open("POST",url,true
Jsp and ajax
Jsp and ajax  Hi I am using jsp and ajax.I am retrieving...; function ajax(str){ var xmlHttp; var i; var config=document.getElementById..."; url=url+"?q="+config+"&q1="+config1; xmlHttp.open("POST",url,true
Jsp and ajax
Jsp and ajax  Hi I am using jsp and ajax.I am retrieving...; function ajax(str){ var xmlHttp; var i; var config=document.getElementById..."; url=url+"?q="+config+"&q1="+config1; xmlHttp.open("POST",url,true
Jsp and ajax
Jsp and ajax  Hi I am using jsp and ajax.I am retrieving...; function ajax(str){ var xmlHttp; var i; var config=document.getElementById..."; url=url+"?q="+config+"&q1="+config1; xmlHttp.open("POST",url,true
ajax in jsp
ajax in jsp  i m not able to compare string with the responseText value, though the value in the responsetext is of string type. my code: login.jsp <html> <head> <meta http-equiv
ajax in jsp
ajax in jsp  i m not able to compare string with the responseText value, though the value in the responsetext is of string type. login.jsp: print("<html> <head> <meta http-equiv="Content-Type" content="text
jsp and ajax - JSP-Servlet
jsp and ajax  i had some problem i want to display the current... to be automatically updated for every second for that i want to make an ajax call.../jsp/fileupload.shtml Thanks
JSP and AJAX- very urgent - Ajax
JSP and AJAX- very urgent  Respected Sir/Madam, I am... (Using AJAX. for ur reference, I have included the coding below: Login.html... not support AJAX!"); return; } var url="check.jsp"; url=url+"?id="+alpha
Image selection instead of radio button very urgent.. - JSP-Servlet
Image selection instead of radio button very urgent..  Respected Sir/Madam, I am R.Ragavendran.. Actually in the following using radio... successfully. But I need some icon or some pictorial selection instead
ajax code for jsp
ajax code for jsp  How to write ajax code to retrieve information on to particular part of webpage when we select option from drop down box
code for jsp - Ajax
country.By using jsp and Ajax.    Hello Friend I send the code you...code for jsp  please give code for using jsp page and Ajax.Retrive... getDetail(source,div){ getData(source,div); } An Ajax Demo
Ajac code with jsp - Ajax
Ajac code with jsp  hi , i am beginner for ajax.I have to perform some calculation on my jsp page. i want to do it automatically that is possible using ajax. problem is - i hv two text box. 1st take
ajax in java - JSP-Servlet
ajax in java  The below code is for a php page but I want this in JSP page. function postRequest(strURL){ var xmlHttp...: "login.php"   Hi friend, Do some changes to convert in JSP
Ajax validation - JSP-Servlet
Ajax validation  How to fade out the images when an onblur function is called moving from a text box to another?? can anyone help out with with the code
Ajax - JSP-Servlet
Ajax  Simple ajax code for getting one text box value  Hi friend, Code to help in solving the problem : function postRequest... on Ajax visit to : http://www.roseindia.net/ajax/ Thanks
The AJAX JSP Tag Library
The AJAX JSP Tag Library       The AJAX JSP Tag Library is a set of JSP tags that simplify the use of Asynchronous JavaScript and XML (AJAX) technology in JavaServer Pages. This tag
DropDown and text boxes with AJAX, JSP - Ajax
present. we are already using jsp's and java technology. can we integrate ajax in jsp's to achieve the same? if so could you please guide me to achive the same...DropDown and text boxes with AJAX, JSP  Hi, we are using one drop
refresh jsp page - Ajax
refresh jsp page  Code for refresh a web page in jsp.  Hi friend, function refreshpage() { window.location.reload
Ajax using jsp
Ajax using jsp  <%@ page import="java.io.*" %> <%@ page import="java.util.*" %> <%@ page import="java.sql.*" %> <%@ page import...); } %> Is there Any error...........In first Page I use ajax for displaying
ajax using jsp
ajax using jsp  <%@ page import="java.io.*" %> <%@ page import="java.util.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.StringTokenizer" %> <%@ page import="java.lang.*" %> <
datagid with paging using jsp - Ajax
datagid with paging using jsp  datagrid with paging using ajax and jsp  Hi friend, For read more information : http://www.roseindia.net/jsp/data-grid.shtml Thanks
Ajax not working in jsp
Ajax not working in jsp  I'm using Netbean, Ajax validation in the following code is not working the code succesfully run for first UID validation...; frmMain.elements[i].valid; } } return blnValid; } //if Ajax
DropDown in ajax+jsp
DropDown in ajax+jsp  I have four dropdown if i select first dd then only corresponding values must be there in 2nd dd,same with 3 and 4 and onchangfe it would not refresh the whole page.   Hi Friend, Try the following
application using AJAX and JQuery and also use JSP
application using AJAX and JQuery and also use JSP   i need create an application using AJAX and JQuery and also use JSP
sir plz help in design a jsp page whichis as follow username [_____] select [__>]password [_____] after selection then onlypassword text box is visible
sir plz help in design a jsp page   sir plz help in design a jsp page which is as follow username [ _ ] select [ > ] password [ _ ] after selection then only password text box is visible
Ajax
Ajax  how to impliment ajax in registration table using jsp-servlet
JSP Ajax Form
Ajax Form Example This example explains you how to develop a form that populates dynamically from the database though Ajax call. When user selects employee id from the combo box then the corresponding employee details are populated
Ajax
Ajax  how to include ajax in jsp page?   Hi, Please read Ajax First Example - Print Date and Time example. Instead of using PHP you can write your code in JSP. Thanks
how to implement ajax in struts2 to operate on selectbox in jsp
how to implement ajax in struts2 to operate on selectbox in jsp  I am doing a project on struts2 in which i have a jsp page which has 2 select boxes... the ajax wll be called and retrieve me the blocks under the selected district
insert multiple selection - Java
insert multiple selection - Java  how to insert multiple selection values from html into database using servlets
Ajax with java(using Dojo) - JSP-Servlet
Ajax with java (using Dojo)  Hi, In In place Edit Box,when I click "save" button how can I save the data in server side i.e JSP or how can I call.... Is there a possibility for tutorial on Scriptaculous. us,for Java programmers
Advertisements
 

 

 

DMCA.com