How to forward the requestdispatcher to a remote URL
Asked Answered
G

3

6

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.

Graecize answered 22/1, 2013 at 13:22 Comment(2)
https://mcmap.net/q/803876/-requestdispatcher-for-remote-serverDomingo
While @fanaj link doesnt contain a description - I highly recommend checking it out as it describes how you would need to use a 307 redirect so "the client will reapply the same POST query parameters on the new URL" the other redirects do not do this. I was researching the same problemGiveaway
H
9

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

Headward answered 22/1, 2013 at 13:26 Comment(5)
if i use HttpServletResponse.sendRedirect() am getting 302 status code that should not happen. Can you suggest me how to response to remote server which is a HTML page. My response contains JSON outputGraecize
how to response to remote server which is a HTML page: I don't understand what that means. What do you want to achieve.Headward
A remote HTML form is sending data to my REST web services. Am giving the response as JSON output to the requested remote HTML form. This is what am trying hope you got it. Now my problem is i want to send the JSON output to the requested remote HTML formGraecize
@Graecize to be honest this sounds like a bit of a mess - I haven't tried it but you'd have to encode the JSON response as a parameter on the URL you'll be redirecting to, which I just can't see working (the escaping will be a nightmare). The page calling the RESTful service should do so using JavaScript processing the JSON response from that service as need be.Bombazine
Please take a notes, a sendRedirect method process request and this request is visible in browser as a new request.Deputize
L
9

If you absolutely need to forward the request as opposed to redirect (for instance, if the remote URL is only accessible to the server and not the user) it is possible to do your own forwarding. In your servlet, you can make a request to the remote URL and write the InputStream from that request to the OutputStream in your servlet. You would obviously want to look for and handle any errors with the request and make sure streams are closed properly, though. You would also need to manually forward any parameters from the request to the new one.

The basic approach would be:

URL url = new URL("http://www.externalsite.com/sample.html");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);

String postParams = "foo="+req.getParameter("foo");

DataOutputStream paramsWriter = new DataOutputStream(con.getOutputStream());
paramsWriter.writeBytes(postParams);
paramsWriter.flush();
paramsWriter.close();

InputStream remoteResponse = conn.getInputStream();
OutputStream localResponder = resp.getOutputStream();
int c;
while((c = remoteResponse.read()) != -1)
    localResponder.write(c);
remoteResponse.close();
localResponder.close();

conn.disconnect();

This obviously doesn't handle the multipart request in your example, but it gives you a basic idea on how to achieve what you want. I'd recommend using Apache HTTP Components to do the request instead of HttpURLConnection since it will make the multipart request with the file easier to achieve (I'd imagine you'd have to manually create the multipart/form-data body with HttpURLConnection). An example of making the request can be found at How can I make a multipart/form-data POST request using Java?. An InputStream can be obtained from the HttpEntity by calling getContent() (which would be the equivalent to conn.getInputStream() in the example).

Writing the InputStream to the OutputStream can also be achieved more easily with Apache Commons IO IOUtils.copy() method.

EDIT: It may be possible to use req.getInputStream() to get the raw request body and write that to paramsWriter.writeBytes() but I have not tried this so there is no guarantee it would work. I'm not sure exactly what req.getInputStream() contains for a post request.

Louden answered 11/3, 2016 at 21:33 Comment(0)
B
2

You cant forward to a different server.

You can use the resp.sendRedirect(url)

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html#sendRedirect%28java.lang.String%29

method instead which will return a 302 redirect to the specified URL.

Benelux answered 22/1, 2013 at 13:25 Comment(2)
yes i can understand that it rises 302 that's why trying an alternative way. Can you suggest me how to response to remote server which is a HTML page. My response contains JSON outputGraecize
A remote HTML form is sending data to my REST web services. Am giving the response as JSON output to the requested remote HTML form. This is what am trying hope you got it. Now my problem is i want to send the JSON output to the requested remote HTML formGraecize

© 2022 - 2024 — McMap. All rights reserved.