• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Redirecting to an URL from servlet

 
Bhaskar Reddy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use complete URLs.
 
Bhaskar Reddy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the Redirect servlet example
See redirect to another site part.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sudhir, let me check using -
response.sendRedirect(response.encodeRedirectURL(request.getParameter("redirectToClientUrl")));
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat:
Bear Bibeault wrote:Use complete URLs.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic