File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes HttpURLConnection and manipulating URL/link addresses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpURLConnection and manipulating URL/link addresses" Watch "HttpURLConnection and manipulating URL/link addresses" New topic

HttpURLConnection and manipulating URL/link addresses

Chris Garrison
Ranch Hand

Joined: Jun 13, 2002
Posts: 44
I'm trying to write a servlet which takes the output of another servlet and outputs it without forwarding control to it. I'm using the URL and HttpURLConnection classes. This portion ( seems to be running smoothly.
The problem is: I'm trying to "rewrite" the URLs in the output to point to the servlet I'm writing. I'm simply using the String.replaceAll()
You got to http://hostA/servlet. This servlet (via the URLConnection class etc.) really points to htp://hostB/servlet and captures it's html output.
But - before outputting the stuff from hostB/servlet, I want to change the links addresses to point to hostA/servlet (which will eventaully rewrite them to point to hostB/servlet and go get the html output... and so on...).
http://hostA/servlet has a login button (http://hostA/servlet/login - user clicks
hostA/servlet does a replaceAll("http://hostA", http://hostB");
then I do the URL and HttpConnection to get the output of http://hostB/servlet/login.
We're cool up to this point, but now is where everything goes haywire.
Once I get the html output back for URLConnection, I want to replaceAll("http://hostB", http://hostA); so that the next click will come back to hostA and so on and so on.
First of all, it goes crazy and get stuck in some sort of loop. I also get an error on the console "ERROR threads.ThreadPool - All threads are busy waiting. Please increase max threads or check the servlet status. ???
So - I may be doing this totally wrong - There may be an easier way to do this, but I don't know about it. I'm new to this, so I'm probably doing something foolish.
My best guess is that everytime the method encounters an "href" or something similar in the replaceAll method - everything goes nuts - the processing seems to start over at the top. When I remove the final "replaceAll" method, it works fine, but of course then my links point directly to "hostB/servlet" instead of "hostA/servlet" which is what I want.
Your assistant is appreciated.

May the force be with you, always.
Eelco den Heijer
Ranch Hand

Joined: Jan 17, 2002
Posts: 61
So to make things clear: you have a reason not to use RequestDispatcher.include( ...) ?

== <br />Rgrds,<br />Eelco<br /> <br />SCJP, SCJD, SCBCD, SCWD, SCEA
I agree. Here's the link:
subject: HttpURLConnection and manipulating URL/link addresses
It's not a secret anymore!