how to insert value in a textfield using ajax and jsp

[nobr]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();

}elseif (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;//[b]this works correctly[/b]

alert(result.getElementByTagName('p')[0].childNodes[0].nodeValue);//nothing happends here

document.getElementById('name').value=result.getElementsByTagName('p')[0].childNodes[0].nodeValue;

[u][b]//above code does not works and this is my problem[/b][/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()){

%>

<%=rs.getString(1)%>

<%

}

%>[code]

[/code]

please help[/nobr]

[5408 byte] By [manu_ama] at [2007-11-27 3:40:34]
# 1

> My problem is that i cannot insert a value in to a textfield which is fetched from

> a database(MySql).

OK. The main question is: what is the cause? What did you have debugged so far? JS alerts, JSP/Java sysouts, JS debugger and/or IDE debuggers are useful. Run the code step by step and find out what is causing the value being empty.

BalusCa at 2007-7-12 8:44:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Sir , I got the error"internel server error" when checked with the http.status==500.what could be the reason.
manu_ama at 2007-7-12 8:44:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Look in the appserver logs. You should find the stacktraces in there.
BalusCa at 2007-7-12 8:44:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Sir,

Thank u for your kind reply.I got solved my problem but partly.

Now iam getting value in the textfield.But only one value.

I used response.getWriter().write(<message>data</message>)

and parsedthe message in the client using dom.But the problem is that i cannot extract more than one value from the responseXML object.Nothing displays but no error messages when i checked with alerts.

for eg:

[b]response.getWriter().write(<message>data</message>)[/b]

[b]response.getWriter().write(<message1>data1</message1>)[/b]

when i used above code (with two response) in the server program nothing displays as result.But will work with only one response

manu_ama at 2007-7-12 8:44:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Get and write once. I recommend to preprocess the stream in a StringBuilder or StringWriter and then pass it using ByteArrayInputStream through the response.
BalusCa at 2007-7-12 8:44:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Sir,

At last i got the problem solved. It was the mistake in the xml format i used to send the result back. When i used the following in the serverscript i got the correct result.

response.getWriter().write("[b]<m>[/b]<message>"+r+"</message>");

response.getWriter().write("<message1>"+r1+"</message1>[b]</m>"[/b]);

I used the <m> tag which included all the other tags.

Thanks for ur kind attention and reply

manu_ama at 2007-7-12 8:44:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...