Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Values from servlet into dropdownlist in jsp page using ajax

Ads

 
 

Share on Google+Share on Google+

Shraddha C
Values from servlet into dropdownlist in jsp page using ajax
0 Answer(s)      5 years ago
Posted in : Ajax

1) jsp file

<[email protected] import="java.sql.Statement"%>
<[email protected] import="java.sql.ResultSet"%>
<[email protected] 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.

Ads
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 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 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
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
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
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
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 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
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 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 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   
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
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 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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