How to retrive xmlhttprequest in Servlet and how to return the value to xml

HI...I am New to Ajax... and now i have a problem how to retrive the xmlhttp request in servlet(Control file) and after processing the xmlhttp request and how to send the response to the xml...!Plz help..! thanks in advance...!
[248 byte] By [dhil_sa] at [2007-10-3 0:53:54]
# 1

> HI...

>

> I am New to Ajax...

Not an issue with a JSP/JSTL forum...

> and now i have a problem how to

> retrive the xmlhttp request in servlet(Control file)

You don't. The XMLHTTP object is a javascript object - it is client side and has nothing to do with Servlets.

On the servlet you work on the HttpServletRequest you get in the doPost or doGet method to get request parameters or headers. If that doesn't make sense to you then you need to buy a book on J2EE.

> and after processing the xmlhttp request and how to

> send the response to the xml...!

Write XML to the HttpServletResponse object.

>

> Plz help..! thanks in advance...!

stevejlukea at 2007-7-14 17:49:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

just call your servlet directly via the JavaScript URL, like:

URL = "/servlets/com.myProject.myServlet?field1=" + document.getElementById('field1').value;

then in your servlet, simple setup would be like:

public void doGet(HttpServletRequest req, HttpServletResponse res) {

try{

PrintWriter out = res.getWriter();

String field1 = req.getParameter("field1");

String result = field1;

// ***** do something with field1

out.print( result );

// or if you are sending xml response then hvae something like this

response.setContentType("text/xml");

response.setHeader("Cache-Control", "no-cache");

out.println("<?xml version="1.0" encoding="UTF-8" standalone="yes"?>");

out.println("<response><item>"+result"</item></response>");

}catch(Exception e){

logger.debug( e.getMessage() );

logger.debug( e.getStackTrace().toString() );

}finally{;}

}

then, back in your JavaScript (code/file):

function updatePage() {

if (request.readyState == 4)

if (request.status == 200)

{

alert("Server is done!");

var message = request.responseXML.documentElement;

var textvalue = message.getElementsByTagName('item')[0].firstChild.data;

document.getElementById("server").value= textvalue;

}

}

swapperpid0a at 2007-7-14 17:49:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...