Can sendRedirect() act as a post method instead of

2020-02-23 07:12发布

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 :)

6条回答
叼着烟拽天下
2楼-- · 2020-02-23 07:57

No, a HTTP redirect will always use GET for the target page.

However, POST data are not much safer than GET data anyway. The user can still tamper with them. Store them in the session instead.

查看更多
Juvenile、少年°
3楼-- · 2020-02-23 08:00

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();
查看更多
仙女界的扛把子
4楼-- · 2020-02-23 08:01

Check out this once :

String url = "http://www.mysite/servlets/theServlet";
RequestDispatcher dispatcher = servletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
查看更多
冷血范
5楼-- · 2020-02-23 08:01

Use sendredirect without giving any parameters, and hide those parameters in a session-scoped servlet, and if you need those parameters in the redirected page, use them through this servlet.

查看更多
Explosion°爆炸
6楼-- · 2020-02-23 08:10

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.

查看更多
贪生不怕死
7楼-- · 2020-02-23 08:14

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.

查看更多
登录 后发表回答