How to forward the requestdispatcher to a remote URL

31,497

Solution 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

Solution 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.

Solution 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.

Share:
31,497
sathya
Author by

sathya

Updated on March 12, 2020

Comments

  • sathya
    sathya over 4 years

    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.

  • sathya
    sathya over 11 years
    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 output
  • sathya
    sathya over 11 years
    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 output
  • JB Nizet
    JB Nizet over 11 years
    how to response to remote server which is a HTML page: I don't understand what that means. What do you want to achieve.
  • sathya
    sathya over 11 years
    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 form
  • sathya
    sathya over 11 years
    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 form
  • Nick Holt
    Nick Holt over 11 years
    @sathya 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.
  • sathya
    sathya over 11 years
    @NickHolt Thanks a lot for your suggestions Nick.
  • Yohanim
    Yohanim over 6 years
    Please take a notes, a sendRedirect method process request and this request is visible in browser as a new request.