aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes AJAX and Servlet question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX and Servlet question" Watch "AJAX and Servlet question" New topic
Author

AJAX and Servlet question

Amit Da
Greenhorn

Joined: Mar 07, 2003
Posts: 16
I have a servlet A that I would call on a button click using AJAX. It checks certain things posted and if invalid comes back with error xml, which gets displayed on the same page.
In Valid case, I want to forward request and response objects to another servlet B, that would gather data from database and render the page using the response object.
This case is not working well for me. Here the callback JS function would not have anything to do in the page.
request.responseXML is null but request.responseText has the entire html document. I am wondering how in the callback JS I could take this document and replace the one in current window.document
Please help. Any ideas about how to get this?
I would really dislike to have a response xml from servlet A in callback JS, that we would need to check and make another client to server (to servlet B) call to show second page.
Any ideas?
Thanks in advance,
[ August 08, 2007: Message edited by: Amit Da ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"amida",
Please check your private messages.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Please use real words when posting to the forums. Abbreviations such as "qn" in place of "question" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

I am wondering how in the callback JS I could take this document and replace the one in current window.document


it doesn;t make sense to do it the way that you are trying to do this.

When, on the server, you determine that a new page is approriate, you don't want to forward to that page. Rather, return information as the response of the Ajax request that will cause your client-side callback to redirect to the new page.
Amit Da
Greenhorn

Joined: Mar 07, 2003
Posts: 16
Two doubts:
1. I am on server side (in servlet, ofcourse) and able to determine if to give user an "Invalid" message or if valid using request dispatcher forward to the next page that is rendered using servlet B. Why would I need to do the extra roundtrip to first return a result to browser JavaScript method that would then send another request to servlet B?
This means pushing business logic from serverside to client, which I don't prefer. Are you saying I cannot do what I wish to do?
I have seen AJAX application that runs in just one page fully. I am sure they would be getting pieces of data and just changing page contents. I wonder if they are creating entire html page and replacing document or always just create divs and manipulate the inner divs inside a template page.

2. If I do what you suggest I should do there will be multiple such request every time I click on any navigation button, whose response will come back to browser and then send another request to servlet for rendering pages. Looks like a lot of overhead.
Let's assume for a moment I still would do it. How would I redirect to another active resource from the callback javascript?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Amit Da:
Two doubts:
1. I am on server side (in servlet, ofcourse) and able to determine if to give user an "Invalid" message or if valid using request dispatcher forward to the next page that is rendered using servlet B. Why would I need to do the extra roundtrip to first return a result to browser JavaScript method that would then send another request to servlet B?
This means pushing business logic from serverside to client, which I don't prefer. Are you saying I cannot do what I wish to do?
I have seen AJAX application that runs in just one page fully. I am sure they would be getting pieces of data and just changing page contents. I wonder if they are creating entire html page and replacing document or always just create divs and manipulate the inner divs inside a template page.


To the browser, an AJAX request is not the same as a regular request.
The screen is not redrawn when the request response cycle has completed.
A function is called and the response is passed to it.
How, from the server side, can you instruct the browser to redraw the screen with your error page under these conditions?


Originally posted by Amit Da:

2. If I do what you suggest I should do there will be multiple such request every time I click on any navigation button, whose response will come back to browser and then send another request to servlet for rendering pages. Looks like a lot of overhead.
Let's assume for a moment I still would do it. How would I redirect to another active resource from the callback javascript?


Amit Da
Greenhorn

Joined: Mar 07, 2003
Posts: 16
To clarify myself,
1. error case is no problem. In there the error xml comes back and using normal AJAX cycle I can change the same page showing error message. Issue is with Valid case because after checking in servlet A, control goes to servlet B and that is where the response is generated
I just need to know a way to get that response html in callback javascript and change the current window.document

2. window.location is an inherent GET htmmp method call to an active/passive resource. I must POST the contents (because I want to send all params as part of body of http message and NOT header (in short, over URL)) so this approach doesn't seem workable. Does document.location work similar to window.location?

Thanks anyway for responses
[ August 08, 2007: Message edited by: Amit Da ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Amit Da:
To clarify myself,
2. window.location is an inherent GET htmmp method call to an active/passive resource. I must POST the contents (because I want to send all params as part of body of http message and NOT header (in short, over URL)) so this approach doesn't seem workable


If you need to make a post request, you can call the submit method of any form in your page (existing or dynamically created).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AJAX and Servlet question