ajax and jsp code

Ads
 

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

Ads









Related Tutorials/Questions & Answers:
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
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
Advertisements
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
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=<
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
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
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 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 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
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  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
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
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
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 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
code in jsp
code in jsp  hi.. I had started a project on college admission system in which two operators has been introduced one is admin who can view all... want a code to sort a list of students on the basis of there 12% marks and AIEEE
Ajax using jsp
Ajax using jsp  <%@ page import="java.io.*" %> <%@ page...); } %> Is there Any error...........In first Page I use ajax for displaying... code........in the above code iam getting all the data that present in previous
The AJAX JSP Tag Library
The AJAX JSP Tag Library       The AJAX JSP Tag Library is a set of JSP tags that simplify the use of Asynchronous JavaScript and XML (AJAX) technology in JavaServer Pages. This tag
Selection With Ajax and JSP
Selection With Ajax and JSP  I am working at a jsp page using ajax for country , state, city selection. so if he select country it will populate... the city. I am doing it through two jsp pages only. Country1.jsp : <%@ page
Ajax not working in jsp
Ajax not working in jsp  I'm using Netbean, Ajax validation in the following code is not working the code succesfully run for first UID validation...; frmMain.elements[i].valid; } } return blnValid; } //if Ajax
ajax using jsp
ajax using jsp  <%@ page import="java.io.*" %> <%@ page import="java.util.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.StringTokenizer" %> <%@ page import="java.lang.*" %> <
insert code jsp to access
insert code jsp to access   insert code jsp to access

Ads