0

After receiving a post request in servlet, I have to do a redirection to another server and at the same time I have to pass an xml file in the redirection request.

For example, I have to do a redirection from my servlet to "http://www.abc.com" and pass an xml file in the request.

I tried the following, but it didn't work.

  1. response.sendRedirect - it is creating only get requests, so not able to send xml
  2. HttpClient or URLConnection - it is creating a new request, not doing the actual redirection
  3. Intermediate jsp - forwarded the request to a intermediate jsp and did a submit from jsp.

It is sending the xml in parameter and not in InputStream

Please let me know how to achieve this.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331

1 Answers1

1

A redirect (either HTTP or HTML) can only operate on a URL, rather than a form submission, which offers built in support for uploading files via the "multipart/form-data" form encoding, and so you would have to encode your file within the URL itself, which would severely limit you given that the lowest-common-denominator (Internet Explorer) URL maximum length is around 2000 characters. If your files are smaller than that, then you could encode your file as a URL query parameter. Otherwise, it's probably not possible, but I will stand corrected if others know of a way to achieve it.

Laird
  • 148
  • 6
  • Actually, I figured out a way to do this overnight: return a page containing a form with a hidden input containing the file's data, and with the form's "action" set to the redirect URL, and then call the form's submit() method using Javascript on page load. This assumes your user has Javascript enabled, but that's a pretty safe assumption these days. – Laird Dec 23 '13 at 00:47
  • Thanks Laird for responding. I tried this as mentioned in my post. I cannot change the receiver's code. The receiver's code is expecting the xml in request body as application/xml. Receiver is reading it from request.getInputStream(). With the approach you have mentioned it is sending the xml as a request parameter. I want to send it in request body as application/xml. – user3126823 Dec 23 '13 at 06:09
  • Probably, then, your best option is #3, but instead of doing a jsp submit, find a way to construct and send the custom request that you want to send. I'm not familiar with the jsp API, so I can't suggest how you would do this using specific functions, but in general, the worst case scenario is you have to use a socket connection function to connect to http://www.abc.com, and then write to the socket your custom request, which would include the XML file data as required by the recipient, then read from the socket to check for success, then close it. – Laird Dec 23 '13 at 10:45
  • Or if you wanted to try it from the client side, maybe this question could help: http://stackoverflow.com/questions/18335281/how-can-i-make-a-custom-post-request-on-client-side – Laird Dec 23 '13 at 10:57
  • I did a bit of research on how to do this with jsp in case you do want to implement a server-side solution - it seems your best bet is the [Apache HttpClient API](http://hc.apache.org/httpcomponents-client-ga/). Here's another question that might help: http://stackoverflow.com/questions/18188041/write-in-body-request-with-httpclient – Laird Dec 23 '13 at 11:30
  • Both of these solutions are making ajax call. It is not doing a browser redirect. I want to do a browser redirect and post xml in request body – user3126823 Dec 23 '13 at 13:39
  • Try the AJAX option I described above, and replace the HTML contents of the page with the returned HTML using Javascript, as per the question [Replacing Entire Page Including Head Using Javascript](http://stackoverflow.com/questions/4292603/replacing-entire-page-including-head-using-javascript), and then set the URL in the address bar to the redirected-to page, as per the question [Is there a way to change the browser's address bar without refreshing the page?](http://stackoverflow.com/questions/352343/is-there-a-way-to-change-the-browsers-address-bar-without-refreshing-the-page). – Laird Dec 24 '13 at 08:52