Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to insert value in a textfield using ajax and jsp

843838May 8 2007 — edited Nov 30 2007
Hi ,

I am new to ajax.My problem is that i cannot insert a value in to a textfield which is fetched from a database(MySql).

I have two jsp pages.Definition1.jsp and definition.jsp.
I am giving the code below.

Definition1.jsp
<html>

<%@ page language="java"%>

 <%@page contentType="text/html" %>


<script language="Javascript" type="text/javascript">


function createRequestObject() {
    var tmpXmlHttpObject;
    
    
    if (window.XMLHttpRequest) { 
        
        tmpXmlHttpObject = new XMLHttpRequest();
	
    } else if (window.ActiveXObject) { 
        
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return tmpXmlHttpObject;
}

//call the above function to create the XMLHttpRequest object
var http = createRequestObject();

function makeGetRequest(wordId) {
    //make a connection to the server ... specifying that you intend to make a GET request 
    //to the server. Specifiy the page name and the URL parameters to send
    http.open('get','definition.jsp?id='+wordId);
	
    //assign a handler for the response
    http.onreadystatechange = processResponse;
	
    //actually send the request to the server
    http.send(null);
}

function processResponse() {
    //check if the response has been received from the server
    if(http.readyState == 4){
	
        //read and assign the response from the server
		 var result2 = http.responseText;

        var result = http.responseXML.documentElement;
	
		
        //do additional parsing of the response, if needed
		
        //in this case simply assign the response to the contents of the <div> on the page. 
document.getElementById('description').innerHTML = result2;//this works correctly
		alert(result.getElementByTagName('p')[0].childNodes[0].nodeValue);//nothing happends here

		document.getElementById('name').value=result.getElementsByTagName('p')[0].childNodes[0].nodeValue;
		[u]//above code does not works and this is my problem[/b[/u]]
    }
}


</script>
<body>

<form>
 <input type="text" id="name" size=10 value=" "></input> 
</form>
<h1>Have you heard these terms before?</h1>

Ceraunophobia <a href="javascript:makeGetRequest(1)">More about Ceraunophobia</a><br>
Astraphobia <a href="javascript:makeGetRequest(2)">More about Astraphobia</a><br>


<div id="description">

</div>

</body>

</html>
Definition.jsp


<%@ page language="java"%><%@ page import="java.sql.*,java.io.*,java.util.*,javax.servlet.*"%>
<?xml version="1.0" encoding="UTF-8"?>
 
  <%
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("pragma","no-cache");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://itserver:3306/manzor?user=root&password=");
Statement st= con.createStatement();
Statement st1= con.createStatement();

ResultSet rs,rs1;
String id=request.getParameter("id");

rs=st.executeQuery("select * from user where USERTYPE='"+id+"'");

if(rs.next()){



  %>
  

   <p> <%=rs.getString(1)%></p>
 


<%

}

%>
please help
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2007
Added on May 8 2007
7 comments
650 views