Values from servlet into dropdownlist in jsp page using ajax

Values from servlet into dropdownlist in jsp page using ajax

1) jsp file

<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="controller.ConnectionString"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript"></script>
<title>Country</title>

<script type="text/javascript">
var htmlres;
function showState(str)
{
    var cou=document.getElementById("cname").value;
    alert("In showState function:"+cou);
    var url="State_drop?cname=" +cou;
    if(window.ActiveXObject)
        {
            htmlres=new ActiveXObject("Microsoft.XMLHTTP");
        }
    else if(window.XMLHttpRequest)
        {
        htmlres=new XMLHttpRequest();
        }
    htmlres.onreadystatechange=changeState;
    htmlres.open("post", url, true);
    htmlres.send();
}

function changeState()
{
    alert("In changeState function");

    if(htmlres.readyState == 4 && htmlres.status == 200)
        {   
            alert("In readystate==4..");

            var res=htmlres.responseText;
            document.getElementById("state").innerHTML=res.text;
        }
}

</script>
</head>
<body>

Select Country: 
<select name="cname" id="cname" onchange="showState(this.value)">
<option value="none">--Select Country--</option>
<%
    ConnectionString cs=new ConnectionString();
    cs.getConn();
    Statement s=cs.conn.createStatement();
    ResultSet rs=s.executeQuery("select * from country");
    while(rs.next())
    {
        //String cid=rs.getString(1);
        //String cname=rs.getString(2);
%>
<option value="<%= rs.getString(2) %>"><%= rs.getString(2) %></option>

<% } 

    s.close(); 
    rs.close(); %>
</select>
<br>  
<br>
<div id="stateid">
Select State: 
<select name="state" id="state">
    <option value='-1'> --Select One--</option>  
</select>  
</div>
</body> 
</html>

2) java file

package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/State_drop")
public class State_drop extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public State_drop() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        System.out.println("In state.jsp");
        PrintWriter out1=response.getWriter();
        response.setContentType("text/xml");
        String country=request.getParameter("cname");

        System.out.println("country name is:"+ country);

        String result="<select name='state'><option value='-1'>Select</option>";  
        try
        {
        ConnectionString cs1=new ConnectionString();
        cs1.getConn();
        int val=0;
        Statement st=cs1.conn.createStatement();
        Statement st1=cs1.conn.createStatement();
        ResultSet rs1=st.executeQuery("select coun_id from country where coun_name= '"+country+"'");
        while(rs1.next())
        {
            val=((Number)rs1.getObject(1)).intValue();  
        }

        System.out.println("val is:"+ val);

        ResultSet rs2=st1.executeQuery("select * from state where coun_id='"+val+"'");
        while(rs2.next())

        {
            result=result + "<option value='"+ rs2.getString(3)+"'>"+rs2.getString(3)+"</option>";

             result= result + "</select>";
            System.out.println("Final result is:"+ result);
            out1.println("result");
        }
        st.close();
        st1.close();
        rs1.close();
        rs2.close(); 

        }
        catch(Exception e)
        {
            out1.println(e);
        }
        }

    }

Im not able to get values retrived from servlet into dropdownlist of jsp page...kindly check the code attached and tell me changes in the same.

View Answers









Related Tutorials/Questions & Answers:
Values from servlet into dropdownlist in jsp page using ajax
Values from servlet into dropdownlist in jsp page using ajax  1) jsp...); } } } Im not able to get values retrived from servlet into dropdownlist of jsp page...kindly check the code attached and tell me
How to carry multiple values from a Servlet to a JSP?
How to carry multiple values from a Servlet to a JSP?  By using the below code I am able to carry the username to a JSP (single value... needs to be carried from my servlet to a JSP. How do I do
Advertisements
Get values from JSP pages - JSP-Servlet
Get values from JSP pages  I have welcome.jsp in which it has... to another JSP page?Do you want to use database to add column values or you want... Name, City, State) into into next jsp page. note these are not Text fields
How to pass multiple values from a servlet to JSP?
How to pass multiple values from a servlet to JSP?  hi, I want to pass multiple values form a servlet to JSP. Hw do i do that? I am able to send one value at a time without any prb, but I am unable to carry multiple (from two
getting values from database - JSP-Servlet
getting values from database  I tried the following code abc.html... JSP code separately.If it will not display database values then try your code in Servlet. Thanks
count values using jsp - JSP-Servlet
count values using jsp  hai deepak i have a query i am dynamically generating textbox with names in that i am having headings... many subheading like that is it posssible with jsp i think it is possible
Calling a jsp page from Servlet
Calling a jsp page from Servlet  How can I do this? Suppose I have jsp page aaa.jsp. From aaa.jsp on form action I have made a call to a servlet... List and data is being retrieve. Now I want to pass this List to another jsp page
how to get a values from processRecord - JSP-Servlet
how to get a values from processRecord  dear sir, i have a problem in getting a values from java to jsp.Here jsp is used to read a excel sheet that intern call a java program so i want a column values i.e a column contains
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
How to get data from DB in to Text box by using Jsp & Ajax
How to get data from DB in to Text box by using Jsp & Ajax   I want to get the data from database in to text box in a jsp page by using Ajax. If I enter the letter A in the textbox it should show only those values which starts
jsp page authentication panel using jsp/servlet?
jsp page authentication panel using jsp/servlet?  I have 10 jsp jsp forms and 7 users and i want to grant variour permission like edit,delete and save for users dynamically on forms.So please refer me code
Passing java variables from JSP to Servlet - return null values
Passing java variables from JSP to Servlet - return null values  I want to pass some variables from a JSP page to a servlet. These variables are from... from JSP to servlet gives null values. I got msg=null. Is there another way
how to get the values to dropdownlist from oracle database
how to get the values to dropdownlist from oracle database   </script> </head> <body> <select name... * from countryname"); while(rs.next()){ %> <option value="<
html dropdownlist code - JSP-Servlet
html dropdownlist code  hi how to get textfield in html when i select the option others in the dropdown list, pls provide the code?   Hi Friend, Try the following code: function show(){ var op
not able to get values from jsp file - JSP-Servlet
not able to get values from jsp file  hi thanks for u feedback, her am sending the file jsp and servlet but in the servlet file am not able to get the value by using method request.getParameter(). it shows null value,but if i
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
retrieving values from dynamically added textboxes in jsp - JSP-Servlet
retrieving values from dynamically added textboxes in jsp  hai friends, iam new to this site ,please help me in this senario in jsp how to retrieve values from dynamically added textbox like we can see in naukri.com
Displaying Constant values from Interface in JSP - JSP-Servlet
Displaying Constant values from Interface in JSP  Hi, ******Sorry... I am directly using the values instead of the constant variables for displaying... for using those constants instead of values::::"PROJ100"; Thank's
how to display values from database into table using jsp
how to display values from database into table using jsp  I want to display values from database into table based on condition in query, how... to display books based on either bookname or authorname, for this i created one jsp page
access image from ajax store in mysql using jsp
access image from ajax store in mysql using jsp  access image from ajax store in mysql using jsp (code to access image captured by camera and store in mysql
How to insert data from a combobox and textbox values into DB using JSP?
How to insert data from a combobox and textbox values into DB using JSP?  hi, How to insert a comb-box and a text box values in to DB using JSP? @DB:student; @table:stu_info; Combobox values:(class1,class2,class3); textbox1
How to download files from server to local disk using ZIP format in JSP page - JSP-Servlet
How to download files from server to local disk using ZIP format in JSP page  hi i have an application to upload and download multiple files. As i can upload multiple files .now i have to download all uploaded files on local
get info from mysql using jsp and servlet
get info from mysql using jsp and servlet  HELLO! I wanna create a jsp page which able to let me get its name, phone and other info by asking the user to key in their email address from mysql database by using servlet and jsp too
Retrieve database from the table dynamically in jsp from oracle using servlet
using java servlet from the database in the jsp page...Retrieve database from the table dynamically in jsp from oracle using servlet  Sir, I have created a table in oracle using eclipse, and added few
how to generate reports from oracle database using jsp and ajax code
how to generate reports from oracle database using jsp and ajax code  Hai masters i am new to this Java world. my team leader ask me to generate sales report data from oracle database to jsp page please any one know how to do
displaying List of records from database in a jsp using ajax
displaying List of records from database in a jsp using ajax  Sir, I...;%@page import="com.tbss.RtChannels"%><%@ taglib uri="http://java.sun.com/jsp... it is not right way. I have written a following servlet to get the records from
how to read values from excel sheet and compare with database using jsp
how to read values from excel sheet and compare with database using jsp  hi sir i am arun how to read values from excel sheet and compare with database using jsp coding i.e, if i have 6(assetid,assetname,serialno,cubical
Hiding form values using ajax
Hiding form values using ajax  i am trying to show and hide form values with ajax but it's not working,but in javascript it is working.Can anyone will help me to the work in ajax please. Thanks in advance
Parameter passing from jsp page to jsp page - JSP-Servlet
Parameter passing from jsp page to jsp page  Hi I intends to pass the parameters that I received from previous page through query String. I get all... Welcome :  For more information on JSP visit
how to retrieve text and images from mysql database and show on html page using jsp servlet
how to retrieve text and images from mysql database and show on html page using jsp servlet  <%@ page language="java" contentType="text/html... {color:blue} <%@ page import="java.sql.*" %> <%@ page import
How to pass parametes from JSP page to HTML page? - JSP-Servlet
to pass the values from my JSP page to HTML page. Pls help me out. Hitendra ...How to pass parametes from JSP page to HTML page?  Hi all, In my project I have one JSP page and one HTML page. In JSP page I have created HTML
By dropdownlist retrive data from database and show in textbox in jsp.
By dropdownlist retrive data from database and show in textbox in jsp.  Hello Sir, I am doing project in jsp.I have to retrive data from database, when I select value from dropdownlist, and that data I want to show in textbox
How to retrieve image from database using jsp and servlet?
How to retrieve image from database using jsp and servlet?  Hi, I am trying to find code for displaying the image from database in a JSP page. How to retrieve image from database using jsp and servlet
jsp and ajax - JSP-Servlet
jsp and ajax  i had some problem i want to display the current system date and time in each and every page of my project just i want the time to be automatically updated for every second for that i want to make an ajax call
Fetching database field from servlet to jsp page ?
Fetching database field from servlet to jsp page ?  Hello Java... field. I wanted to pass some of the database field from servlet to jsp... (i... Request setAttribute method and at the JSP page can retrieve this value using
how to pass form values from javascript of html page to jsp page
how to pass form values from javascript of html page to jsp page   ... to submit all the form values with lattitude and longitude returned from showlocation funtion to submitform.jsp page and display latitude value in jsp page
how to get a values from a multipart/form-data - JSP-Servlet
how to get a values from a multipart/form-data  Dear sir , I... ,matter,file field values but not status and from field i am getting null values and also the file is not uploading the content type in other jsp it is showing
How to get the values from the Combo Box - JSP-Servlet
How to get the values from the Combo Box   Sir, Actually i am getting the values in the combo box from table.I want what ever... in their respective text box. e.g suppose i select ram values from the combo box and its
retrive values - JSP-Servlet
retrive values  how to get multiple values from html to jsp
How to store values in a database using JSTL? - JSP-Servlet
How to store values in a database using JSTL?  I want to store values... extract details and displays in a table format... Now i want to store these values... in database using JSTL name number Position add
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  Can anybody tell me what is the problem in this code as i am not able to fetch the data from... tutorial go through the link JSP Get Data Into Dropdown list From Database   
How to fetch entries/values from database to a jsp page one by one?
How to fetch entries/values from database to a jsp page one by one?  I have a table in Microsoft SQL server Management Studio with two columns title and data and each column has 10 enteries. I have a jsp page on which i want
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  //Customer Name: <select name="name"> <% try{ Class.forName...="select * from staff"; ResultSet rs=st.executeQuery(sql); while(rs.next
how to pass the mutiple values from <Ui:datagrid hyperlink - JSP-Servlet
how to pass the mutiple values from    I am getting the error when passing the values from this way emp2=${employee.lastName1} & name=${employee.firstName.How to pass the values from hyperlink in column
retrive the employee details with image from mysql database using jsp servlet
retrive the employee details with image from mysql database using jsp... stored in the database using jsp servlet then want to show the result in the next jsp page. any one help me out
fetch record from oracle database using jsp-servlet?
fetch record from oracle database using jsp-servlet?  how can i fetch data from oracle database by using jsp-servlet. i'm using eclipse, tomcat server and oracle database and creating jsp pages and also using servlet
how to get data from database into dropdownlist in jsp
how to get data from database into dropdownlist in jsp  Customer Name:<select name="name"> <% try{ Class.forName...=con.createStatement(); String sql="select * from staff"; ResultSet rs
Problem dispalying Country, City, State in JSP using Ajax in Struts Framework - JSP-Servlet
Problem dispalying Country, City, State in JSP using Ajax in Struts Framework  Hi, In my JSP page, I have used one div that adds some rows... showing data from database using ajax, jquery.. but it is not working.. It is because
How to edit values in textboxes from database using jsp
How to edit values in textboxes from database using jsp  Hi RoseIndia, I need help to solve my technical problem, i want to edit values in textboxes from database table using jsp, here is my code Please can anyone help me
Retrieve image from database using servlet and display in JSP
to use MySQL Database from JSP page. How to retrieve image from database using servlet and display in JSP? Thanks...Retrieve image from database using servlet and display in JSP  Hi, I

Ads