Ajax example in Java for country and state

0
Ajax example in Java for country and state:

1)Create table for Country  using MYSQL

create table country(countryid int,countryname varchar(50))

2)Create table for state using MYSQL

create table state(stateid int,statename varchar(50),countryid int)


3)   Create JSP and fill data into dropdownlist of country table
<%--
    Document   : country
    Created on : Dec 14, 2019, 4:57:26 AM
    Author     : Hp
--%>

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
       
        <script>
            function showstate(a)
            {
               
                xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange=function()
                {
                    document.getElementById("state").innerHTML=xmlhttp.responseText;
                   
                }
                xmlhttp.open("POST","state.jsp?s="+a,true);
                xmlhttp.send();
            }
           
           
        </script>
    </head>
    <body>
        <select name="ddlcountry" onchange="showstate(this.value)">
        <%
           
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javatest","root","");
            Statement st = conn.createStatement();
            ResultSet res = st.executeQuery("select * from country");
            while(res.next())
            { %>
            
            <option value="<%= res.getString(1) %>"><%= res.getString(2) %></option>
               
           <% } %>
          
        </select>
           <br>
           <div id="state"></div>
    </body>
</html>


4)  Create onchanage event on dropdownlist using javascript

5)  create state.jsp and show statedata according to countryid

<%--
    Document   : state.jsp
    Created on : Dec 14, 2019, 5:09:44 AM
    Author     : Hp
--%>

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       <select name="ddlstate" >
        <%
           
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javatest","root","");
            Statement st = conn.createStatement();
            ResultSet res = st.executeQuery("select * from state where countryid='"+request.getParameter("s")+"'");
            while(res.next())
            { %>
            
            <option value="<%= res.getString(1) %>"><%= res.getString(2) %></option>
               
           <% } %>
          
        </select>
               
    </body>
</html>

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)