Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can sendRedirect() act as a post method instead of get? - jsp/servlets

I have a simple form which accepts a username and a password. I have to use sendRedirect() method for the page to redirect to one page if log in is valid and to another if not. I need to use sendRedirect() and not forward() since the other pages are located in another server. I noticed that when using

response.sendRedirect(response.encodeRedirectURL("FileName.jsp?paramName=" +value));

the sendRedirect() is using the GET method since name=value are being shown in the URL. This is not desirable for me since I don't want these values to show in the URL for safety reasons.

Is there a way to POST these values using sendRedirect() ? I tried to do a form with method POST which hides the values I need but still no luck

What can I do please? Thanks :)

like image 666
Bernice Avatar asked Dec 26 '12 10:12

Bernice


People also ask

What is the use of the sendRedirect () method?

sendRedirect() method redirects the response to another resource, inside or outside the server. It makes the client/browser to create a new request to get to the resource. It sends a temporary redirect response to the client using the specified redirect location URL.

What is the difference between sendRedirect () and forward () in a servlet?

Difference between forward() and sendRedirect() methodThe forward() method works at server side. The sendRedirect() method works at client side. It sends the same request and response objects to another servlet. It always sends a new request.

In which scenario we should use forward and sendRedirect ()?

Let's consider a scenario, where we need to redirect the request from a servlet to other servlets which is in another server, in this case, we can use the sendRedirect() method so that the client/browser knows where the request is going and getting processed.

What is the key difference between using a jsp forward and HttpServletResponse sendRedirect ()? *?

First and most important difference between the forward() and sendRedirect() method is that in the case of the former, redirect happens at the server end and not visible to the client, but in case of later, redirection happens at the client end and it's visible to the client. 2.


3 Answers

No, it's not possible. The only (dirty) workaround I see is to forward to an internal page containing a hidden form (with method POST) and a JavaScript script submitting this form.

like image 81
JB Nizet Avatar answered Oct 18 '22 14:10

JB Nizet


This is kinda old, but here I've succesfully run this:

response.setStatus(307); //this makes the redirection keep your requesting method as is.
response.addHeader("Location", "http://address.to/redirect");

See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8 for explanation of HTTP 307 status code.

like image 37
a.b Avatar answered Oct 18 '22 13:10

a.b


use javascript

$('#inset_form').html('<form action="FlowService" name="form" method="post" style="display:none;"><input type="hidden" name="idapp" value="' + idApp + '" /></form>');
        document.forms['form'].submit();
like image 2
Daniel Kennedy Avatar answered Oct 18 '22 15:10

Daniel Kennedy