How to forward the requestdispatcher to a remote U

2019-02-10 23:15发布

问题:

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.

回答1:

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



回答2:

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.



回答3:

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.