Monday 1 April 2013

Ajax example using with jsp



// Using this Ajax example you can show data with jsp into text area, text field. 

<%@ page import="java.util.*" %>
<%@ page import="java.sql.*"  %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.*"  %>

<% 
  Connection con = null;
  ResultSet rs = null;
  Statement stmt = null;
  
  Class.forName("net.sourceforge.jtds.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/amantrn", "aman","kumar");
 // stmt = con.createStatement();
%>

<%-- <% --%>
<!--   String Query1="SELECT [id],[username],[password],[email],[profile] FROM [admin]"; -->
<!--   stmt = con.createStatement(); -->
<!--   ResultSet rs1 = stmt.executeQuery(Query1); -->
  
<!-- %> -->

<script language="javascript" type="text/javascript">
 var xmlHttp;
 function modifyData(Str) {
  
  var versionIds = [ "Msxml2.XMLHTTP", "Microsoft.XMLHTTP",
    "Microsoft.XMLDOM", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0",
    "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0",
    "Msxml2.XMLHTTP.2.6", "Microsoft.XMLHTTP.1.0",
    "Microsoft.XMLHTTP.1" ];
  if (typeof XMLHttpRequest != "undefined") {
   xmlHttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
   for ( var i = 0; i < versionIds.length; i++) {
    try {

      return new ActiveXObject(versionIds[i]);

     } catch (e) {
     alert("Browser does not support XMLHTTP Request");
    }
   }
   new XDomainRequest();
  }
  if (xmlHttp == null) {
   alert("Browser does not support XMLHTTP Request");
   return;
  }
  
  var url = "modify.jsp?";
  url += "id=" + Str;
  url += "&sid=" + Math.random();
  xmlHttp.onreadystatechange = stateChange;
  xmlHttp.open("POST", url, true);
  xmlHttp.send(null);
  
  function stateChange() {
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    var abc = xmlHttp.responseText.split("~");
    //alert(abc);
    document.getElementById('id').value = abc[0];//id
    document.getElementById('name').value = abc[1];//Username
    document.getElementById('pass').value = abc[2];//Password
    document.getElementById('email').value = abc[3];//Email
    //document.getElementById("id").value = abc[4];//Profile
   }
  }
 }
</script>

<form name="modify.jsp">
<table>
  <tr>
   <td>Id:</td>
     <td><input type="text" name="id" id="id" value="" onblur='modifyData(this.value);'/></td>
  </tr>
    <tr>
   <td>Name:</td> 
    <td><input type="text" name="name" id="name" value=""/></td>
  </tr>
   <tr>
   <td>Password:</td>
   <td><input type="password" name="pass" id="pass" value="" /></td>
  </tr>
   <tr>
   <td>Email:</td>
   <td><input type="text" name="email" id="email" value="" /></td>
  </tr>
  <tr>
  <td><input type="reset" value="Clear"></td>
  <td></td>
  </tr>
</table>
</form>


// modify.jsp 


<%@ page import="java.util.*" %>
<%@ page import="java.sql.*"  %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.*"  %>

<% 
  Connection con = null;
  ResultSet rs = null;
  Statement stmt = null;
  
  Class.forName("net.sourceforge.jtds.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/amantrn", "aman","Kumar");
  stmt = con.createStatement();
%>

<%
  String id = request.getParameter("id").trim();
  String Query1="SELECT [id],[username],[password],[email],[profile] FROM [admin] where id like '"+id+"%' ";
  stmt = con.createStatement();
  ResultSet rs1 = stmt.executeQuery(Query1);
  while(rs1.next()){
%>

<%=rs1.getString("id").trim()%>~<%=rs1.getString("username")%>~<%=rs1.getString("password")%>~<%=rs1.getString("email")%>~
<% }%>

No comments:

Post a Comment