• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Response for AJAX

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a servlet where i am forming this xml string and passing it back to the JSP.
The servlet code:



When I access this response from the JSP, it is adding the JSP code from the CreateShipment.jsp page to ehich i am forwarding the respose to in the response body along with the xml string.

I cant commit the response in the servlet coz i am writing to the writer. That gives me an IllegalStateException. What is it that i am missing here?

Thanks in advance.
harmeet

[BSouther: Added UBB CODE tags]
[ January 05, 2007: Message edited by: Ben Souther ]
 
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are missing, I think, is an understandable description of your problem.

When I access this response from the JSP, it is adding the JSP code from the CreateShipment.jsp page to ehich i am forwarding the respose to in the response body along with the xml string.

First of all I don't understand what "access... from the JSP" means. Is the JSP code forwarding, or redirecting, to this servlet? Or does it contain Java code that does an HTTP connection to this servlet? Or does it generate HTML which, in the browser, uses AJAX to request this servlet?

I can see that you're forwarding to CreateShipment.jsp (although I don't think you should be doing that if you set the response status to SC_NO_CONTENT). So it's natural to expect that CreateShipment.jsp would do what it's meant to do and write HTML to the response. Did you not expect that? Your question sort of seems to suggest that you didn't, but I can't quite unravel that pile of words.
 
harmeet bawa
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the prompt response. This is an AJAX transcation. The JSP code excerpt is here:
function getTheRequest() {
if(window.XMLHttpRequest) {
return new XMLHttpRequest();
} else {
isIE = true;
return new ActiveXObject("Microsoft.XMLHTTP");
}
}


function doComplete() {
if(idField.value == "") {
clearTable();
} else {
searchType = document.getElementById("searchBy");
completeTable.style.visibility = "visible";

var url = "AutoComplete?action=complete&id=" + escape(idField.value) + "&searchBy=" + escape(searchType.value);
req = getTheRequest();
req.open("GET", url, true);
req.onreadystatechange = processRequest;
req.send(null);
}
}


function processRequest() {
if(req.readyState == 4) {
if(req.status == 200) {
parseMessages(req.responseXML);
}
}
}


function parseMessages(responseXML) {

clearTable();
var search = responseXML.getElementsByTagName("search")[0];
if(search.childNodes.length > 0) {
} else {
clearTable();
}

for(loop = 0; loop < search.childNodes.length; loop++) {
var customer = search.childNodes[loop];
var companyName = customer.getElementsByTagName("companyName")[0];
appendCompany(unescape(companyName.childNodes[0].nodeValue));
}
}


function appendCompany(companyName) {
var row;
row = document.createElement("option");
row.className = "popupRow";
row.setAttribute("value", companyName);
row.appendChild(document.createTextNode(companyName));
completeTable.appendChild(row);
}


function clearTable() {
if(completeTable) {
completeTable.style.visible = false;
for(loop = completeTable.childNodes.length - 1; loop >= 0; loop--) {
completeTable.removeChild(completeTable.childNodes[loop]);
}
}
}

The HTTP response adds the xml string and then I forward back to the JSP. Here it is reading the xml string in addition to the HTML from the JSP being added to the response. Can I know a way to prevent this? I hope I made it a bit more clearer this time.
 
Paul Clapham
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The HTTP response adds the xml string and then I forward back to the JSP.

If you want the servlet to return an XML string then I don't understand why you have it forward to a JSP, which is just going to add more data to that XML. It sounds like you don't want that. If so then don't do it.

Oh, wait a minute, now I see your problem. You see the JSP as a fuzzy blob that does a bunch of things. Here's how you should think of it:

On the server, the JSP is used to generate HTML (including some Javascript in this case). That HTML is sent to the browser, which displays it to a person sitting there. At this point it isn't "the JSP" any more, it's some HTML in the browser. The JSP is finished.

Then the user does something that causes an AJAX request to be sent to the server. It's supposed to return an XML document to the browser, and the browser (NOT the JSP, remember, that finished long ago) is going to do something with that XML.

So, your servlet should generate the XML and send it as a response to the request that the browser sent. That's all. There's no reason to forward to a JSP... unless you want that JSP's output to be part of that response. And you don't.
 
harmeet bawa
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you Paul.
Could you please tell me the way to go here. I got what you said and that is what I exactly want.
In the servlet I have formed the response. Now I leave it here. But in the JSP what I am doing is, I need to read the HTTP Response from the servllet and then I parse the XML and display all the relevant info on the JSP. How exactly do I do this?

Thanks again,
Harmeet
 
Paul Clapham
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by harmeet bawa:
But in the JSP what I am doing is, I need to read the HTTP Response from the servllet and then I parse the XML and display all the relevant info on the JSP. How exactly do I do this?

So, assuming that you read what I wrote, this means that on the server you want to do something that takes the result of a servlet and does something with it. This is before the user gets to see anything, right? And this is nothing to do with the original question, right?
 
harmeet bawa
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to take the result of the servlet, parse the XML response from the servlet and display the values parsed from the XML response on the JSP
 
Paul Clapham
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what it means to you when you say you want to "display something on a JSP". A JSP is a program that generates HTML for a browser. Displaying something on that isn't a concept that makes any sense to me.

Perhaps you're trying to ask how to handle the XML that the servlet returns to that HTML and Javascript you have in the browser. If that's the case then let me remind you again that that isn't the JSP, and it doesn't help to keep asking questions as if it were.
 
harmeet bawa
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Perhaps you're trying to ask how to handle the XML that the servlet returns to that HTML and Javascript you have in the browser"


Ok I think thats what it is. what am I doing wrong here then?
 
Paul Clapham
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. So you have that Javascript that you posted. It makes an AJAX request to a servlet that returns some XML (after you change it so it doesn't forward to the unnecessary JSP). Now your question is "What should my Javascript be doing with that XML to make some changes to the HTML", isn't it? You haven't said how you want to change the HTML or what's in the XML or anything, so it's pretty hard to answer that. But that question isn't anything to do with JSPs (even if you happen to use JSP to generate the HTML), it's just a question about Javascript and AJAX.

So perhaps it would be better for you at this point to formulate a new question in terms of HTML and AJAX, if that's what it is, and ask it in the HTML/Javascript forum.
 
harmeet bawa
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Servlets response I have some XML like:
<customer><customerName>the name</customerName></customer>

THis comes back to the Javascript and it generates a select box with the customerNames returned back.
 
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic