How to forward the requestdispatcher to a remote URL

sathya picture sathya · Jan 22, 2013 · Viewed 30.4k times · Source

Am having a HTML page http://www.mywebapp.com/sample.html which is used from remote server. am passing the HTML URL as hidden form like this in the same HTML form,

<form action="/myservlet?userid=12345" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Submit">
<input type="hidden" name="url" value="http://www.mywebapp.com/sample.html"/>
</form>

In my servlet i got the hidden URL http://www.mywebapp.com/sample.html and stored it as String fieldValue = http://www.mywebapp.com/sample.html

Now When i try RequestDispatcher and forward the page to the hidden URL like this,

RequestDispatcher rd = req.getRequestDispatcher(fieldValue);
rd.forward(req, resp);

am getting ERROR 404.

Can anyone suggest me an idea to solve this.

EDITED

What i exactly want to do is, From a Remote Server a HTML page will request to my REST Web services. The response of the web service will be in JSON output. Now i want to send this JSON Response to the requested HTML form(i.e. to the Remote Server HTML page)

Can anyone suggest an idea to solve this. Your help will be appreciated.

Answer

JB Nizet picture JB Nizet · Jan 22, 2013

You can't forward a request to a URL which is external to your webapp. You probably want to send a redirect to this URL instead. See HttpServletResponse.sendRedirect().

See Difference between JSP forward and redirect