ajax and jsp code

ajax and jsp code

View Answers

September 17, 2008 at 1:48 PM

Hello

You have to change here your database name , tablename, column name

Data.java(Through this class you retrieve statename of a selected country)
--------------------------------------------------

package com.roseindia;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Data extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{

String value = req.getParameter("country");
String dbQuery = "Select StateName FROM STATES WHERE CountryName='" + value +"'";
PrintWriter out = res.getWriter();
String result="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase";, "root", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(dbQuery);
while(rs.next()){
result = result + rs.getString("StateName") + ",";
}
}catch(Exception ex){

System.out.println(ex.getMessage());

}
out.write(result);
}

}

Look this jsp here only i took two select box in one contain contry name and in oter it disp;lay corresponding states

SelectDemo.jsp
---------------

<html>

<head>
<script language="javascript">
var xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
function selctData(form)
{
var source = (form.first.options[form.first.selectedIndex].text)
var url = "../Data?country="+source + "&r=" + new Date().getTime() ;
alert(url);
xmlObj.open("GET", url)
alert("3");
xmlObj.onreadystatechange = function()
{
if(xmlObj.readystate == 4 && xmlObj.status == 200)
{
var id = document.getElementById("second");
var result = xmlObj.responseText;

var s = result.split(',');
var indx = 0;
for(indx=0; indx < s.length; indx++)
{
id.options[indx] = new Option(s[indx],s[indx]);
}
}
}
xmlObj.send(null);
}
</script>
</head>
<body>
<h1>On Select change data of another Select</h1>

<br>
<form>
<select id="first" onchange="selctData(this.form)">
<option value="">Select One </option>
<option>india</option>
<option>saudi</option>
</select>
<select id="second"></select>
</form>
</body>
</html>


Thanks
Rajanikant

September 17, 2008 at 1:48 PM

Hello

You have to change here your database name , tablename, column name

Data.java(Through this class you retrieve statename of a selected country)
--------------------------------------------------

package com.roseindia;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Data extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{

String value = req.getParameter("country");
String dbQuery = "Select StateName FROM STATES WHERE CountryName='" + value +"'";
PrintWriter out = res.getWriter();
String result="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase";, "root", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(dbQuery);
while(rs.next()){
result = result + rs.getString("StateName") + ",";
}
}catch(Exception ex){

System.out.println(ex.getMessage());

}
out.write(result);
}

}

Look this jsp here only i took two select box in one contain contry name and in oter it disp;lay corresponding states

SelectDemo.jsp
---------------

<html>

<head>
<script language="javascript">
var xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
function selctData(form)
{
var source = (form.first.options[form.first.selectedIndex].text)
var url = "../Data?country="+source + "&r=" + new Date().getTime() ;
alert(url);
xmlObj.open("GET", url)
alert("3");
xmlObj.onreadystatechange = function()
{
if(xmlObj.readystate == 4 && xmlObj.status == 200)
{
var id = document.getElementById("second");
var result = xmlObj.responseText;

var s = result.split(',');
var indx = 0;
for(indx=0; indx < s.length; indx++)
{
id.options[indx] = new Option(s[indx],s[indx]);
}
}
}
xmlObj.send(null);
}
</script>
</head>
<body>
<h1>On Select change data of another Select</h1>

<br>
<form>
<select id="first" onchange="selctData(this.form)">
<option value="">Select One </option>
<option>india</option>
<option>saudi</option>
</select>
<select id="second"></select>
</form>
</body>
</html>


Thanks
Rajanikant









Related Tutorials/Questions & Answers:
ajax code with jsp - Ajax
ajax code with jsp  hi , Sorry for insufficient data.i m making some correction on question. I have to perform some calculation on my jsp page. i want to do it automatically (using ajax in jsp). problem is - i hv two
ajax and jsp code - Ajax
ajax and jsp code  can u please give me the code for retriving the data from database using ajax our requriment is if i select country name in listbox display the corresponding all the states. using jsp and ajax   
Advertisements
ajax code for jsp
ajax code for jsp  How to write ajax code to retrieve information on to particular part of webpage when we select option from drop down box
code for jsp - Ajax
country.By using jsp and Ajax.    Hello Friend I send the code you...code for jsp  please give code for using jsp page and Ajax.Retrive... getDetail(source,div){ getData(source,div); } An Ajax Demo
Ajac code with jsp - Ajax
Ajac code with jsp  hi , i am beginner for ajax.I have to perform some calculation on my jsp page. i want to do it automatically that is possible using ajax. problem is - i hv two text box. 1st take
how to generate reports from oracle database using jsp and ajax code
how to generate reports from oracle database using jsp and ajax code  ... sales report data from oracle database to jsp page please any one know how to do this send me a code to my email:[email protected] its a humble request my team
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
ajax code - Ajax
ajax code  hello sir, how can i insert a text field dynamically by using ajax   Hi Friend, Try the following code: 1)ajax.jsp...= document.getElementById("name").value; window.open("http://localhost:8080/examples/jsp
jsp and ajax
jsp and ajax  how to enable or disable textbox using radio buttons by using jsp and ajax
jsp & ajax
jsp & ajax  how to enable or disable textbox using radio buttons by using jsp and ajax? plz help me.... i m new in jsp & ajax
Ajax code
Ajax code   I want an ajax code in jsp where selecting a listbox changes the other selectbox values .Suppose take a country as one select box...; For the above code, we have created two tables: 1)country: CREATE TABLE `country
code problem:ajax - Ajax
code problem:ajax  Hi,I am using ajax to populate a select box.for this I am writing out.write("ONE"); like that.it runs fine in firefox.bt not in IE.Can anyone help me out this... thanks
Ajax with jsp - Ajax
Ajax with jsp  multiple combo boxes with ajax in jsp?  Hi friend, I am sending you a link. I hope that, this link will help you. Please visit for more information. http://www.roseindia.net/jsp
ajax jsp - Ajax
ajax jsp  multiple combo with ajax using jsp?  Hi friend, I am sending you a link. This link will help you. Please visit for more information. http://www.roseindia.net/jsp/comboSelect.shtml Thanks
jsp - Ajax
jsp  I'm very new in using the jsp and ajax, i have some JSP pages and i need to add some ajax features in my jsp page, for the example i need.... How can I implement this and then add this to my jsp page? please
ajax code
ajax code  Explain me this code function disp_branch(val) { if(unit_branch!=val){ unit_branch=val; xmlHttp=GetXmlHttpObject... not support HTTP Request") return } var url="index.php?q=<
ajax code - Ajax
ajax code  hi can any body tell me how i can create an autocomplete textbox in java using ajax. For example in google when we type any thing it will reflect the matching details according the character.. Hope some body
problem with code - Ajax
to use ajax i wrote the code of jsp and the remaning code by using ajax is not wrritened by observing these below code please try the remainning ajax code its...problem with code  hi friends i am sending a code in that when we
Jsp and ajax
Jsp and ajax  Hi I am using jsp and ajax.I am retrieving... code. Plz anybody help me.Its urgent. al.jsp <%@ page import...; function ajax(str){ var xmlHttp; var i; var config=document.getElementById
Jsp and ajax
Jsp and ajax  Hi I am using jsp and ajax.I am retrieving... code. Plz anybody help me.Its urgent. al.jsp <%@ page import...; function ajax(str){ var xmlHttp; var i; var config=document.getElementById
Jsp and ajax
Jsp and ajax  Hi I am using jsp and ajax.I am retrieving... code. Plz anybody help me.Its urgent. al.jsp <%@ page import...; function ajax(str){ var xmlHttp; var i; var config=document.getElementById
Jsp and ajax
Jsp and ajax  Hi I am using jsp and ajax.I am retrieving... code. Plz anybody help me.Its urgent. al.jsp <%@ page import...; function ajax(str){ var xmlHttp; var i; var config=document.getElementById
How to use ajax in jsp?
How to use ajax in jsp?  Hi, How i can access the server-side data in JSP using the Ajax? Thanks   Hi, You can use the Ajax code to access the server side data from JSP page. Check the tutorial Combo Box Using Ajax
JSP and AJAX very urgent - Ajax
JSP and AJAX very urgent  Hi Team, This is Ragavendran.R.. I have a very basic doubt in AJAX. While Using AJAX, of course, there will be tag involved in JSP page. But in my current project, I am using too many tags
ajax in jsp
ajax in jsp  i m not able to compare string with the responseText value, though the value in the responsetext is of string type. my code: login.jsp <html> <head> <meta http-equiv
jsp and ajax - JSP-Servlet
jsp and ajax  i had some problem i want to display the current... to be automatically updated for every second for that i want to make an ajax call.../jsp/fileupload.shtml Thanks
ajax in jsp
ajax in jsp  i m not able to compare string with the responseText value, though the value in the responsetext is of string type. login.jsp: print("<html> <head> <meta http-equiv="Content-Type" content="text
JSP and AJAX- very urgent - Ajax
JSP and AJAX- very urgent  Respected Sir/Madam, I am... (Using AJAX. for ur reference, I have included the coding below: Login.html... not support AJAX!"); return; } var url="check.jsp"; url=url+"?id="+alpha
Sample Ajax Code
Sample Ajax Code  Sample Ajax Code for getting values from another JSP   The below code is helpful to access another Action class //In Main JSP file var xmlHttpObj,xmlHttpObj1; function getXmlHttpObject() { var
jsp code
jsp code  what are the jsp code for view page in online journal
JSP CODE
JSP CODE  what is the code for downloading images from database using JSP?   Please visit the following link: http://www.roseindia.net/jsp/downloadimage.shtml
jsp+ajax validation
jsp+ajax validation  hi, I have created the jsp form (course... 60% in the drop down list .how to validate student from using ajax and jsp... have created student jsp form where it should display register number
jsp code
jsp code  hi i am Ruchi can anybody plz tell me the jsp code... visit the following links: http://www.roseindia.net/jsp/user-search.shtml http://www.roseindia.net/servlets/search.shtml www.roseindia.net/jsp/searchbook.shtml
jsp code
jsp code  i want health management system project code using jsp.its urgent
ajax in java - JSP-Servlet
ajax in java  The below code is for a php page but I want this in JSP...: "login.php"   Hi friend, Do some changes to convert in JSP page and remember some points : 1. replace the code var url = "login.php
JSP CODE
JSP CODE  Please help me as soon as possible.Its Urgent. I am working on my college ALUMNI PORTAL. I want to have a ADD FRIEND option in a user's profile. Please send me code
jsp code
jsp code  i want to jsp code which are working as,i have three drop down menu 1st menu for class second menu for subject and third menu as chapter... according to selectde menu,,,,please send me such type code,,,,plz help me
Ajax validation - JSP-Servlet
Ajax validation  How to fade out the images when an onblur function is called moving from a text box to another?? can anyone help out with with the code
refresh jsp page - Ajax
refresh jsp page  Code for refresh a web page in jsp.  Hi friend, function refreshpage() { window.location.reload
Ajax - JSP-Servlet
Ajax  Simple ajax code for getting one text box value  Hi friend, Code to help in solving the problem : function postRequest... on Ajax visit to : http://www.roseindia.net/ajax/ Thanks
jsp code - JSP-Servlet
jsp code  Can anyone help me in writing jsp/servlet code to retrieve files and display in the browser from a given directory.  Hi Friend, Try the following code: Thanks
jsp code
jsp code  how to display date in drop down list by jsp thus the date start from current date and for next year it has to check leap year function to february then it display 30 for some months else 31 for other months
jsp code - JSP-Servlet
jsp code  sample code for change password example Old Password: new Password: confirm Password
JSP code
JSP code  I get an error when i execute the following code : <... = con.createStatement(); st.executeQuery(query); %> <jsp:forward page="address.jsp"></jsp:forward> HTTP Status 500 - type Exception report message
jsp code - JSP-Servlet
jsp code  I need code for bar charts using jsp. I searched some code but they are contain some of their own packages. Please give me asimple code... friend, Code to solve the problem : Thanks
jsp code - JSP-Servlet
jsp code  sample code to create hyperlink within hyperlink example: reservation: train: A/C department non A/c Department
jsp code - JSP-Servlet
jsp code  how to count no of clicks made on a link and save it on database( using jsp)  Hi Friend, Try the following code: 1)click.jsp var numberOfClicks = 0; function hello(){ numberOfClicks
jsp code - JSP-Servlet
jsp code  in a jsp a table is existed ,in table each row contain one checkbox and name and desription how to delete specific rows in a table .  Hi Friend, Try the following code: 1)form.jsp: Name
jsp code plz
jsp code plz  write jsp code which takes student roll number as input...;Please visit the following links: http://www.roseindia.net/jsp/user-search.shtml http://www.roseindia.net/jsp/popup-window-using-ajax-in-jsp.shtml The above links
JSP code - JSP-Servlet
me how to link these pages with each other using JSP..and how to write the code using JSP.. requriment for Newuser: 1>fname,lastname 2>Loginname...JSP code  hello,i working in project with JSP technology,i have one

Ads