Jose Sandoval Google
 Resume     Book     Software     Drawings     Home     Subscribe to RSS feed Search Web Search josesandoval.com

Running XMLHttpRequest with Java
Thursday, March 24, 2005

I had to get the XMLHttpRequest with Java demo running - It was running at one point, but my DSL provider keeps changing my dynamic IP address (Hence the name, dynamic IP address) - The demo now has a home of its own.

Running Pointless XMLHttpRequest with Java application.

When I decided to write my little app, I looked around for examples with actual code and didn't find any Java specific sites. So, for the benefit of curious eyes and code tinkerers, here is the code again - Hopefully you'll find the trivial example useful:
  1. The JavaScript
    This is where most of the work is done, via the XMLHttpRequest. This is JavaScript specific functionality and as per the time of this writing IE and FireFox were the only browsers supporting it - Opera doesn't, yet.

  2. MyGoogleSearchServlet.java
    This is the Servlet receiving the request - Nothing special about this class - It's your typical Java Servlet implementing doGet().

  3. MyGoogleSearch.java
    This is a helper class - I abstracted this functionality out of the Servlet so I can reuse it. I.e. A Swing Application - BTW, you'll need to get your own key from google, if you want to run it on your own machine.

  4. SearchResultBean.java
    This is your typical VO bean - I use it so that I don't couple the google API code with the Servlet.


7:27 PM | 19 comment(s) |

Comments:

It's ok with IE but doesn't work with Firefox.
By Anonymous MikeMarch, at 2:58 AM


http://www.dynip.com/

Mikemarch, I checked it with FireFox 1.0.1 and seems to be working for me - Perhaps I used a different version than yours.

Look nice, but the licence of google api don't permit it to be used in web application

http://www.google.com/apis/api_faq.html#gen6


You should be using the List interface as the return type from your doSearch method. And why aren't you using an iterator to iterate over the contents of the results list?:

for( Iterator iter = results.iterator(); iter.hasNext(); ) {
SearchResultBean result = (SearchResultBean)iter.next();
...
}
By Anonymous Anonymous, at 2:52 PM


Carfield Yim, I read through the licence agreement and the terms and conditions before writing the app and I don't think my little demo violates any of the terms.

The only thing I think you may be refering to is: "And you may not use the search results provided by the Google Web APIs service with an existing product or service that competes with products or services offered by Google." However, the demo is not competing with google, it is only using the API to demo a trivial app.

Also, they actually suggest that the apps to be developed are "totally up to you and your creative genius." I'll read it again, just in case - Thanks for the heads up.


"And why aren't you using an iterator."

Mostly for performance reasons.

Creating a brand new object (Iterator) to traverse to the list seems to be a waste of memory and CPU. Of course with this little demo it makes no difference, but, with thousands of concurrent users you may see the difference. Have you read Enterprise Java Performance? These guys have some reviews on Java Enterprise Performance books.

But, I agree with you, an Iterator would be more OO like and would be a more Javaesque solution. To each its own, I guess.

As per an ArrayList and not List, ArrayList was the first thing that came to mind - Alphabetically, that is - :)


Using iterator is faster at many cases.

I've tried the demo with Firefox 1.0 and 1.0.3 with no luck. I am behind a proxy, so I think that may have something to do with it.

However, it works in IE behind a proxy, so perhaps it is a bug with Firefox, not inheriting the proxy information when making the "remote" call.

Thoughts?


great example.. works great with firefox 1.0.2....

me gusto mucho tu ejemplo, de hecho el primero que encuentro para java y lo unico que te queria preguntar es que si sabes como puede hacer para que en lugar de puro texto que me retorne el search, pueda visualizar una imagen... ya sea desde una base de datos o desde un archivo en el servidor local.

gracias...
By Blogger jf, at 9:08 PM


Not in Firefox for me either (IE ok).

Wonder why the inconsistency; maybe Firefox settings?

I've looked at mine, but cant' see anything obvious.
By Anonymous Anonymous, at 11:56 PM


Firefox

Initially, it did not work with Firefox, but I re-ran the Firefox install and selected the 'developer tools' option in custom install; this installs the Dom Inspector, and now it works.
By Anonymous Anonymous, at 12:03 AM


Hello :-)
I'm using some special national characters and using escape function didn't worked. I think you should use encodeURI because it works with URL request parametters better. See http://xkr.us/articles/javascript/encode-compare/ if you want see differences in encoding with that functions.
By Anonymous JirkaB, at 11:08 AM


very good
By Anonymous Anonymous, at 4:11 AM


This is nice, how would you add the feature "more results" to this?
By Anonymous Anonymous, at 3:27 AM


Regarding the last question of "how to add more restuls to it": there are a few ways of dealing with this, for example, you could use the google API to continue searching; or you could hold more results in some server variable and just keep displaying information without searching; or you can sent a bunch of results via XML and just hold them into some JavaScript variable...

when i run this prg it will shows like this........
Server Application Error:
The server has encountered an error while loading an application during the processing of your request. Please refer to the event log for more detail information. Please contact the server administrator for assistance.
By Anonymous Anonymous, at 12:45 AM


If you are running it on my server, you probably found that google wasn't connecting.

I just tried it and it seems to be working.

If you are running on your server, you need your own google API key.


i buy hydrocodone at buy hydrocodone - can't find any cheaper
By Anonymous Anonymous, at 10:08 AM



This page is powered by Blogger. Isn't yours?

Guestbook
© Jose Sandoval 2004-2009 jose@josesandoval.com