Values from servlet into dropdownlist in jsp page using ajax

Ads
 

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

Ads









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
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
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
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
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
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
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
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
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 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 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
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
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
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
How to display image in jsp from database using Servlet?
How to display image in jsp from database using Servlet?  Hi, How to display image in jsp from database using Servlet? Thanks   Hi, You will find code and example program at Retrieve image from database using Servlet
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
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
Populate dropdown menu from database using jsp and servlet
Populate dropdown menu from database using jsp and servlet  please i need code to populate dropdown menu from mysql database using jsp and servlet. thanks
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
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 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
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 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="<
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
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
How to get the data from the database using Servlet or JSP program
How to get the data from the database using Servlet or JSP program  ... by using servlet or jsp program by typing the output as like this below... the problem   Get data from database using servlet Retrieve data from
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

Ads