Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward the requestdispatcher to a remote URL

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.

like image 285
sathya Avatar asked Jan 22 '13 13:01

sathya


People also ask

What is difference between redirect and RequestDispatcher?

SendRedirect() will search the content between the servers. it is slow because it has to intimate the browser by sending the URL of the content. then browser will create a new request for the content within the same server or in another one. RquestDispatcher is for searching the content within the server i think.

What are the methods of the RequestDispatcher interface?

The RequestDispatcher interface provides the option of dispatching the client's request to another web resource, which could be an HTML page, another servlet, JSP etc. It provides the following two methods: public void forward(ServletRequest request, ServletResponse response)throws ServletException, java. io.

Why use RequestDispatcher to forward a request to another resource instead of SendRedirect?

We can use request dispatcher only when the other servlet to which the request is being forwarded lies in the same application. On the other hand Send Redirect can be used in both the cases if the two servlets resides in a same application or in different applications.


Video Answer


1 Answers

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

like image 197
JB Nizet Avatar answered Oct 08 '22 20:10

JB Nizet