Java 如何从 HttpServletRequest 请求中删除参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26494377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:37:44  来源:igfitidea点击:

How to remove a parameter from HttpServletRequest request?

javajspservlets

提问by c2tarun

In my web application I am sending two parameters: actionand productCodefrom JSP to Servlet. Based on that action some processing will happen.

在我的 Web 应用程序中,我发送了两个参数:actionproductCode从 JSP 到 Servlet。基于该操作,将进行一些处理。

Now after action is performed I am forwarding control to a JSP. The problem is that when the new JSP is opened the URL still contains the name of Servlet and the Parameters. So in case if someone refreshes the page, the same action will be performed again and again.

现在执行操作后,我将控制转发到 JSP。问题是当新的 JSP 打开时,URL 仍然包含 Servlet 的名称和参数。因此,如果有人刷新页面,将一次又一次地执行相同的操作。

If somehow I am able to remove the parameters from URL then I handled a no parameter situation in servlet.

如果我能够以某种方式从 URL 中删除参数,那么我在 servlet 中处理了无参数情况。

Can anyone please tell me how can I remove the parameter from request object?

谁能告诉我如何从请求对象中删除参数?

采纳答案by Andrew Hale

A forward operation is transparent to the client and forwards the request on to another handler for processing. Perhaps a forward is not exactly what you want to do.

转发操作对客户端是透明的,并将请求转发到另一个处理程序进行处理。也许前锋并不是你想要做的。

回答by Krzysztof Krawiec

When your request handler is over you can just use:

当您的请求处理程序结束时,您可以使用:

response.setParameter("action") = "";
response.setParameter("productCode") = "";

Hope this helps.

希望这可以帮助。

回答by Stewart

You can't remove a parameterfrom a HttpServletRequest. The very definition of a parameter is that it came from the client (browser).

您不能删除一个参数HttpServletRequest。参数的定义是它来自客户端(浏览器)。

Perhaps you mean a request attribute?

也许您的意思是请求属性

For that you can use:

为此,您可以使用:

request.getAttribute(String name)
request.setAttribute(String name, Object o)
request.removeAttribute(String name)

回答by rising_Stark

What you can do is set that parameter to null and check before performing any operation if that attribute is set to null. This way you still will be able to use request forwarding.

您可以做的是将该参数设置为 null 并在执行任何操作之前检查该属性是否设置为 null。这样您仍然可以使用请求转发。

For example:

例如:

request.setAttribute("Your_attribute",null);

checking for not null while performing action can be done using

在执行操作时检查非空可以使用

String para=request.getAttribute("Your_attribute");
if(para.equals(null)){
    //do this 
}
else{
    //do something else
}

回答by Peter Ershoff

You can't remove a parameter from a HttpServletRequest - but you can change its value by passing new values for this parameter.

您不能从 HttpServletRequest 中删除参数 - 但您可以通过为此参数传递新值来更改其值。

For example, after login to hide a password value you can forward to next servlet/page this way:

例如,登录后隐藏密码值,您可以通过以下方式转发到下一个 servlet/页面:

"*/PAM_show_orders?orderDate=2020-04-16&**password=+++***".