Tuesday, April 30, 2013

Dynamically adding textbox and labels in jsp

1)ajax.jsp:


<%@page import="java.sql.*"%><html>
<head>
<script type="text/javascript">
function showData(value){ 

xmlHttp=GetXmlHttpObject()
var url="getdata.jsp";
url=url+"?name="+value;
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged() { if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    var showdata = xmlHttp.responseText; 
    document.getElementById("lab").innerHTML="Address";
    document.getElementById("address").style.visibility="visible";
    document.getElementById("address").value= showdata;
        } }
function GetXmlHttpObject(){
var xmlHttp=null;
try {
  xmlHttp=new XMLHttpRequest();
 }
catch (e) {
 try  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
</script>
</head>
<body>
<form name="employee">
<br><br>
<pre>
<label>Name</label>     <input  type="text" name="name" id="name" onkeyup="showData(this.value);">
<label id="lab">       </label>  <input style="visibility:hidden" type="text" name="address" id="address">
</form>    </html>
 
 
--------------------------------------------------
 
 
2)getdata.jsp:

<%@ page import="java.sql.*" %> <%
String name = request.getParameter("name").toString();
System.out.println(name);
String data ="";
try{
           
Class.forName("com.mysql.jdbc.Driver");
           
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           
Statement st=con.createStatement();
           
ResultSet rs=st.executeQuery("select * from employee where name='"+name+"'");
while(rs.next())
{
data
=rs.getString("address");
}
out.println(data);
System.out.println(data);
}
catch (Exception e) {
System.out.println(e);
}
%>

No comments:

Post a Comment