Two Laptop Bag*
The moose likes Servlets and the fly likes Redirecting to an URL from servlet 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 "Redirecting to an URL from servlet" Watch "Redirecting to an URL from servlet" New topic
Author

Redirecting to an URL from servlet

Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
What I am trying to do :
1. I have created a small application (servlet, HTML & log4j.properties) that is deployed in a web server. There is a switch (say wireless router) that intercepts clients (who are outside network) trying to access wireless network and routes the request to the HTML in the application. The HTML contains some form info that user needs to fill and submit. When he submits this form, it sends the request to the servlet (via GET), the servlet logs the required info, authenticates user and needs to redirect the user back to the site that he initially tried to access.
2. I got everything working but the redirect part. What I am trying to do for redirection is this -


So, when the client tries to go to "www.google.com", the switch redirects to the HTML, user enters the form data and submits the page. This is then sent to the servlet which logs required info, but is not able to redirect to the site 'www.google.com'. The URL at the client end is changed to http://216.216.216.216/www.google.com (where 216.216.216.216 is the IP address of the web server that hosts this application.)

Seeking experts help in redirecting to an URL (which is outside the application) from servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Use complete URLs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
By "Complete URLs", did you mean I need to use "http://www.google.com" - instead of "www.google.com"? If so, I was just curious as to how to form the "Complete URL", because client can try any combination - like "google.com" or "www.google.com" or "http://www.google.com" or some other site that may not even have 'www' in the host name.

Just to remind, I am using GET method to retrieve the "redirectToClientUrl" variable from the html.
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Look at the Redirect servlet example
See redirect to another site part.


[Servlet tutorial] [Servlet 3.0 Cook Book]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Bhaskar Reddy wrote:By "Complete URLs", did you mean I need to use "http://www.google.com" - instead of "www.google.com"?
If you provide a partial URL (e.g. one without the http:// protocol in front) then the server will expand it to a complete URL. You have already seen one possible result of that process and you naturally don't like what it does. So therefore it's up to you to do it the way you want it done.
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
Thank you for your insight!

Well, coming to think of what I am getting after the redirect -

when I do
it redirects the client URL to http://xxx.xxx.xxx.xxx/<<actual_client_url>>.

what I want to do is to redirect the client URL to <<actual_client_url>>. (I am guessing the 'http://' will not be needed as the URL will be submitted by the browser to look up up for the DNS entry.) Is it possible to do so?
Bhaskar Reddy
Ranch Hand

Joined: Dec 17, 2002
Posts: 52
Thanks Sudhir, let me check using -
response.sendRedirect(response.encodeRedirectURL(request.getParameter("redirectToClientUrl")));
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

I repeat:
Bear Bibeault wrote:Use complete URLs.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Bhaskar Reddy wrote:Thank you for your insight!

You are welcome. These things are often quite simple, but people tend to make them complicated by imagining they work in a way that those people want them to work as opposed to the way they actually work.
Bhaskar Reddy wrote:what I want to do is to redirect the client URL to <<actual_client_url>>. (I am guessing the 'http://' will not be needed as the URL will be submitted by the browser to look up up for the DNS entry.) Is it possible to do so?

If you provide a relative URL, then the server will assume it's a URL in its own domain. You have already done the experiments which show that to be the case. Believe what you are seeing. If you provide an absolute URL (complete with http://) then the server will take it as given. You have already tested this case.

Your requirements apparently allow the user to enter part of a URL, and require some guesswork to make it into a complete URL. Your experiments have shown that your server won't do this guesswork for you. So therefore you have to do it. This isn't difficult to see once you stop assuming that things work the way you want them to work. Just look at the way they actually work -- you've already done this -- and go from there.
 
 
subject: Redirecting to an URL from servlet
 
Similar Threads
Why Swing client at all?
servlet mapping clarification
Comparision between redirect and forward
preventing direct access to html files
Redirecting to an outside URL from HandlerInterceptor