It's not a secret anymore!*
The moose likes Servlets and the fly likes Calling Tomcat directly using Ajax: request for opinions and suggestions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling Tomcat directly using Ajax: request for opinions and suggestions" Watch "Calling Tomcat directly using Ajax: request for opinions and suggestions" New topic
Author

Calling Tomcat directly using Ajax: request for opinions and suggestions

Dave Anderson
Greenhorn

Joined: Nov 09, 2009
Posts: 29
I have a Javascript application where I call the Tomcat servlet using port 8080 directly for all communications with the server. Here is the essence of the URL I use: "http://123.456.789:8080/Servletname/Servletname"

I am also using what I understand to be Ajax to do the communications (my javascript code follows):



Using Microsoft Internet Explorer, calls succeed (i.e., xmlhttp.status == 200), although a warning comes up beforehand on the first message sent:
"This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?"

QUESTION 1: am I doing something basically dumb here (especially with respect to Ajax and calling the servlet directly)?

Using all other browsers such as Chrome I get a response status (xmlhttp.status) of zero (0), and fall flat on my face.

QUESTION 2: I have a feeling that I am doing something basically wrong in my approach to Ajax and calling port 8080 directly, since the non-IE browsers will not issue a success status (i.e., xmlhttp.status == 200).

The answer to this message is important at a fundamental level to me. I therefore request your help and opinions.

Thanks!

Node (David)


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Firstly, please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

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

Joined: Jan 10, 2002
Posts: 60752
    
  65

Dave Anderson wrote:QUESTION 1: am I doing something basically dumb here (especially with respect to Ajax and calling the servlet directly)?

Calling a servlet directly is not an issue. But why are you using a full absolute URl rather than a server-relative URL? Is the servelt not in the same web app as the page?

QUESTION 2: I have a feeling that I am doing something basically wrong in my approach to Ajax and calling port 8080 directly, since the non-IE browsers will not issue a success status (i.e., xmlhttp.status == 200).

As I said, there's nothing wrong with your request going to a servlet. The problem is your URL addressing, I believe.

Dave Anderson
Greenhorn

Joined: Nov 09, 2009
Posts: 29
The servlets can be hosted on many different servers, and their names are resolved to the actual IP address.

Is my Ajax ok?

David
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Generally that won't work with Ajax for security reasons. You'll need to use a server-side proxy.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Here an article which would tells you why cross domain Ajax queries don't work directly and a few ways in which you can get them to work.

http://www.xml.com/pub/a/2005/11/09/fixing-ajax-xmlhttprequest-considered-harmful.html

It has a small example on how you can set up application proxies too.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Dave Anderson
Greenhorn

Joined: Nov 09, 2009
Posts: 29
Thanks for this tip Sam. I looked through the 3 options in the article you suggested (http://www.xml.com/pub/a/2005/11/09/fixing-ajax-xm...equest-considered-harmful.html), and spent some time looking at the Javascript / cURL approach. This didn't work out well, at least for me (although I'm keeping cURL on my radar for the future).

I did solve my problem, however. Here is the essence: continue to use the XMLHttpRequest object in my main web page, but instead of calling the resolved IP address, call instead a PHP script on the same server with the URL I want to call (i.e., url = /php/proxy.php). Now this PHP script uses a niffty library that makes the call, gets the response, and returns the response to the main web page:



This works very well indeed (can be improved on too from what I'm reading).

Thank you Sam for getting me moving on this.

David
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597


Great to know it worked for you!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling Tomcat directly using Ajax: request for opinions and suggestions
 
Similar Threads
Return Ajax response to JSP
Notify Users before session expires
Loading xml in Javascript fails
JSF1.2 Core+AJAX+Portlets getting problematic while constructing the url
ajax not working with jsp