Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Tomcat directly using Ajax: request for opinions and suggestions

 
Dave Anderson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally that won't work with Ajax for security reasons. You'll need to use a server-side proxy.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Anderson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Great to know it worked for you!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic