Bookmark Topic Watch Topic
  • New Topic

Redirect from servlet to EXTERNAL URL using POST

 
sudharsan, varadharajan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello All,
I am running in to a problem regarding which I would really appreciate some assistance. I am
using Visual Age for Java 3.5 for my Java IDE.
My problem is as follows:
1.) Browser issues a HTTP POST to a servlet.
2.) within the servlet, I have to re-direct to
a COMPLETELY DIFFERENT URL which is outside of my servlet Engine itself, like as below.
RequestDispatcher.forward("http://www.cnn.com?param1=value1");
RequestDispatcher.forward SEEMS TO BE WORKING
ONLY with RELATIVE URI's . How do I re-direct my
POST with my ORIGINAL REQUEST PARAMETERS, TO A URL WHICH IS COMPLETELY OUTSIDE OF MY APPLICATION
FROM WITHIN MY SERVLET???.
I am currently trying to get it to work using
HttpUrlConnection object but I have not had success till now.
Thanks in Advance,
SudharsanRequestDispatcher.forward SEEMS TO BE WORKING ONLY WITH RELATIVE URI's
 
Joe Gilvary
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think your requirements contradict one another.
A relative URL would be relative to the current
URL. An external URL will require the scheme prefix
(http, ftp, etc). Once you start a URL string with
the scheme, you will not have a relative URL.
HTH,
Joe
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Report post to moderator
RequestDispatcher is tied to the local JVM and servlet engine so I don't think you can use it here.
How about this...
1. create a new HttpUrlConnection to that separate resource,
2. recreate the POST request and send it.
3. receive the response and relay the content through your servlet to the original request
Bill
 
sudharsan, varadharajan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello Joe,
I am sorry if I didn't make myself clear.
Yes! I am not dealing with a relative URI at all.
Iam dealing with a re-direct to an external URL
with protocol prefix "http", "host", "port" etc
for which I observed the RequestDispatcher cannot be used and so I was looking to see if others have found a working solution for this.
Thanks for replying back!.
Sudharsan.
 
sudharsan, varadharajan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello William Brogden,
Thanks for your reply.
I tried using HttpURLConnection as you said.
Now, there is a different problem.
The post parameter string originally before
using HttpURLConnection had characters like
%2B, %2F, %0A, %3D to signify the ascii values
for characters +, /, '\n', = respectively.
After doing the HttpURLConnection post in the
re-directed servlet, when I look at the request
parameter string I don't see %2B, %2F, %0A etc
but I see their decoded values of +, /, '\n',=
etc. I think, the HttpURLConnection is doing
some kind of URL Decoding behind the Scenes.
Is there any way to solve this Problem?.
Thanks,
Sudharsan.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Report post to moderator
It sounds like you need the URLEncoder class in the java.net package to re-encode those strings.
Bill
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
This post seems to have been re-started, so this is closed.

New one:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=7&t=007529
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!