wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to capture a  respnse object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to capture a  respnse object" Watch "How to capture a  respnse object" New topic
Author

How to capture a respnse object

vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
I am using the google search feature, which returns the query results in an XML format. Now I want to parse it and generate custom HTML.Only one problem though.

I am doing a response.sendRedirect() and I get the google search results. But I want to capture the results page as an XML document in order to parse it. How can it be done?

I am not very familiar with XML,so this might sound like a stupid question.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

You can do this with Ajax (but you'll need a server-side proxy to escape the dreaded "Ajax security sandbox"). It will return the XML document which you can then parse with JavaScript DOM functions.

If you'd rather do it on the server-side, I'd suggest "hitting" the service with URL.getConnection() in your servlet and parsing the returned response. HttpUnit is also a great tool for this.

What you probably don't want to do -- simply because it's a lot more work -- is to hit the service directly from the browser and try to intercept the response using filters and response wrappers. But that is a possible way of doing it.
[ May 11, 2006: Message edited by: Bear Bibeault ]

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

Joined: Jan 10, 2002
Posts: 60810
    
  65

P.S. <shameless-plug>If you are interested in the Ajax solution and you're a patient man, the upcoming Manning book "Ajax in Practice" will have a whole chapter devoted to using open APIs (google, Yahoo!, Flickr, et al) from Ajax.</shameless-plug>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Other solutions could be using XSLT (would not be my choice), or perhaps even a JSP page using the JSTL "x" tags.
[ May 11, 2006: Message edited by: Bear Bibeault ]
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Thanks for suggestions.
This article http://java.sun.com/products/servlet/Filters.html says filters can be useful too.And I think its easier for me to use this than AJAX as I am a complete stranger to AJAX.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Yes, as I mentioned in my first reply, filters are a possibility for doing this on the server side, but I think it would be a lot easier to pursue the URL connection route. But either means should work for you.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Another reason to do the work on the server side is so you can cache the results, avoid needless extra round-trips to Google and speed page generation.
Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to capture a respnse object
 
Similar Threads
reading xml file in Android
How does search engines work?
parsing XML file in java
how to extract search engine results
to list all links