Send send request in java using response.sendRedirect method

I want to send a mail request in java. I saw examples for a mail request using the Http Client. But I want to use the sendRedirect method.

For example, https://processthis.com/process?name=xyz&phone=9898989898

I want to use a mail request to send these parameters. This way, these options will not be visible to anyone, and at the same time I need to redirect my url to that url, like

response.sendRedirect("https://processthis.com/process"); 
+7
source share
4 answers

According to RFC2616 with HTTP / 1.1, you can send a 307 response code that will force the user-agent repeat the POST request to the specified host. In your case, just do

 response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); response.setHeader("Location", url); 

the answer is your HttpServletResponse object.

Hope this helps.

+6
source

When a browser receives an HTTP redirect code, it will always execute GET or HEAD for a given URL by standard. This is why data must be sent along the lines of the query. If you want to simulate POST redirection, you can send your client a form with the necessary information, and in the event of a page load event, you automatically submit the form using Javascript (usually used to communicate between different servers using the SAML protocol). Here is an example:

 <form name="myRedirectForm" action="https://processthis.com/process" method="post"> <input name="name" type="hidden" value="xyz" /> <input name="phone" type="hidden" value="9898989898" /> <noscript> <input type="submit" value="Click here to continue" /> </noscript> </form> <script type="text/javascript"> $(document).ready(function() { document.myRedirectForm.submit(); }); </script> 

Side note: If you already have information on your server, why are you sending a redirect instead of doing this? Perhaps you want to implement the POST / REDIRECT / GET pattern?

+3
source

I do not think that's possible. Why don't you use RequestDispatcher. This will work for you. Just set the parameters in the request

 request.setAttribute("message", "Hello test"); RequestDispatcher dispatcher = servletContext().getRequestDispatcher(url); 

OR - The HTTP specification indicates that all redirects must be in the form of GET (or HEAD). You might consider encrypting query string parameters if there is a security problem.

OR is another option, set the parameters in the session in the servlet, if you have a session. Then get it from the session after redirecting to the desired page.

+1
source

I am also looking for the same. I want to redirect to a payment page with a post request. I implemented using a JSP page, but I want to redirect from Java code. Can you post a Java example to help me implement in my project.

-1
source

All Articles