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



Home Answers Viewqa Ajax Values from servlet into dropdownlist in jsp page using ajax
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Shraddha C
Values from servlet into dropdownlist in jsp page using ajax
0 Answer(s)      2 years and 3 months ago
Posted in : 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.


Advertisement
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 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="<
Advertisements
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
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
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
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
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 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 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
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
how to get data using dropdownlist, textbox and search button in jsp
how to get data using dropdownlist, textbox and search button in jsp  Hi, I want to display data using jsp,javascript and mysql. My Q. is If i select an entry suppose 'title' from dropdownlist and give a title name 'partial dif
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
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
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 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
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
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
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
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
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 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
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
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
dynamic retrival of data from mysql database to dropdownlist in jsp
dynamic retrival of data from mysql database to dropdownlist in jsp  Hello, Am having problem in my project... i want to retrive the data from mysql database to the dropdownlist in jsp. Example: 1st i want to display the list
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
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... :  For more information on JSP visit to : http
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
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
Advertisements
 

 

 

DMCA.com