Pass XML string from Javascript to JSP

D

dmckeon

I have a Javascript that is building an XML string and then invokes a
JSP. I need to pass the XML string from the Javascript to the JSP.
What's the best way to do that? I am new to this technology and really
trying to learn.

Thanks,
Dan
 
M

Martin Honnen

I have a Javascript that is building an XML string and then invokes a
JSP. I need to pass the XML string from the Javascript to the JSP.
What's the best way to do that?

It depends on how you want/can process the XML in the JSP but usually
doing a HTTP POST request with
Content-Type: application/xml
and the XML in the HTTP request body is a good idea:

var httpRequest = null;
if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest();
}
else if (typeof ActiveXObject != 'undefined') {
try {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {}
}
if (httpRequest != null) {
httpRequest.open('POST', 'whatever.jsp', true);
httpRequest.setRequestHeader('Content-Type', 'application/xml');
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
//deal with response here
}
};
httpRequest.send('<example>Kibology for all</example>');
}
 
D

dmckeon

Martin said:
It depends on how you want/can process the XML in the JSP but usually
doing a HTTP POST request with
Content-Type: application/xml
and the XML in the HTTP request body is a good idea:

var httpRequest = null;
if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest();
}
else if (typeof ActiveXObject != 'undefined') {
try {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {}
}
if (httpRequest != null) {
httpRequest.open('POST', 'whatever.jsp', true);
httpRequest.setRequestHeader('Content-Type', 'application/xml');
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
//deal with response here
}
};
httpRequest.send('<example>Kibology for all</example>');
}

Thanks so much for the info and please excuse my lack of experience
with this. Do you know what I would do in the JSP to get the
information that is set in the Javascript code?
 
M

Martin Honnen

Do you know what I would do in the JSP to get the
information that is set in the Javascript code?

You need to read in the body of the HTTP request (with an XML parser if
you want to process the XML). I don't do JSP so I can't give you code
details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top